SVM Algorithm for Python Code

 

For every Machine Learning Algorithm following are the major steps to build the Model:

1. Collection of data         

2. Divide data into training and testing          

3. Build the Network

5. Performance Analysis and Accuracy calculations

Step1:Load dataset

#Import scikit-learn dataset library

from sklearn import datasets

 Ex. cancer = datasets.load_breast_cancer()

 Step 2:Split dataset into a training set and test set

Import train_test_split function

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.3,random_state=109) # 70% training and 30% test

Step3:Create a svm Classifier

 Import svm model

from sklearn import svm

clf = svm.SVC(kernel='linear') # Linear Kernel

clf = svm.SVC(kernel='linear') # Linear Kernel 

Step4:Train the model using the training sets

clf.fit(X_train, y_train) 

Step5:Predict the response for test dataset

y_pred = clf.predict(X_test) 

Step6: Model Accuracy: how often is the classifier correct?

Import scikit-learn metrics module for accuracy calculation

from sklearn import metrics 

# Model Accuracy: how often is the classifier correct?

print("Accuracy:",metrics.accuracy_score(y_test, y_pred))print("Accuracy:",metrics.accuracy_score(y_test, y_pred))


Comments

Post a Comment

Popular posts from this blog

WHY DEEP NEURAL NETWORK: Difference between neural network and Deep neural network

Training and Testing Phase of Neural Network