typeError 帮助,plt.scatter 将我的 .csv 读取为真/假而不是数值

问题描述 投票:0回答:4

我正在关注这篇文章,当我收到此错误时,使用我自己的数据尝试绘制客户订单数量与他们一生的花费:

我尝试从我的数据框中删除真/假值并更新相关包

TypeError                                 Traceback (most recent call last)
<ipython-input-74-221045cec1a1> in <module>
      3 y_means = km4.fit_predict(X)
      4 #Visualizing the clusters for k=4
----> 5 plt.scatter(X[y_means==0,0],X[y_means==0,1],s=50, c='purple',label='Cluster1')
      6 plt.scatter(X[y_means==1,0],X[y_means==1,1],s=50, c='blue',label='Cluster2')
      7 plt.scatter(X[y_means==2,0],X[y_means==2,1],s=50, c='green',label='Cluster3')

/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2655                                  'backfill or nearest lookups')
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:
   2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(array([ True,  True,  True, ...,  True,  True,  True]), 0)' is an invalid key```



Update: After following the advice in the comments and changing my plt.scatter to `plt.scatter(X[y_means==0][:,0],X[y_means==0][:,1],`

I receive the error `TypeError: '(slice(None, None, None), 0)' is an invalid key`
python pandas scikit-learn cluster-analysis typeerror
4个回答
5
投票

尝试在 pandas.dataframe 上使用 numpy 技术时出现问题 我使用

X=X.value
转换它并且它有效


3
投票
using Your error code here 

y_means = km4.fit_predict(X)
# solution, convert the dataframe to a np.array
#Visualizing the clusters for k=4
X = np.array(X) #that all
plt.scatter(X[y_means==0,0],X[y_means==0,1],s=50, c='purple',label='Cluster1')
plt.scatter(X[y_means==1,0],X[y_means==1,1],s=50, c='blue',label='Cluster2')
plt.scatter(X[y_means==2,0],X[y_means==2,1],s=50, c='green',label='Cluster3')

0
投票

导入数据集后使用

X = X.values


0
投票

将 matplotlib.pyplot 导入为 plt plt.plot(x)

© www.soinside.com 2019 - 2024. All rights reserved.