如何使用 Knn 模型测量 MSE 误差?
How can I measure the MSE error with Knn model?
假设我有一个数据框如下:
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
并且我想预测以下 test_sample 的 class :
a b Class
0 5 3 ?
所以,我像这样训练我的 KNN 模型:
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)
我的目标是如何测量 MSE 误差以及如何计算混淆矩阵?
举个例子:
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)
假设我有一个数据框如下:
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
并且我想预测以下 test_sample 的 class :
a b Class
0 5 3 ?
所以,我像这样训练我的 KNN 模型:
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)
我的目标是如何测量 MSE 误差以及如何计算混淆矩阵?
举个例子:
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)