簡體   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,)

請幫助我,謝謝

我想在 matplotib 中對圖像和 plot 執行 K 均值聚類,但是它一直顯示此錯誤:

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

錯誤: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,)**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM