Building and Evaluating a Model
1 min readConcept: Now that the data is preprocessed and ready, we can build a machine learning model. Models like K-Nearest Neighbors (KNN), Decision Trees, and Logistic Regression help us classify or predict based on the input data. We also evaluate how well the model performs using metrics like accuracy, precision, and recall.
Simple Example: Let’s build a model that predicts whether a student will pass or fail based on their scores. First, we train the model with some sample data:
from sklearn.neighbors import KNeighborsClassifier x_train = [[50, 55], [60, 65], [70, 75], [85, 80]] # Scores in two subjects y_train = [0, 0, 1, 1] # 0 = Fail, 1 = Pass knn = KNeighborsClassifier(n_neighbors=3) knn.fit(x_train, y_train)
Once the model is trained, we can use it to predict whether a new student will pass or fail:
x_test = [[65, 70]] prediction = knn.predict(x_test) print('Predicted:', 'Pass' if prediction == 1 else 'Fail')
knn_model = KNeighborsClassifier() knn_model.fit(train_X, train_y) predictions = knn_model.predict(test_X)
from sklearn.metrics import confusion_matrix print(confusion_matrix(test_y, predictions))
Building and evaluating models is the final step in the machine learning pipeline, and it’s crucial to choose the right model and evaluate its effectiveness using appropriate metrics.