May I know how to correct the NameError: name 'xx' is not defined ?

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:,[2,3]]
y = iris.target

def plotSVC(title):
# 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
    h = (x_max / x_min)/100
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
             np.arange(y_min, y_max, h))
    plt.subplot(1, 1, 1)
    Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

cs = [0.1, 1, 10, 100]
for c in cs:
    svc = svm.SVC(kernel='rbf', C=c).fit(X, y)
    plotSVC('C=' + str(c))
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('petal length')
plt.ylabel('petal width')
plt.xlim(xx.min(), xx.max())
plt.show()

The error message is - NameError: name 'xx' is not defined

Please see the attached files -

SVM.jpg

Kernel_SVM.jpg

**

Please help me so that I can improve my computing skills

**

I'm far from a python pro but it seems to me that xx is local to plotSVC and goes out of scope once that function exits.

I suggest you try to keep all module code contiguous. You have defned plotSVC in the middle of your mainline code. This is a very bad habit to get into. Keep all the mainline code as one contiguous block.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.