Python KNN:TypeError:在0-d数组上迭代
我正在使用()中的KNN代码处理我自己的数据。(我没有使用Iris数据集。)我已经为这篇文章缩减了数据量,这样我就可以在这里包含这两个数组Python KNN:TypeError:在0-d数组上迭代,python,pandas,scikit-learn,knn,Python,Pandas,Scikit Learn,Knn,我正在使用()中的KNN代码处理我自己的数据。(我没有使用Iris数据集。)我已经为这篇文章缩减了数据量,这样我就可以在这里包含这两个数组 working_df.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 475 entries, 236582 to 237060 Data columns (total 4 columns): Salinity 475 non-null floa
working_df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 475 entries, 236582 to 237060
Data columns (total 4 columns):
Salinity 475 non-null float64
Temperature 475 non-null float64
Calculated Chlorophyll 475 non-null float64
Station_group 475 non-null object
我从KNN代码中得到错误:
ValueError: RGBA values should be within 0-1 range
(从调用plt.pcolormesh(xx,yy,Z,cmap=cmap_light))
及
(从调用plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap\u bold,edgecolor='k',s=20)
)
我发誓这在今天早些时候起了作用。我甚至不知道它在哪里“迭代”
这是我正在使用的代码(不是我写的)
X:
y:
回溯:
看起来您的CMAP中只有3种颜色,但有6个类需要为每个类指定一种颜色。尝试在列出的颜色映射中列出6种颜色,而不是3种颜色。绘制数据时会出现错误:
plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap\u bold,edgecolor='k',s=20)
Um,是吗?我知道错误在哪里。我不知道为什么。我只是指出有两个错误:调用散点图时c=y的值错误和类型错误。类型错误发生在plt.pcolormesh(xx,yy,Z,cmap=cmap_light)
我不熟悉pcolormesh
,所以我无法帮助。@Chris-抱歉;我误解了。感谢您注意到c=y的值错误(它可能在该调用中的任何地方)。
ValueError: RGBA values should be within 0-1 range
TypeError: iteration over a 0-d array
n_neighbors = 3
h = .02 # step size in the mesh
# Create color maps
cmap_light = ListedColormap(['orange', 'cyan', 'cornflowerblue'])
cmap_bold = ListedColormap(['darkorange', 'c', 'darkblue'])
for weights in ['uniform', 'distance']:
# we create an instance of Neighbours Classifier and fit the data.
clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
clf.fit(X, y)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("3-Class classification (k = %i, weights = '%s')"
% (n_neighbors, weights))
plt.show()
array([[16.7 , 20.88],
[16. , 21.22],
[16.7 , 20.87],
[15.87, 21.22],
[16.29, 21.1 ],
[16.6 , 20.94],
[15.92, 21.23],
[17.1 , 21. ],
[17.3 , 20.91],
[17.09, 20.99],
[17.48, 20.86],
[17.23, 20.95],
[17.36, 20.88],
[17.09, 21. ],
[17.29, 20.92],
[17.08, 21. ],
[17.42, 20.87],
[17.77, 20.69],
[18.13, 20.5 ],
[17.59, 20.83],
[17.88, 20.62],
[18.17, 20.48],
[17.68, 20.75],
[18.07, 20.53],
[17.59, 20.83],
[18.13, 20.49],
[18.19, 20.78],
[18.46, 20.7 ],
[18.29, 20.76],
[18.41, 20.72],
[18.23, 20.78],
[18.35, 20.75],
[17.88, 20.7 ],
[18.44, 20.71],
[18.25, 20.77],
[18.38, 20.73],
[18.26, 20.53],
[19.21, 20.12],
[18.78, 20.46],
[19.14, 20.18],
[18.61, 20.52],
[18.84, 20.42],
[19.18, 20.14],
[18.68, 20.52],
[18.99, 20.3 ],
[19.65, 19.83],
[19.85, 19.65],
[19.73, 19.76],
[19.59, 19.87],
[19.79, 19.71],
[19.71, 19.78],
[19.86, 19.63],
[19.75, 19.75],
[19.62, 19.84],
[19.82, 19.67],
[19.72, 19.77],
[19.58, 19.89],
[19.77, 19.72],
[19.9 , 19.59],
[20.1 , 19.38],
[19.96, 19.53],
[19.89, 19.62],
[20.1 , 19.37],
[19.92, 19.57],
[20. , 19.49],
[19.9 , 19.61],
[20.11, 19.36],
[19.94, 19.55],
[19.89, 19.63],
[20.07, 19.41],
[20.11, 19.39],
[20.18, 19.29],
[20.14, 19.33],
[20.16, 19.3 ],
[20.12, 19.36],
[20.18, 19.29],
[20.15, 19.32],
[20.18, 19.29],
[20.13, 19.34],
[20.16, 19.31],
[20.58, 19.02],
[20.58, 19.01],
[20.6 , 19. ],
[20.58, 19.02],
[20.59, 19.01],
[20.56, 19.04],
[20.61, 18.99],
[20.58, 19.01],
[20.6 , 19. ],
[21.14, 18.75],
[21.22, 18.63],
[21.27, 18.59],
[21.14, 18.73],
[21.24, 18.61],
[21.18, 18.67],
[21.25, 18.6 ],
[21.87, 18.1 ],
[21.91, 18.03],
[21.8 , 18.29],
[21.92, 18.01],
[21.89, 18.06],
[21.91, 18.02],
[21.84, 18.17],
[21.9 , 18.04],
[21.92, 18.01],
[22.15, 17.79],
[22.31, 17.6 ],
[22.22, 17.75],
[21.99, 17.99],
[22.3 , 17.62],
[22.19, 17.77],
[22.32, 17.6 ],
[21.89, 18.24],
[22.22, 17.76],
[22.07, 17.88],
[22.31, 17.62],
[22.24, 17.74],
[21.93, 18.11],
[22.29, 17.65],
[22.1 , 18.22],
[23.24, 16.8 ],
[22.73, 17.34],
[23.51, 16.49],
[22.17, 18.11],
[23.08, 16.98],
[23.62, 16.35],
[22.32, 17.86],
[23.33, 16.69],
[22.89, 17.17],
[23.57, 16.41],
[22.11, 18.21],
[23.16, 16.89],
[23.62, 16.35],
[22.52, 17.56],
[23.45, 16.56],
[22.99, 17.07],
[23.6 , 16.38],
[22.87, 17.34],
[23.43, 16.85],
[23.27, 16.94],
[23.71, 16.55],
[23.32, 16.92],
[24.11, 16.36],
[23.04, 17.14],
[23.57, 16.74],
[23.31, 16.92],
[23.86, 16.41],
[22.61, 17.66],
[23.35, 16.9 ],
[24.2 , 16.33],
[23.18, 17. ],
[23.68, 16.65],
[23.32, 16.92],
[23.98, 16.4 ],
[22.08, 15.89],
[23.61, 16.02],
[24.33, 15.94],
[22.82, 16. ],
[23.9 , 16. ],
[24.34, 15.93],
[21.8 , 15.95],
[23.39, 16.04],
[24.29, 15.96],
[24.63, 15.79],
[22.58, 15.9 ],
[23.67, 16.01],
[24.34, 15.93],
[21.86, 15.93],
[22.98, 16.08],
[24.09, 15.98],
[24.36, 15.91],
[21.87, 15.93],
[23.48, 16.03],
[24.32, 15.95],
[22.71, 15.96],
[23.73, 16.01],
[24.35, 15.92],
[21.81, 15.95],
[23.15, 16.08],
[24.22, 15.96],
[24.36, 15.92],
[24.12, 15.3 ],
[24.71, 15.08],
[25.11, 14.96],
[26.25, 14.6 ],
[26.36, 14.56],
[23.57, 15.56],
[24.23, 15.26],
[24.93, 15.01],
[25.51, 14.83],
[26.23, 14.6 ],
[26.42, 14.54],
[23.79, 15.44],
[24.56, 15.13],
[25.06, 14.97],
[26.07, 14.65],
[26.31, 14.57],
[24.17, 15.28],
[24.84, 15.04],
[25.29, 14.9 ],
[26.24, 14.6 ],
[26.41, 14.54],
[23.69, 15.5 ],
[24.28, 15.24],
[24.97, 15. ],
[25.57, 14.81],
[26.21, 14.61],
[26.43, 14.53],
[23.86, 15.41],
[24.6 , 15.12],
[25.07, 14.98],
[26.02, 14.67],
[26.33, 14.57],
[24.19, 15.27],
[24.89, 15.02],
[25.48, 14.84],
[26.23, 14.6 ],
[26.48, 14.52],
[23.77, 15.46],
[24.37, 15.2 ],
[25.03, 14.98],
[25.86, 14.72],
[26.33, 14.57],
[16.07, 17.03],
[22.22, 15.72],
[19.57, 16.38],
[21.59, 15.88],
[17.38, 16.82],
[22.57, 15.62],
[19.71, 16.34],
[15.6 , 17.15],
[21.84, 15.82],
[18.69, 16.57],
[22.67, 15.6 ],
[20.64, 16.12],
[14.96, 17.07],
[16.85, 16.72],
[13.66, 17.49],
[18.33, 16.51],
[15.36, 16.99],
[17.69, 16.59],
[14.44, 17.19],
[16.06, 16.85],
[12.9 , 18.12],
[18.04, 16.53],
[13.52, 17.37],
[17.95, 16.54],
[12.19, 17.92],
[15.43, 16.99],
[18.95, 16.4 ],
[12.72, 17.93],
[17.06, 16.7 ],
[19.53, 16.29],
[11.86, 18.11],
[13.84, 17.26],
[18.85, 16.4 ],
[12.23, 18. ],
[16.33, 16.83],
[18.98, 16.4 ],
[13.11, 17.63],
[17.24, 16.66],
[19.96, 16.2 ],
[12.07, 17.85],
[14.68, 17.12],
[18.94, 16.39],
[12.35, 18.09],
[16.66, 16.77],
[19.15, 16.36],
[13.59, 17.28],
[16.67, 16.78],
[11.92, 17.61],
[15.39, 17. ],
[12.51, 17.47],
[16. , 16.91],
[14.16, 17.18],
[11.93, 17.6 ],
[15.88, 16.93],
[12.87, 17.4 ],
[16.29, 16.85],
[11.86, 17.67],
[14.62, 17.11],
[12.15, 17.55],
[15.9 , 16.93],
[10.54, 17.7 ],
[ 9.53, 17.85],
[ 9.29, 18.04],
[10.2 , 17.74],
[ 9.4 , 17.92],
[ 5.33, 18.14],
[ 4.31, 18.59],
[ 5.91, 18.06],
[ 4.97, 18.23],
[ 5.54, 18.11],
[ 4.55, 18.39],
[ 5.13, 18.19],
[ 5.79, 18.08],
[ 4.78, 18.29],
[ 3. , 18.37],
[ 3.57, 18.3 ],
[ 4.63, 18.2 ],
[ 3.4 , 18.32],
[ 4.41, 18.22],
[ 2.69, 18.42],
[ 3.48, 18.31],
[ 4.54, 18.21],
[ 3.21, 18.34],
[ 3.9 , 18.27],
[ 3.43, 18.32],
[ 4.47, 18.22],
[ 2.87, 18.39],
[ 3.51, 18.31],
[ 4.57, 18.21],
[ 3.37, 18.32],
[ 4.22, 18.24],
[ 2.43, 18.47],
[ 3.46, 18.31],
[ 4.52, 18.22],
[ 0.64, 18.89],
[ 0.77, 18.83],
[ 0.87, 18.79],
[ 0.67, 18.88],
[ 0.53, 19.14],
[ 0.74, 18.84],
[ 0.64, 18.89],
[ 0.8 , 18.82],
[ 0.91, 18.78],
[ 0.69, 18.86],
[ 0.61, 18.9 ],
[ 0.75, 18.84],
[ 0.65, 18.88],
[ 0.84, 18.8 ],
[ 0.98, 18.75],
[ 0.5 , 19.28],
[ 0.72, 18.85],
[ 0.33, 19.05],
[ 0.36, 19.01],
[ 0.36, 19. ],
[ 0.35, 19. ],
[ 0.35, 19.01],
[ 0.36, 19.01],
[ 0.31, 19.19],
[ 0.35, 19.01],
[ 0.36, 19.01],
[ 0.38, 19.03],
[ 0.35, 19.01],
[ 0.36, 19.01],
[ 0.36, 19. ],
[ 0.36, 19. ],
[ 0.36, 19.01],
[ 0.37, 19.02],
[ 0.32, 19.1 ],
[ 0.36, 19.01],
[ 0.36, 19. ],
[ 0.39, 19.03],
[ 0.35, 19.01],
[ 0.36, 19.01],
[ 0.36, 19.01],
[ 0.36, 19. ],
[ 0.36, 19.01],
[ 0.38, 19.03],
[ 0.16, 19.25],
[ 0.16, 19.31],
[ 0.18, 19.22],
[ 0.16, 19.26],
[ 0.21, 19.13],
[ 0.16, 19.34],
[ 0.16, 19.24],
[ 0.16, 19.3 ],
[ 0.21, 19.16],
[ 0.16, 19.25],
[ 0.22, 19.13],
[ 0.16, 19.31],
[ 0.17, 19.24],
[ 0.16, 19.27],
[ 0.21, 19.14],
[ 0.07, 19.48],
[ 0.07, 19.47],
[ 0.07, 19.48],
[ 0.07, 19.5 ],
[ 0.07, 19.48],
[ 0.07, 19.48],
[ 0.07, 19.47],
[ 0.07, 19.94],
[ 0.07, 19.48],
[ 0.07, 19.5 ],
[ 0.07, 19.47],
[ 0.07, 19.48],
[ 0.07, 19.47],
[ 0.07, 19.52],
[ 0.07, 19.48],
[ 0.07, 19.45],
[ 0.07, 19.44],
[ 0.07, 19.58],
[ 0.07, 19.44],
[ 0.07, 19.82],
[ 0.07, 19.43],
[ 0.07, 19.47],
[ 0.07, 19.43],
[ 0.07, 19.73],
[ 0.07, 19.42],
[ 0.07, 19.63],
[ 0.07, 19.62],
[ 0.07, 19.63],
[ 0.07, 19.61],
[ 0.07, 19.62],
[ 0.07, 19.98],
[ 0.07, 19.62],
[ 0.07, 19.63],
[ 0.07, 19.62],
[ 0.07, 19.78],
[ 0.07, 19.62],
[ 0.07, 19.92],
[ 0.07, 19.69],
[ 0.07, 19.81],
[ 0.07, 19.69],
[ 0.07, 20.1 ],
[ 0.07, 19.7 ],
[ 0.07, 19.68],
[ 0.07, 19.86],
[ 0.07, 19.69],
[ 0.07, 19.76],
[ 0.07, 19.69],
[ 0.07, 20.07],
[ 0.07, 19.7 ],
[ 0.07, 19.82],
[ 0.07, 19.68],
[ 0.07, 19.71],
[ 0.07, 19.68],
[ 0.06, 19.64],
[ 0.07, 20.24],
[ 0.06, 19.63],
[ 0.06, 19.7 ],
[ 0.06, 19.62],
[ 0.06, 19.63],
[ 0.06, 19.89],
[ 0.06, 19.63],
[ 0.06, 19.68],
[ 0.06, 19.62],
[ 0.07, 20.55],
[ 0.06, 19.63],
[ 0.06, 19.8 ],
[ 0.06, 19.62],
[ 0.06, 19.81],
[ 0.07, 20.3 ],
[ 0.06, 19.82],
[ 0.06, 19.91],
[ 0.07, 20.8 ],
[ 0.06, 19.81],
[ 0.07, 20.22],
[ 0.06, 19.85],
[ 0.07, 20.39],
[ 0.06, 19.82],
[ 0.06, 19.99],
[ 0.06, 19.22],
[ 0.06, 19.89],
[ 0.06, 19.18],
[ 0.06, 19.49],
[ 0.06, 19.22],
[ 0.06, 19.8 ],
[ 0.06, 19.18],
[ 0.06, 19.33],
[ 0.06, 19.93],
[ 0.06, 19.2 ],
[ 0.06, 19.57],
[ 0.06, 19.18],
[ 0.08, 19.86],
[ 0.08, 19.53],
[ 0.08, 19.56],
[ 0.08, 19.55],
[ 0.08, 19.74],
[ 0.08, 19.5 ],
[ 0.08, 19.56],
[ 0.08, 19.54],
[ 0.08, 19.57],
[ 0.08, 19.55]])
array(['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
'1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '6', '6', '6',
'6', '6', '6', '6', '6', '6', '6'], dtype=object)