Error while building an SVM model

Pearl_20: <p>I am working on building an SVM model and I’m getting this error:</p>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_23568/2951013395.py in &lt;module&gt;
     11 
     12 
---&gt; 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):
-&gt; 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:
-&gt; 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
-&gt; 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 
--&gt; 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 
-&gt; 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 &lt;= 4:
-&gt; 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:
-&gt; 1452             raise TypeError(f"Input z must be 2D, not {z.ndim}D")
   1453         if z.shape[0] &lt; 2 or z.shape[1] &lt; 2:
   1454             raise TypeError(f"Input z must be at least a (2, 2) shaped array, "

TypeError: Input z must be 2D, not 1D
</code></pre>
<p>this is my code</p>
<pre><code class="lang-auto">
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()
</code></pre>

ptrblck: <p>The error is raised by <code>matplotlib</code> as <code>plt.contourf</code> fails with a shape mismatch.<br>
The <a href="https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.contourf.html">docs</a> for this method explain the expected input shapes.<br>
In your case it seems <code>z</code> is failing as it should have 2 dimensions:</p>
<blockquote>
<p>Z(M, N) array-like<br>
The height values over which the contour is drawn.</p>
</blockquote>