Plot K-Means in Matplotlib: ValueError: x和y必须具有相同的第一个维度,但具有形



请帮帮我,谢谢

我想在图像上执行K均值聚类并在matplotib中绘制它,但是它一直显示此错误:

ValueError: x和y必须具有相同的第一个维度,但具有形状(10,)和(1,)

有人知道怎么解决这个问题吗?我的代码如下:

import cv2
import numpy as np
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
image = cv2.imread(r"C:UsersKaChunDesktoprotapple.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()

错误:x和y必须具有相同的第一个维度,但具有形状(10,)
和(1,)有人知道怎么解吗?我的代码如下:

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()

错误:

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,)**

最新更新