0% found this document useful (0 votes)
27 views

10 SupportVectorMachine Demo Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

10 SupportVectorMachine Demo Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import pandas as pd

from sklearn.datasets import load_iris


iris = load_iris()

iris.feature_names

iris.target

iris.target_names

df = pd.DataFrame(iris.data,columns=iris.feature_names)
df.head()

df['target'] = iris.target
df.head()

df[df.target==1].head()

df[df.target==2].head()

df0 = df[:50]
df1 = df[50:100]
df2 = df[100:]

# Commented out IPython magic to ensure Python compatibility.


import matplotlib.pyplot as plt
%matplotlib inline

#Sepal length vs Sepal Width (Setosa vs Versicolor)


plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.scatter(df0['sepal length (cm)'], df0['sepal width (cm)'],color="green",marker='+')
plt.scatter(df1['sepal length (cm)'], df1['sepal width (cm)'],color="blue",marker='.')

from sklearn.model_selection import train_test_split


X = df.drop(['target'], axis='columns')
y = df.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=10)

len(X_train)
len(X_test)

from sklearn.svm import SVC


model = SVC()

model.fit(X_train, y_train)

model.score(X_test, y_test)

model.predict([[4.8,3.0,1.5,0.3]])

#Tune parameters
#1. Regularization (C)
model = SVC(C=10)
model.fit(X_train, y_train)
model.score(X_test,y_test)

#2. Gamma
model = SVC(gamma=100)
model.fit(X_train, y_train)
model.score(X_test,y_test)

#3. Kernel
model = SVC(kernel='linear')
model.fit(X_train, y_train)
model.score(X_test,y_test)

You might also like