I am working on building an SVM model and I’m getting this error:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_23568/2951013395.py in <module>
11
12
---> 13 plt.contourf (xx, yy, predictions, cmap=plt.cm.coolwarm, alpha=0.3)
14 plt.scatter(xtrain[:,:0], xtrain[:,:4000], c=ytrain, cmap=plt.cm.coolwarm)
15 plt.xlabel('Sepal length')
~\Documents\python\lib\site-packages\matplotlib\pyplot.py in contourf(data, *args, **kwargs)
2743 @_copy_docstring_and_deprecators(Axes.contourf)
2744 def contourf(*args, data=None, **kwargs):
-> 2745 __ret = gca().contourf(
2746 *args, **({"data": data} if data is not None else {}),
2747 **kwargs)
~\Documents\python\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1359 def inner(ax, *args, data=None, **kwargs):
1360 if data is None:
-> 1361 return func(ax, *map(sanitize_sequence, args), **kwargs)
1362
1363 bound = new_sig.bind(ax, *args, **kwargs)
~\Documents\python\lib\site-packages\matplotlib\axes\_axes.py in contourf(self, *args, **kwargs)
6432 def contourf(self, *args, **kwargs):
6433 kwargs['filled'] = True
-> 6434 contours = mcontour.QuadContourSet(self, *args, **kwargs)
6435 self._request_autoscale_view()
6436 return contours
~\Documents\python\lib\site-packages\matplotlib\contour.py in __init__(self, ax, levels, filled, linewidths, linestyles, hatches, alpha, origin, extent, cmap, colors, norm, vmin, vmax, extend, antialiased, nchunk, locator, transform, *args, **kwargs)
775 self._transform = transform
776
--> 777 kwargs = self._process_args(*args, **kwargs)
778 self._process_levels()
779
~\Documents\python\lib\site-packages\matplotlib\contour.py in _process_args(self, corner_mask, *args, **kwargs)
1364 self._corner_mask = corner_mask
1365
-> 1366 x, y, z = self._contour_args(args, kwargs)
1367
1368 _mask = ma.getmask(z)
~\Documents\python\lib\site-packages\matplotlib\contour.py in _contour_args(self, args, kwargs)
1422 args = args[1:]
1423 elif Nargs <= 4:
-> 1424 x, y, z = self._check_xyz(args[:3], kwargs)
1425 args = args[3:]
1426 else:
~\Documents\python\lib\site-packages\matplotlib\contour.py in _check_xyz(self, args, kwargs)
1450
1451 if z.ndim != 2:
-> 1452 raise TypeError(f"Input z must be 2D, not {z.ndim}D")
1453 if z.shape[0] < 2 or z.shape[1] < 2:
1454 raise TypeError(f"Input z must be at least a (2, 2) shaped array, "
TypeError: Input z must be 2D, not 1D
this is my code
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn import svm, datasets
import matplotlib.pyplot as plt
IRIS_TRAINING = r'C:\Users\User\PycharmProjects\AI LAB\prof NN code\train.txt'
IRIS_TEST = r'C:\Users\User\PycharmProjects\AI LAB\prof NN code\test.txt'
train_data = np.genfromtxt(IRIS_TRAINING, skip_header=1,
dtype=float, delimiter=';')
test_data = np.genfromtxt(IRIS_TEST, skip_header=1,
dtype=float, delimiter=';')
xtrain = train_data[:,:4000]
ytrain = train_data[:,4001]
xtest = test_data[:,:4000]
ytest = test_data[:,4001]
clf = svm.SVC(kernel='linear', C=1).fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print(accuracy_score(ytest, classifier_predictions)*100)
from sklearn import *
h = 0.02
x_min, x_max = xtrain[:,0].min() - 1, xtrain[:,0].max() + 1
y_min, y_max = xtrain[:,:4000].min() - 1, xtrain[:,:4000].max() + 1
xx, yy = np.meshgrid(np.arange (x_min, x_max, h),
np.arange(y_min, y_max, h))
xx.shape
plt.contourf (xx, yy, predictions, cmap=plt.cm.coolwarm, alpha=0.3)
plt.scatter(xtrain[:,:0], xtrain[:,:4000], c=ytrain, cmap=plt.cm.coolwarm)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title("Linear")
plt.show()