Skip to content

Instantly share code, notes, and snippets.

@bha159
Created February 5, 2018 01:58
Show Gist options
  • Save bha159/1b008ac2d3d20732f30f55a159c702e8 to your computer and use it in GitHub Desktop.
Save bha159/1b008ac2d3d20732f30f55a159c702e8 to your computer and use it in GitHub Desktop.
A python program to test SVM classifier with linear kernel and test the accuracy.
#Importing modules
from sklearn import svm # To fit the svm classifier
from sklearn import model_selection
import numpy as np
import pandas
#Import iris data to model Svm classifier
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)
#Check the data
print(dataset.head(20))
#Train
#Dividing dataset to train and test
array = dataset.values #Array contains all the data from dataset
X = array[:,0:4] #X contains data values of all four features
Y = array[:,4] #Y contains data values of all classes corresponding to X
validation_size = 0.25 #Dividing dataset into 75% and 25% for training and testing
seed = 7
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)
clf=svm.SVC(kernel='linear')
clf.fit(X_train,Y_train)
print("Accuracy is",clf.score(X_validation,Y_validation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment