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

时间:2019-06-07 14:54:53

标签: python pandas scikit-learn cluster-analysis typeerror

我正在遵循此article,并使用我自己的数据尝试在出现此错误时根据客户的生命周期支出来绘制客户的订单数量:

我尝试从数据框中删除true / false值并更新相关包

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`

3 个答案:

答案 0 :(得分:0)

尝试在pandas.dataframe上使用numpy技术是一个问题 我使用X=X.value进行了转换,并且有效

答案 1 :(得分:0)

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

答案 2 :(得分:0)

导入数据集后使用X = X.values