# plotting linear SVM - Python

TAGS :
Viewed: 3 - Published at: a few seconds ago

#### [ plotting linear SVM ]

I tried following the example here but i am having trouble applying it when i have 16 features. lin_svc is trained with those 16 features (i deleted the line to re-train it again from the example). it works and i tried it and also extracted .coef_before.

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm

#features is an array of 16
#lin_svc variable is available
#train is a pandas DF

X = train[features].as_matrix()
y = train.outcome

h = .02 # step size in the mesh

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))

# title for the plots
titles = ['SVC with linear kernel']

for i, clf in enumerate([lin_svc]):
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
plt.subplot(2, 2, i + 1)

Z = clf.predict(X)

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title(titles[i])

plt.show()

The error i am getting is:

ValueError                                Traceback (most recent call last)
&lt;ipython-input-8-d52ca252fc3a&gt; in &lt;module&gt;()
24
25     # Put the result into a color plot
---&gt; 26     Z = Z.reshape(xx.shape)
27     plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
28

ValueError: total size of new array must be unchanged