Supprt Vector Machine (SVM)
A tutorial On how to use SVM.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Social_Network_Ads.csv')
dataset
dataset.isna().sum()
dataset.info()
dataset.drop('User ID', axis=1, inplace=True)
dataset.head()
X = dataset.drop('Purchased', axis=1)
X.head()
y = dataset['Purchased']
y.head()
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
categorical_feature = ["Gender"]
one_hot = OneHotEncoder()
transformer = ColumnTransformer([("one_hot",
one_hot,
categorical_feature)],
remainder="passthrough")
transformed_X = transformer.fit_transform(X)
pd.DataFrame(transformed_X).head()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(transformed_X, y, test_size = 0.25, random_state = 2509)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)
classifier.score(X_test,y_test)
y_pred = classifier.predict(X_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)