简体   繁体   English

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

[英]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.

相关问题 ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' ValueError: x 和 y 必须具有相同的第一维,但有形状,tkinter 和 matplotlib 问题 - ValueError: x and y must have same first dimension, but have shapes, tkinter and matplotlib problem 谁能帮我? ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10,) 和 (0,) - Can anyone help me? ValueError: x and y must have same first dimension, but have shapes (10,) and (0,) ValueError: x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (90,) - ValueError: x and y must have same first dimension, but have shapes (10, 1) and (90,) 线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1) - Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1) Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error Matplotlib:ValueError:x 和 y 必须具有相同的第一维 - Matplotlib: ValueError: x and y must have same first dimension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM