python - 在我自己实现的 kNN 算法中找到训练和测试错误
问题描述
我已经用 python 中的 iris 数据集实现了我自己的 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')
推荐阅读
- corda - 如何使用 Corda 创建帐户?
- python - 如何在 python 中使用 keras 训练具有列表数组的神经网络
- linux - Spring Security getprincipal() 方法返回字符串(用户名)代替类 UserDetails
- python-3.x - 如何将多行字典转换为单个格式化字典?- Python
- java - 读取字符串后将 int 位置设置为行
- triggers - Azure 数据工厂 ADFV2 触发器重叠
- c# - CustomHost 导致 T4 模板转换出错
- c++ - 如何使用 protoc 的 decode 选项对 google::protobuf:any 类型进行解码
- python - 如何将 cov 函数用于数据集 iris python
- r - 您可以使用 lapply/sapply on 将一个列表的元素应用到另一个列表的相应元素吗?