Python Forum
How to correct the Python programming - Support Vector Machine
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to correct the Python programming - Support Vector Machine
#1



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 -




[Image: Gu1Gr.jpg]







Please help so that I can improve my computing skills









Reply
#2
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
# -------------------------------------------
Reply
#3
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


Reply
#4
(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.
Reply
#5



Why the output is number but not picture ?




Reply
#6
(Apr-04-2019, 07:59 AM)vokoyo Wrote: Please help so that I can improve my computing skills
This 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 equals
This 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').
Reply
#7



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-ar...lib-pyplot





Please see the attached image file




Reply
#8
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reverse math in python, who will find the correct answer? Kakha 11 1,126 Jan-11-2021, 11:59 AM
Last Post: Gribouillis
  vector field plots Larssauerkraut 0 671 Oct-15-2019, 11:15 AM
Last Post: Larssauerkraut
  Class for Vector no_named_nobody 4 1,102 Oct-06-2019, 03:27 PM
Last Post: no_named_nobody
  Verilog HDL Programming to Python Programming? noobcoder 1 1,431 Jul-18-2019, 09:28 PM
Last Post: nilamo
  How to correct the programming for KNN vokoyo 0 1,159 Apr-10-2019, 03:29 AM
Last Post: vokoyo
  Divide a vector Langosmon 1 1,583 May-13-2018, 09:09 AM
Last Post: ThiefOfTime
  New to python... Is this trace table correct? spham9 1 1,342 Mar-17-2018, 03:27 AM
Last Post: Larz60+
  Unit 18 Procedural Programming Python kanwal121 4 2,062 Dec-21-2017, 10:53 PM
Last Post: Terafy
  Unit 18 Procedural Programming Python kanwal121 6 2,276 Dec-17-2017, 07:18 PM
Last Post: Terafy
  python machine learning model, macro precision score? metalray 5 4,761 Sep-28-2017, 09:13 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020