import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('Social_Network_Ads.csv')
df.head()
Show hidden output
df.shape
(400, 5)
x = df.iloc[:,[2,3]]
y = df.iloc[:,4]
x.head()
Age EstimatedSalary
0 19 19000
1 35 20000
2 26 43000
3 27 57000
4 19 76000
y.head()
Purchased
0 0
1 0
2 0
3 0
4 0
dtype: int64
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)
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 SVC(kernel linear ,random_state 0)
classifier.fit(x_train,y_train)
▾ SVC i ?
SVC(kernel='linear', random_state=0)
y_pred = classifier.predict(x_test)
from sklearn.metrics import confusion_matrix, accuracy_score, precision_recall_fscore_support
cm = confusion_matrix(y_test,y_pred)
cm
array([[66, 2],
[ 8, 24]])
print("ACCURACY", accuracy_score(y_test,y_pred))
ACCURACY 0.9
from sklearn.metrics import classification_report
class_report=classification_report(y_test, y_pred)
print(class_report)
precision recall f1-score support
0 0.89 0.97 0.93 68
1 0.92 0.75 0.83 32
accuracy 0.90 100
macro avg 0.91 0.86 0.88 100
weighted avg 0.90 0.90 0.90 100
import seaborn as sns
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Predicted 0', 'Predicted 1'],
yticklabels=['Actual 0', 'Actual 1'])
plt.title('Confusion Matrix')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()