How to correct the Python programming - Support Vector Machine - 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 Python programming - Support Vector Machine (/thread-17263.html) |
How to correct the Python programming - Support Vector Machine - vokoyo - Apr-04-2019 May I know how to modify my Python programming as refer to the attached image file - # To Get iris dataset from sklearn import datasets # To fit the svm classifier from sklearn import svm import numpy as np import matplotlib.pyplot as plt iris_dataset = datasets.load_iris() def visuvalise_petal_data(): iris = datasets.load_iris() # Only take the first two features X = iris.data[:, 2:3] y = iris.target visuvalise_petal_data() iris = datasets.load_iris() # Only take the Sepal two features X = iris.data[:, 2:3] y = iris.target # SVM regularization parameter # SVC with rbf kernel rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=1).fit(X, y) rbf_svc = svm.SVC(kernel='rbf', gamma=0.01, C=10).fit(X, y) # step size in the mesh h = 0.02 # create a mesh to plot in def plotSVC(title): 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) C = [1, 10] for c in cs: svc = svm.SVC(kernel='rbf', C=1).fit(X, y) svc = svm.SVC(kernel='rbf', C=10).fit(X, y) plotSVC('C=' + str(c)) from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 100, random_state = 0) sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) linear_svm1 = SVC(kernel = 'rbf', C = 1, random_state = 0) linear_svm1.fit(X_train_std, y_train) y_predict1 = linear_svm1.predict(X_test_std) print('Gamma=0.01,C=1') linear_svm2 = SVC(kernel = 'rbf', C = 10, random_state = 0) linear_svm2.fit(X_train_std, y_train) y_predict2 = linear_svm2.predict(X_test_std) print('Gamma=0.01,C=10') svm = SVC(kernel='linear', C=1.0, random_state=0) svm.fit(X_train_std, y_train) plot_decision_regions(X, y, classifier=svm, test_idx=range(105,150)) plt.xlabel('petal length [standardized]') plt.ylabel('petal width [standardized]') plt.legend(loc='upper left') plt.show() The error message is - runfile('C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py', wdir='C:/Users/HSIPL/Desktop') Traceback (most recent call last): File "<ipython-input-85-761bed922ac3>", line 1, in <module> runfile('C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py', wdir='C:/Users/HSIPL/Desktop') File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 44, in <module> plotSVC('C=' + str(c)) File "C:/Users/HSIPL/Desktop/Homework 6 Solution draft.py", line 32, in plotSVC y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 IndexError: index 1 is out of bounds for axis 1 with size 1 Please see the attached file - Please help so that I can improve my computing skills RE: How to correct the Python programming - Support Vector Machine - scidam - Apr-04-2019 I just started to write code, but doesn't test it yet. Hope it helps. from sklearn import datasets from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split import numpy as np # ------------ Prepare your data ------------ iris = datasets.load_iris() X = iris.data[:, 1:3] # this selects first and second columns y = iris.target # ------------------------------------------- # -------- Classifier definitions ----------- clf_schemas = ({'classifier': SVC, 'parameters': {'gamma': 0.01, 'C': 1}, 'name': 'SVC: first case' }, {'classifier': SVC, 'parameters': {'gamma': 0.01, 'C': 10}, 'name': 'SVC: second case' } ) # ------------------------------------------- # --------- Error estimations --------------- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10) for clf_dct in clf_schemas: print("Trying classifier: ", clf_dct['classifier'].__qualname__) print("Parameters used: ", clf_dct['parameters']) print("Starting training with train data...: ", clf_dct['name']) clf_trained = clf_dct['classifier'](**clf_dct['parameters']).fit(X_train, y_train) print("Classifier is trained.") print("Applying it to test data.") y_pred = clf_trained.predict(X_test) score = some_score_function(y_test, y_pred) # insert your own score function here # or you can use accuracy score, e.g. score = (y_test == y_pred).sum() / len(y_test) print("You got the following score estimation: ", score) print("=" * 40) # make a line of equals # ------------------------------------------- RE: How to correct the Python programming - Support Vector Machine - vokoyo - Apr-04-2019 The error message is - runfile('C:/Users/HSIPL/Desktop/hhhhhhhhhhhhh.py', wdir='C:/Users/HSIPL/Desktop') Trying classifier: SVC Parameters used: {'gamma': 0.01, 'C': 1} Starting training with train data...: SVC: first case Classifier is trained. Applying it to test data. Traceback (most recent call last): File "<ipython-input-23-cc309851e44d>", line 1, in <module> runfile('C:/Users/HSIPL/Desktop/hhhhhhhhhhhhh.py', wdir='C:/Users/HSIPL/Desktop') File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/HSIPL/Desktop/hhhhhhhhhhhhh.py", line 39, in <module> score = some_score_function(y_test, y_pred) # insert your own score function here NameError: name 'some_score_function' is not defined RE: How to correct the Python programming - Support Vector Machine - scidam - Apr-04-2019 (Apr-04-2019, 07:59 AM)vokoyo Wrote: Please help so that I can improve my computing skills You need to look at available score functions and choose one of them. Or implement your own. RE: How to correct the Python programming - Support Vector Machine - vokoyo - Apr-04-2019 Why the output is number but not picture ? RE: How to correct the Python programming - Support Vector Machine - scidam - Apr-04-2019 (Apr-04-2019, 07:59 AM)vokoyo Wrote: Please help so that I can improve my computing skillsThis is typical assignment and you need to work on it yourself. If I solve it for you, how you could improve your skill?! Nevertheless, lets consider the following code snippet. It plots classifier's decision regions. # --------- helper function ------------- def plotSVC(X, fitted_clf, ax, title='', resolution=100, cmap=plt.cm.RdBu): """Plot decision regions""" 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.linspace(x_min, x_max, resolution), np.linspace(y_min, y_max, resolution)) Z = fitted_clf.predict(np.c_[xx.ravel(), yy.ravel()]) # Have you read about np.c_ helper? Z = Z.reshape(xx.shape) ax.contourf(xx, yy, Z, cmap=cmap) if title: ax.set_title(title) # -------------------------------------------Further, you need to add somewhere in the loop the following lines of code: # for loop declaration over classification schemas goes here... print("Now we are ready to plot decision regions") fig = plt.figure() ax = fig.add_subplot(111) plotSVC(X, clf_trained, ax, title=clf_dct['name']) plt.show() print("=" * 40) # make a line of equalsThis should produce decision regions for specified classifiers; The only thing, you need to project training data points on current axes, e.g. use ax.scatter ; or ax.plot(..., 'ro') .
RE: How to correct the Python programming - Support Vector Machine - vokoyo - Apr-06-2019 I get the answer but not sure how to put together the four pictures Please come to this web page - https://python-forum.io/Thread-How-to-arrange-the-four-pictures-of-a-matplotlib-pyplot Please see the attached image file RE: How to correct the Python programming - Support Vector Machine - scidam - Apr-06-2019 You need to read about figure and axes in Matplotlib. Pseudocode for putting four pictures in one figure is following: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(221) ax1.scatter(...) ax2 = fig.add_subplot(222) # ax2.scatter or something else ax3 = fig.add_subplot(223) # manipulation with ax3 ax4 = fig.add_subplot(224) # manipulation with ax4 plt.show() |