Professional Documents
Culture Documents
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
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)
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)
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
Step 4:
Import library SVM into the coding. This library is used for classification and regression
problems.
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
Output: