Search code examples
pythondataframeknnconfusion-matrixmse

How can I measure the MSE error with Knn model?


Assume that I have a dataframe as follows :

   a  b  Class
0  1  2  yes
1  4  5  yes
2  7  8  No
3  10 5  No
4  4  5  No
5  1  2  No
6  8  1  yes
7  4  5  yes
8  7  8  No

and that I would like to predict the class of the following test_sample :

   a  b  Class
0  5  3   ?

So, I train my KNN model like :

from sklearn.neighbors import KNeighborsClassifier
k = 3
knn = KNeighborsClassifier(n_neighbors = k)
knn = knn.fit(Dataset.drop("Class", axis=1), Dataset["Class"])
knn.predict(test_sample)

My aim is how can I measure the MSE error and how can I compute the confusion matrix ?


Solution

  • Let's take an example:

    import pandas as pd
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.metrics import mean_squared_error
    from sklearn import preprocessing
    from sklearn.metrics import confusion_matrix
    k = 3
    
    Dataset = pd.DataFrame({'a':[1,4,7,10,4,1,8,4],'b':[2,5,8,5,5,2,1,5],'Class':['y','y','n','n','n','n','y','y']})
    knn = KNeighborsClassifier(n_neighbors = k)
    knn = knn.fit(Dataset.drop("Class", axis=1), Dataset["Class"])
    
    test_ds = pd.DataFrame({'a':[1,4,1,1,4,1,8,4],'b':[2,1,1,5,1,2,1,5],'Class':['y','y','n','n','n','n','y','y']})
    y_pred = knn.predict(test_ds.drop("Class", axis=1))
    y_true = test_ds['Class']
    y_true = y_true.values
    le = preprocessing.LabelEncoder() # We are using label encoder to convert categorical labels to number
    le.fit(y_true) # Since this array contains both classes 'y' and 'n'.
    print(list(le.classes_)) # To check the classes which are encoded
    
    y_true = le.transform(y_true) 
    y_pred = le.transform(y_pred)
    MSE = mean_squared_error(y_true, y_pred) # Calculating MSE 
    print(MSE)
    cm = confusion_matrix(y_true,y_pred) # Creation of Confusion Matrix
    print(cm)