[英]Plot K-Means in Matplotlib: ValueError: x and y must have same first dimension, but have shapes (10,) and (1,)
Please help me, thanks请帮助我,谢谢
I would like to perform a K means clustering on an image and plot it in matplotib, however it keep show this error:我想在 matplotib 中对图像和 plot 执行 K 均值聚类,但是它一直显示此错误:
ValueError: x and y must have same first dimension, but have shapes (10,) and (1,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (1,)
Anyone know how to solve this?有谁知道如何解决这个问题? My code is as shown below:
我的代码如下所示:
import cv2
import numpy as np
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
image = cv2.imread(r"C:\Users\KaChun\Desktop\rotapple.jpg")
reshaped = image.reshape(image.shape[0] * image.shape[1], image.shape[2])
wcss = []
for i in range(1,11):
kmeans = KMeans(n_clusters=i, init ='k-means++', max_iter=300, n_init=10,random_state=0 )
kmeans.fit(reshaped)
wcss.append(kmeans.inertia_)
plt.plot(range(1,11),wcss)
plt.title('The Elbow Method Graph')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
Error: x and y must have same first dimension, but have shapes (10,)错误:x 和 y 必须具有相同的第一维,但具有形状 (10,)
and (1,) Anyone know how to solve this?和(1,)有人知道如何解决这个问题吗? My code is as shown below:
我的代码如下所示:
import numpy as np
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
# generating own data make_blobs
X,y = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
plt.scatter(X[:,0],X[:,1])
#elbow method
wcss=[]
for i in range(1,11):
kmeans=KMeans(n_clusters=i,init="k-means++",max_iter=300,n_init=10,random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
plt.plot(range(1,11), wcss)
plot.title('elbow method')
plot.xlabel("number of clusters")
plot.ylabel('wcss')
plt.show()
Error:错误:
if x.shape[0] != y.shape[0]:
raise ValueError(f"x and y must have same first dimension, but "f"have
shapes {x.shape} and {y.shape}")
if x.ndim 2 or y.ndim 2:
ValueError: x and y must have same first dimension, but have shapes (10,)
and (1,)**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.