In this post, you will get an access to Python code example for building a machine learning classification model using SVM (Support Vector Machine) classifier algorithm. We will work with Python Sklearn package for building the model.
The following steps will be covered for training the model using SVM:
- Load the data
- Create training and test split
- Perform feature scaling
- Instantiate an SVC classifier
- Fit the model
- Measure the model performance
First and foremost we will load appropriate Sklearn modules and classes.
# Basic packages import numpy as np import pandas as pd import matplotlib.pyplot as plt # Sklearn modules & classes from sklearn.linear_model import Perceptron, LogisticRegression from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn import datasets from sklearn import metrics
Lets get started with loading the data set and creating the training and test split from the data set. Pay attention to the stratification aspect used when creating the training and test split. The train_test_split class of sklearn.model_selection is used for creating training and test split.
# Load the data set; In this example, the breast cancer dataset is loaded. bc = datasets.load_breast_cancer() X = bc.data y = bc.target # Create training and test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y)
Next step is to perform feature scaling. The reason for doing feature scaling is to make sure that data for different features are in the same range. The StandardScaler class of sklearn.preprocessing is used.
sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test)
Next step is to instantiate a SVC (Support Vector Classifier) and fit the model. The SVC class of sklearn.svm module is used.
# Instantiate the Support Vector Classifier (SVC) svc = SVC(C=1.0, random_state=1, kernel='linear') # Fit the model svc.fit(X_train_std, y_train)
Finally, it is time to measure the model performance. Here is the code for doing the same:
# Make the predictions y_predict = svc.predict(X_test_std) # Measure the performance print("Accuracy score %.3f" %metrics.accuracy_score(y_test, y_predict))
The performance of the model will turn out to be 0.953.
- Spacy Tokenization Python Example - December 4, 2020
- Top 10 Types of Analytics Projects – Examples - December 3, 2020
- Different Success / Evaluation Metrics for AI / ML Products - December 2, 2020