Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python x和y必须是相同的尺寸误差-K-表示散点图_Python_Matplotlib_Jupyter Notebook_Data Visualization_Scatter Plot - Fatal编程技术网

Python x和y必须是相同的尺寸误差-K-表示散点图

Python x和y必须是相同的尺寸误差-K-表示散点图,python,matplotlib,jupyter-notebook,data-visualization,scatter-plot,Python,Matplotlib,Jupyter Notebook,Data Visualization,Scatter Plot,我正试图绘制一个散点图,显示基于在我的数据集上运行的K-means算法的质心和簇。所以,它应该是这样的: 但是,我的代码不断抛出x和y必须大小相同的错误。以下是我正在使用的函数: import numpy as np import matplotlib.pyplot as plt def thefunction(filedata, centroids, labels, cmap_name='seaice_2'): df = pd.read_table('filedata',s

我正试图绘制一个散点图,显示基于在我的数据集上运行的K-means算法的质心和簇。所以,它应该是这样的:

但是,我的代码不断抛出x和y必须大小相同的错误。以下是我正在使用的函数:

import numpy as np
import matplotlib.pyplot as plt

def thefunction(filedata, centroids, labels, cmap_name='seaice_2'):
        df = pd.read_table('filedata',sep=' ', index_col= 0, skiprows = 6).iloc[:, 0:].dropna()
        values = df.values.reshape((len(df),2))
        centroids,labels = vq.kmeans2(values, 3, minit='points')
        plt.plot()
        plt.xlim([0, 10])
        plt.ylim([0, 10])
        plt.title('Dataset')
        plt.scatter(centroids, labels)
        plt.show()
        #   create new plot and data
        plt.plot()
        X = np.array(list(zip(centroids, labels))).reshape(len(centroids), 2)
        colors = ['b', 'g', 'r']
        markers = ['o', 'v', 's']
        # KMeans algorithm
        K = 3
        kmeans_model = KMeans(n_clusters=K).fit(X)
        plt.plot()
        for i, l in enumerate(kmeans_model.labels_):
            plt.plot(centroids[i], labels(df1)[i], color=colors[l], marker=markers[l],ls='None')
            plt.xlim([0, 10])
            plt.ylim([0, 10])
            plt.show()
我使用的数据或“值”如下所示:

array([[20.,   20.],
       [30., 30.],
       [ 40.,  40.],
       [50., 50.],
       [60., 60.],
       [70., 70.]
centroids,labels = vq.kmeans2(values, 2, minit='points')
centroids, labels
(array([[10.123, 20.232       ],
        [27.526, 18895]]),
 array([1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
        1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
        0, 0, 1], dtype=int32))
在数据上运行的kmeans算法返回如下结果:

array([[20.,   20.],
       [30., 30.],
       [ 40.,  40.],
       [50., 50.],
       [60., 60.],
       [70., 70.]
centroids,labels = vq.kmeans2(values, 2, minit='points')
centroids, labels
(array([[10.123, 20.232       ],
        [27.526, 18895]]),
 array([1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
        1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
        0, 0, 1], dtype=int32))
堆栈跟踪:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-132-fa95f1dffabc> in <module>()
     24 plt.ylim([0, 10])
     25 plt.title('Dataset')
---> 26 plt.scatter(centroids, len(df1))
     27 plt.show()
     28 

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, hold, data, **kwargs)
   3376                          vmin=vmin, vmax=vmax, alpha=alpha,
   3377                          linewidths=linewidths, verts=verts,
-> 3378                          edgecolors=edgecolors, data=data, **kwargs)
   3379     finally:
   3380         ax._hold = washold

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1715                     warnings.warn(msg % (label_namer, func.__name__),
   1716                                   RuntimeWarning, stacklevel=2)
-> 1717             return func(ax, *args, **kwargs)
   1718         pre_doc = inner.__doc__
   1719         if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   3953         y = np.ma.ravel(y)
   3954         if x.size != y.size:
-> 3955             raise ValueError("x and y must be the same size")
   3956 
   3957         if s is None:

ValueError: x and y must be the same size
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
24 plt.ylim([0,10])
25 plt.标题(“数据集”)
--->26 plt.散射(质心,透镜(df1))
27 plt.show()
28
~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py散点(x,y,s,c,marker,cmap,norm,vmin,vmax,alpha,线宽,顶点,边色,保持,数据,**kwargs)
3376 vmin=vmin,vmax=vmax,alpha=alpha,
3377线宽=线宽,顶点=顶点,
->3378 edgecolors=edgecolors,data=data,**kwargs)
3379最后:
3380 ax.\U hold=洗旧
内部的~/anaconda3/lib/python3.6/site packages/matplotlib/__init__.py(ax,*args,**kwargs)
1715警告。警告(消息%(标签名称,功能名称),
1716运行时警告,堆栈级别=2)
->1717返回函数(ax,*args,**kwargs)
1718预付款单=内部付款单__
1719如果pre_doc为无:
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes//u axes.py散点(self、x、y、s、c、marker、cmap、norm、vmin、vmax、alpha、线宽、顶点、边色、**kwargs)
3953 y=np.ma.ravel(y)
3954如果x尺寸!=y、 尺寸:
->3955提升值错误(“x和y必须大小相同”)
3956
3957如果s为无:
ValueError:x和y的大小必须相同
根据,plt.scatter的前两个参数应为点的x坐标和y坐标

在代码中

plt.scatter(centroids, labels)
您告诉函数
质心
标签
是x坐标和y坐标

你想做的应该是

plt.scatter(values[:,0], values[:,1], c=labels)       # Original points
plt.scatter(centroids[:,0], centroids[:,1], c=[0, 1]) # Clusters centroids

请你把代码适当地缩进好吗?看起来你在cmap\u name='seaice\u 2中漏掉了一个'in'。我更新了代码。希望有帮助!您尚未修复代码中的缩进,因此无法判断函数()的一部分是什么。此外,我看不到代码中任何地方实际调用了该函数,也看不到显示代码中哪一行抛出错误的堆栈跟踪。请创建一个并包含错误的完整堆栈跟踪。好的,我确实缩进了代码并粘贴了堆栈跟踪。谢谢