Apr-04-2019, 07:59 AM
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