DEV Community

loading...

Day 98 Of 100DaysOfCode: Learning About Confusion Matrix

iamdurga profile image Durga Pokharel ・2 min read

Today, is my 98th day of #100daysofcode and #python learning journey. Today sometimes I learned more about streamlit. Like usual day today I also kept learning from DataCamp about the topic Cross validation and Confusion Matrix.

While studying I learned that confusion matrix counts the number of instance when the model predicted the outcome of an event and measure it against the actual value. Similarly cross validation maximize the availability of training data by splitting data into various combination and testing each specific combination.

Code for Confusion Matrix

# Import necessary modules
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

# Create training and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state=42)

# Instantiate a k-NN classifier: knn
knn = KNeighborsClassifier(n_neighbors=6)

# Fit the classifier to the training data
knn.fit(X_train, y_train)

# Predict the labels of the test data: y_pred
y_pred = knn.predict(X_test)

# Generate the confusion matrix and classification report
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred)
Enter fullscreen mode Exit fullscreen mode

Output of the codewill be

[[176  30]
 [ 52  50]]
             precision    recall  f1-score   support

          0       0.77      0.85      0.81       206
          1       0.62      0.49      0.55       102

avg / total       0.72      0.73      0.72       308
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

pic
Editor guide
Collapse
mccurcio profile image
Matt Curcio

One of my favorite videos series is by Victor Lavrenko
;)

Collapse
iamdurga profile image
Durga Pokharel Author

Thank you for sharing to me.