You are on page 1of 4

Section Development of SVM model using function train test split

Step 1:
Open anaconda navigator and launch Jupyter Notebook
Save the iris.csv dataset in the same folder as your coding
Create a new phyton file and start doing the coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
iris_data = pd.read_csv('C:/Users/ariny/OneDrive/Documents/iris.csv')
iris_data

Step 3:
Split the dataset into training and testing datasets using the function train_test_split.
This function is used to randomly select training data and testing data.
Import function train_test_split using library sklearn.model_selection

from sklearn.model_selection import train_test_split


X = iris_data.drop('species', axis=1)
y = iris_data['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

Step 4:
Import library SVM into the coding. This library is used for classification and regression
problems.
Here, you need to have three results coming from the four kernels.
Train the model for each kernel one by one
Test the model for each kernel and calculate the accuracy score value, one by one
Save the accuracy score into an Excel file for comparison purposes (refer Excel file attached)

from sklearn.svm import SVC

svm_linear = SVC(kernel='linear')
svm_rbf = SVC(kernel='rbf')
svm_poly = SVC(kernel='poly')
svm_sigmoid = SVC(kernel='sigmoid')
svm_linear.fit(X_train, y_train)
svm_rbf.fit(X_train, y_train)
svm_poly.fit(X_train, y_train)
svm_sigmoid.fit(X_train, y_train)

from sklearn.metrics import accuracy_score


pred_linear = svm_linear.predict(X_test)
pred_rbf = svm_rbf.predict(X_test)
pred_poly = svm_poly.predict(X_test)
pred_sigmoid = svm_sigmoid.predict(X_test)

print("Linear kernel:")
print(accuracy_score(y_test, pred_linear))
print("RBF kernel:")
print(accuracy_score(y_test, pred_rbf))
print("Polynomial kernel:")
print(accuracy_score(y_test, pred_poly))
print("Sigmoid kernel:")
print(accuracy_score(y_test, pred_sigmoid))

Output:
Section Development of SVM model without using function train test split

Step 1:
Open anaconda navigator and launch Jupyter Notebook
In the iris.csv put =RANDBETWEEN(x,y) with x and y is 1 until the last number
Sort and split into two for training data and testing data
Save the datasets in the same folder as your coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
iris_train = pd.read_csv('C:/Users/ariny/OneDrive/Documents/iris_train.csv')
iris_test = pd.read_csv('C:/Users/ariny/OneDrive/Documents/iris_test.csv')

Step 3:
Divide the datasets into input and target variables

input_training = iris_train.drop("species", axis=1)


target_training = iris_train["species"]
input_testing = iris_test.drop("species", axis=1)
target_testing = iris_test["species"]

Step 4:
Import library SVM into the coding. This library is used for classification and regression
problems.

from sklearn.svm import SVC


svm_linear = SVC(kernel='linear')
svm_rbf = SVC(kernel='rbf')
svm_poly = SVC(kernel='poly')
svm_sigmoid = SVC(kernel='sigmoid')

svm_linear.fit(input_training, target_training)
svm_rbf.fit(input_training, target_training)
svm_poly.fit(input_training, target_training)
svm_sigmoid.fit(input_training, target_training)

pred_linear = svm_linear.predict(input_testing)
pred_rbf = svm_rbf.predict(input_testing)
pred_poly = svm_poly.predict(input_testing)
pred_sigmoid = svm_sigmoid.predict(input_testing)
Step 5:
Calculate and print the accuracy score using the accuracy_score function

from sklearn.metrics import accuracy_score


print("Linear kernel:")
print(accuracy_score(target_testing, pred_linear))
print("RBF kernel:")
print(accuracy_score(target_testing, pred_rbf))
print("Polynomial kernel:")
print(accuracy_score(target_testing, pred_poly))
print("Sigmoid kernel:")
print(accuracy_score(target_testing, pred_sigmoid))

Output:

You might also like