Social_Network_Ads data set ကို Support Vector Machine algoritham ဖြင့် classification လုပ်သည့်ကုဒ်များကို ရှင်းပြထားသည်။ data set ထဲတွင် feature များအဖြစ် အသက်(age), လိင်(Gender), လစာ(EstimatedSalary) တို့ပါဝင်သည်။ Label အဖြစ် ဝယ်သည် သို့မဟုတ် မဝယ်ခဲ့ပါ (Purchased)ပါသည်။
pandas, numpy , seaborn နှင့် matplotlib.pyplot တို့ကို import လုပ်သည်။
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the datasets
datasets = pd.read_csv('Social_Network_Ads.csv')
datasets.head(10)
X = datasets.iloc[:, [2,3]].values
Y = datasets.iloc[:, 4].values
Feature တစ်ခုချင်းစီတို့၏ ဆက်စပ်မှု(correlation)ကို စစ်ဆေးသည်။
datasets.corr()
Missing value များ သို့မဟုတ် null value ရှိ မရှိ စစ်ဆေးသည်။
datasets.isnull().sum()
print('Shape of DataFrame(rows,colums):',datasets.shape)
Dataset ထဲတွင် sample 400 ခု ပါဝင်ပြီး feature ၅ ခု ပါဝင်သည်။
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
X_Train[0:4]
X_Test[0:4]
Y_Train[0:4]
Y_Test[0:4]
plt.scatter(X_Train[:,0],X_Train[:,1])
plt.title('Train Data Before Feature Scaling')
plt.grid()
မူလ(original) data set ထဲတွင် feature များ၏ တန်ဖိုးများသည် အလွန်ကွာခြားသည်။ ထို့ကြောင့် feature Scaling လုပ်ရန်လိုအပ်သည်။
Feature Scaling လုပ်ရန် sklearn.preprocessing မှ StandardScaler ကို အသုံးပြုသည်။
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
Feature Scaling လုပ်သည့်အခါ Train data set (X_Train) နှင့် Test data set (X_Test) နှစ်ခုစလုံးကို တူညီသည့် Feature Scaling ပုံစံမျိုးလုပ်ရသည်။ အပေါ်မှ ဂရပ်ပုံသည် Feature Scaling မလုပ်ရသေးသည့် ပုံဖြစ်သည်။ အောက်တွင် Feature Scaling လုပ်ပြီးသည့် ဂရပ်ပုံထုတ်ကြည့်သည်။
X_Train = sc_X.fit_transform(X_Train)
X_Test = sc_X.transform(X_Test)
plt.scatter(X_Train[:,0],X_Train[:,1])
plt.title('Train Data After Feature Scaling')
plt.grid()
Training set များကို ထည့်၍ SVC model တည်ဆောက်သည်။ SVC model ကို SVC classifier ဟုလည်း ခေါ်သည်။ SVC model တွင် 'linear' kernel ကို အသုံးပြုထားသည်။ C တန်ဖိုးသည် default တန်ဖိုးဖြစ်သည 1.0 ကို အသုံးပြုထားသည်။
# Fitting the classifier into the Training set
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_Train, Y_Train)
တည်ဆောက်ပြီးသည့် SVC model/SVC classifier ထဲသို့ Test data set (X_Test) ထည့်၍ ရလဒ်ကို ခန့်မှန်းသည်။
# Predicting the test set results
Y_Pred = classifier.predict(X_Test)
ပြုလုပ်ခဲ့ပြီးသည့် classification ၏ accuracy ကို ခန့်မှန်းရန်, evaluate လုပ်ရန်အတွက် confusion matrix ကို တွက်သည်။
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_Test, Y_Pred)
cm
Visualising the Training set results
from matplotlib.colors import ListedColormap
X_Set, Y_Set = X_Train, Y_Train
X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_Set)):
plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],
c = ListedColormap(('blue', 'yellow'))(i), label = j)
plt.title('Support Vector Machine (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
Visualising the Test set results
from matplotlib.colors import ListedColormap
X_Set, Y_Set = X_Test, Y_Test
X1, X2 = np.meshgrid(np.arange(start = X_Set[:, 0].min() - 1, stop = X_Set[:, 0].max() + 1, step = 0.01),
np.arange(start = X_Set[:, 1].min() - 1, stop = X_Set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_Set)):
plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],
c = ListedColormap(('blue', 'yellow'))(i), label = j)
plt.title('Support Vector Machine (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()