1

I'm currently building my SVM model, I'm new to ML and have been building my model just by looking at the tutorial here and there. I have a problem at data visualization. This is the reference that I used https://data-flair.training/blogs/svm-support-vector-machine-tutorial/ This is my data visualization code:

markers = ('x', '.')
colors = ('blue', 'green')
cmap = ListedColormap(colors[:len(np.unique(y_test))])
for idx, cl in enumerate(np.unique(y)):
    plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
           c=cmap(idx), marker=markers[idx], label=cl)

and this is the error message:

TypeError                                 Traceback (most recent call last)

<ipython-input-17-cd1df4df7bea> in <module>()
      4 print(cmap)
      5 for idx, cl in enumerate(np.unique(y)):
----> 6     plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
      7            c=cmap(idx), marker=markers[idx], label=cl)

3 frames

/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in __getitem__(self, key)
   3456             if self.columns.nlevels > 1:
   3457                 return self._getitem_multilevel(key)
-> 3458             indexer = self.columns.get_loc(key)
   3459             if is_integer(indexer):
   3460                 indexer = [indexer]

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3359             casted_key = self._maybe_cast_indexer(key)
   3360             try:
-> 3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
   3363                 raise KeyError(key) from err

/usr/local/lib/python3.7/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

/usr/local/lib/python3.7/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(0         True
1         True
2         True
3         True
4         True
         ...  
12283    False
12284    False
12285    False
12286    False
12287    False
Name: Label, Length: 12288, dtype: bool, 0)' is an invalid key

I'm using datasets that have 2 labels and 8 features. What should I put as the plt.scatter parameter?

hanaganesa
  • 23
  • 5

1 Answers1

0

Hope I understand your question.

In scatter plot you need 2 columns, one as x-axis & other as y-axis, so need to do the same for all your 8 features by selecting 2 pair of columns.

I have changed the scatter parameters as x=X.loc[y == cl, 'col1'], y=X.loc[y == cl, 'col2'] to make it work.

col1 = [1.1,1.2,1.3,1.4,1.4,1.5]
col2 = [0.1,0.2,0.3,0.4,0.4,0.5]
col3 = [2.1,2.2,2.3,2.4,2.4,2.5]
col4 = [3.1,3.2,3.3,3.4,3.4,3.5]
lbl = [0,1,1,1,0,0]

df1 = pd.DataFrame({'col1':col1, 'col2':col2, 'col3':col3, 'col4':col4, 'target':lbl})

X = df1.iloc[:,0:4]
y = df1['target']

markers = ('x', 's')
colors = ('red', 'blue')
cmap = ListedColormap(colors[:len(np.unique(y))])
print(cmap)
for idx, cl in enumerate(np.unique(y)):
    plt.scatter(x=X.loc[y == cl, 'col1'], y=X.loc[y == cl, 'col2'], c=cmap(idx), marker=markers[idx], label=cl)
plt.show()
Vignesh
  • 231
  • 1
  • 6