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

Posted

技术标签:

【中文标题】typeError 帮助,plt.scatter 将我的 .csv 读取为真/假而不是数值【英文标题】:typeError help, plt.scatter reading my .csv as true/false rather than numerical values 【发布时间】:2019-10-23 02:25:37 【问题描述】:

我正在关注此article,当我收到此错误时,我正在使用我自己的数据尝试绘制客户的订单数量与他们的生命周期支出:

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

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`

【问题讨论】:

你应该试试plt.scatter(X[y_means==0][:,0],X[y_means==0][:,1],.... 收到之后,TypeError: '(slice(None, None, None), 0)' is an invalid key 【参考方案1】:

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

【讨论】:

【参考方案2】:
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')

【讨论】:

【参考方案3】:

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

【讨论】:

你能试着解释一下你所说的 x = x.value 是什么意思吗?谢谢

以上是关于typeError 帮助,plt.scatter 将我的 .csv 读取为真/假而不是数值的主要内容,如果未能解决你的问题,请参考以下文章

matplotlib scatter画图报错:TypeError: ufunc ‘sqrt‘ not supported for the input types...rule ‘‘safe‘‘

matplotlib scatter画图报错:TypeError: ufunc ‘sqrt‘ not supported for the input types...rule ‘‘safe‘‘

为啥 matplotlib 需要在 plt.scatter() 之前设置日志比例而不是 plt.plot()?

关于plt.scatter()的使用

plt.scatter()函数

论plt.scatter()画散点图未设置“颜色参数c”却能画出五颜六色点的原因