How to correct the NameError: name 'xx' is not defined? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: How to correct the NameError: name 'xx' is not defined? (/thread-17243.html) |
How to correct the NameError: name 'xx' is not defined? - vokoyo - Apr-03-2019 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()Please see the attached files - Please help me so that I can improve my computing skills RE: How to correct the NameError: name 'xx' is not defined? - Larz60+ - Apr-03-2019 Please post complete unaltered error traceback message in BBCode error tags. Thank You RE: How to correct the NameError: name 'xx' is not defined? - ichabod801 - Apr-03-2019 Do us a favor and don't post a ton of images like that. Your problem is that xx (and yy and Z) are defined in the function plotSVC. You can't access them outside the function unless you use a return statement and an assignment to get them out of the function: def one23(): return 1, 2, 3 one, two, three = one23() print(two) RE: How to correct the NameError: name 'xx' is not defined? - vokoyo - Apr-03-2019 The error message - How to write the new coding for correct programming ?
RE: How to correct the NameError: name 'xx' is not defined? - nilamo - Apr-03-2019 The variable isn't defined. You can't use a variable that isn't defined. So to solve the error, you can either: 1) Define the variable, and set it with whatever value makes sense in the context, or 2) Not try to use it anymore. RE: How to correct the NameError: name 'xx' is not defined? - delonbest - Feb-17-2021 In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it. |