[英]I got this error: "f x and y must have same first dimension but have shapes python"
[英]Python Error: x and y must have same first dimension, but have shapes (8,) and (1,)
#我正在尝试读取python给定的数据并应用KNN。 但是我的图没有数据,我得到的ax和y必须具有相同的第一维,但形状为(8,)和(1,)误差。
# I have tried using .shape to no luck.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
df = pd.read_csv ('/content/heart.csv')
df.head()
df.shape
x = df.drop('target', axis=1).values
y = df['target'].values
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y, test_size=0.4,random_state=42, stratify=y)
from sklearn.neighbors import KNeighborsClassifier
neighbors = np.arange(1,9)
train_accuracy = np.empty(len(neighbors))
test = np.empty(len(neighbors))
knn = KNeighborsClassifier(n_neighbors=2)
knn.fit(x_train, y_train)
train_accuracy = knn.score(x_train,y_train)
test_accuracy = knn.score(x_test, y_test)
test_accuracy = knn.score(x_test, y_test)
plt.title('k-NN Varying number of neighbors')
plt.plot(neighbors, test_accuracy, label='Testing Accuracy')
plt.plot(neighbors, train_accuracy, label='Training Accuracy')
plt.legend()
plt.xlabel('Number of neighbors')
plt.ylabel('Accuracy')
plt.show()
Should plot a graph,
however, the graph has no info on it.
的shapes (8,) and (1,) error
是因为你想要绘制与图表neighbors
上x轴,其具有在其内部和8倍的值(numpy的阵列) test_accuracy
上y轴只具有一个值的score
。 此图没有意义,无法绘制。 我不确定您要在程序中实现什么。 您可以更新所需的结果吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.