In Python machine learning, how do I write integration tests?

Integration tests are essential in Python machine learning applications to ensure that different modules of the code work together as expected. Below is an example of how to write integration tests for a simple machine learning pipeline using the `unittest` framework.

import unittest from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score class TestMLPipeline(unittest.TestCase): def setUp(self): self.data = load_iris() self.X_train, self.X_test, self.y_train, self.y_test = train_test_split( self.data.data, self.data.target, test_size=0.2, random_state=42) self.model = RandomForestClassifier() def test_model_training(self): self.model.fit(self.X_train, self.y_train) predictions = self.model.predict(self.X_test) self.assertEqual(len(predictions), len(self.y_test), "Predictions should match the test set size.") def test_model_accuracy(self): self.model.fit(self.X_train, self.y_train) predictions = self.model.predict(self.X_test) accuracy = accuracy_score(self.y_test, predictions) self.assertGreater(accuracy, 0.7, "The model accuracy should be greater than 70%.") if __name__ == '__main__': unittest.main()

Python Machine Learning Integration Testing Unittest Scikit-learn