我试图在k的不同值上找到模型上的训练误差。
这是我的代码:
x_eq = np.array(equal_scales[['x1','x2']])
y_eq = np.array(equal_scales[['category']])
y_eq = y_eq.ravel()
train_error_list = []
for i in range(30):
i = i+1
knn4 = KNeighborsClassifier(n_neighbors=i)
knn4.fit(x_eq, y_eq)
#evaluate accuracy training-error
train_accuracy = knn2.score(x_eq, y_eq)
train_error = 1 - train_accuracy
train_error_list.append(train_error)
x_eq
是一个numpy数组,其形状(100,2)如下所示:
array([[ -1.33255739e-01, -1.68917051e+00],
[ 1.22342923e+00, -4.58386227e-01],
[ -7.50266300e-01, 1.84619283e-01],
[ -5.75475318e-01, 7.98265338e-01],
[ -2.59622198e-01, -8.56879897e-01],
[ -4.54110670e-01, 1.36136079e+00],
[ 6.87128005e-01, -1.56213731e+00],
[ -8.52919125e-01, -2.15160667e-01],
[ 1.08863420e+00, 1.88649799e+00],
[ -6.08777374e-05, 1.43126563e+00],
[ -4.00791901e-01, 2.06030370e-01],
[ 7.36267212e-01, 2.29608975e-01],
y_eq是一个numpy数组,其形状(100,)如下所示:
array(['B', 'C', 'B', 'B', 'B', 'B', 'C', 'B', 'C', 'B', 'B', 'C', 'B',
'C', 'B', 'D', 'B', 'B', 'A', 'B', 'A', 'D', 'B', 'A', 'B', 'C',
'B', 'B', 'B', 'C', 'B', 'C', 'B', 'B', 'C', 'B', 'B', 'B', '
我收到以下错误消息:
ValueError: query data dimension must match training data dimension
我之前的模型非常相似,我不确定这个模型与我以前的测试有什么不同。谢谢你的帮助!