在我自己实现的 kNN 算法中找到训练和测试错误

Find the training and test error in my self implemented kNN-algorithm

我已经使用 python 中的鸢尾花数据集实现了我自己的 kNN 算法。现在我希望能够报告不同类型 k 的训练和测试错误。我已经计算了我的预测的准确性,但真的不知道如何从中得到训练和测试错误。有什么想法吗?

提前致谢

编辑:这是代码

import pandas as pd
import math
import operator
from sklearn.model_selection import train_test_split


def euclideanDistance(instance1, instance2, length):
    distance = 0
    for x in range(length):
        distance += pow((instance1[x] - instance2[x]), 2)
    return math.sqrt(distance)


def getNeighbors(trainingSet, testInstance, k):
    distances = []
    length = len(testInstance) - 1
    for x in range(len(trainingSet)):
        dist = euclideanDistance(testInstance, trainingSet.iloc[x], length)
        distances.append((trainingSet.iloc[x], dist))
    distances.sort(key=operator.itemgetter(1))
    neighbors = []
    for x in range(k):
        neighbors.append(distances[x][0])
    return neighbors


def getResponse(neighbors):
    classVotes = {}
    for x in range(len(neighbors)):
        response = neighbors[x][-1]
        if response in classVotes:
            classVotes[response] += 1
        else:
            classVotes[response] = 1
        sortedVotes = sorted(classVotes.items(), key=operator.itemgetter(1), reverse=True)

    return sortedVotes[0][0]


def getAccuracy(testSet, predictions):
    correct = 0
    for x in range(len(testSet)):
        if testSet.iloc[x][-1] == predictions[x]:
            correct += 1
    return (correct / float(len(testSet))) * 100.0


def main():
    dataset = pd.read_csv('DataScience/iris.data.txt',
                          names=["Atr1", "Atr2", "Atr3", "Atr4", "Class"])

    x = dataset.drop(['Class'], axis=1)
    y = dataset.drop(["Atr1", "Atr2", "Atr3", "Atr4"], axis=1)

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5, random_state=65, stratify=y)

trainingSet = pd.concat([x_train, y_train], axis=1)
testSet = pd.concat([x_test, y_test], axis=1)
# prepare data

# generate predictions
predictions = []
k = 5
for x in range(len(testSet)):
    neighbors = getNeighbors(trainingSet, testSet.iloc[x], k)
    result = getResponse(neighbors)
    predictions.append(result)
    print('> predicted=' + repr(result) + ', actual=' + repr(testSet.iloc[x][-1]))
accuracy = getAccuracy(testSet, predictions)
print('Accuracy: ' + repr(accuracy) + '%')

主要()

训练误差和测试误差就是分别对训练集和测试集进行预测时的误差。

您需要做的就是测量对训练集和测试集的预测。

您可以将训练和测试错误视为准确性的另一面。例如,如果您在测试中的准确率为 60%,那么您在测试中将有大约 40% 的错误。通常您可以绘制精度与不同 k 的关系图,以了解您的算法在不同 k 下的表现。

from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier 
import matplotlib.pyplot as plt

# create a training and testing set (use your X and y)    
X_train,X_test, y_train, y_test= train_test_split(X,y,random_state=42, test_size=.3)
# create a set of k values and an empty list for training and testing accuracy scores
k_values=[1,2,3,4,5,6,7,8,9,10]
train_scores=[]
test_scores=[]
# instantiate the model 
k_nn=KNeighborsClassifier()
# create a for loop of models with different k's 

for k in k_values: 
  k_nn.n_neighbors=k 
  k_nn.fit(X_train,y_train)
  train_score=k_nn.score(X_train,y_train)
  test_score=k_nn.score(X_test,y_test)
  train_scores.append(train_score)
  test_scores.append(test_score)

plt.plot(k_values,train_scores, color='red',label='Training Accuracy')
plt.plot(k_values,test_scores, color='blue',label='Testing Accuracy')
plt.xlabel('K values')
plt.ylabel('Accuracy Score')
plt.title('Performace Under Varying K Values')