current position:Home>MNIST dataset with KNN algorithm

MNIST dataset with KNN algorithm

2022-02-02 20:54:05 CSDN Q & A

The phenomenon and background of the problem
Operation results and error reporting contents
My solution ideas and tried methods
What I want to achieve

Refer to the answer 1:
#!/usr/bin/env python# coding: utf-8# In[1]:from sklearn.metrics import confusion_matrixfrom sklearn.metrics import accuracy_scorefrom sklearn.metrics import classification_reportimport numpy as npimport pandas as pdimport matplotlib.pyplot as plt# In[2]:train_data = pd.read_csv('mnist_train.csv')train_data.dropna(inplace=True)train_feature = []mid_data = np.array(train_data)for i in mid_data:    train_feature.append(i[1:])train_label = (mid_data.T)[0]print(len(train_feature),len(train_label))print(train_feature[0].shape)# In[3]:test_data = pd.read_csv('mnist_test.csv')test_data.dropna(inplace=True)test_feature = []mid_data = np.array(test_data)for i in mid_data:    test_feature.append(i[1:])test_label = (mid_data.T)[0]print(len(test_feature),len(test_label))print(test_feature[0].shape)# In[5]:plt.imshow(np.reshape(test_feature[8], (28, 28)), In[8]:from sklearn.neighbors import KNeighborsClassifierknn = KNeighborsClassifier(n_neighbors=45) # Define a knn Classifier object, train_label)  # Call the training method of the object , It mainly receives two parameters : Training data set and its sample label y_predict = knn.predict(test_feature[:500])# In[9]:# test fenzi = 0for i in range(len(y_predict)):    if y_predict[i] == test_label[i]:        fenzi += 1    else:        passprint(' The test accuracy is :',float(fenzi)/len(y_predict))#100%=1print(' Error rate :',1-float(fenzi)/len(y_predict))

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended