Search code examples
pythonpandasscikit-learncluster-analysistypeerror

typeError help, plt.scatter reading my .csv as true/false rather than numerical values


I'm following this article, using my own data trying to plot a customers # of orders against their lifetime spend when I get this error:

I've tried removing true/false values from my dataframe and updating relevant packages

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`

Solution

  • It was an issue with trying to use a numpy technique on a pandas.dataframe I converted it using X=X.value and it worked