SVC
SVC
SVC
Code
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score , confusion_matrix
import seaborn as sb
import matplotlib.pyplot as plt
iris=load_iris()
data=pd.DataFrame(data=iris.data , columns = iris.feature_names)
x=data[[ 'sepal length (cm)' , 'sepal width (cm)' , 'petal length (cm)' , 'petal width (cm)']]
y=(iris.target==2)
svm_clf = make_pipeline(StandardScaler() , LinearSVC(C=1 , random_state = 42))
x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.3 , random_state=42)
svm_clf.fit(x_train , y_train)
svm_pred_y = svm_clf.predict(x_test)
cm = confusion_matrix(svm_pred_y , y_test)
cm
f,ax = plt.subplots(figsize=(5,5))
sb.heatmap(cm , annot=True , fmt='.0f', linecolor="pink" ,linewidths=0.5 , ax=ax)
plt.xlabel("y_pred")
plt.ylabel("y_test")
myaccuracy = accuracy_score(y_test , svm_pred_y)
print("Accuracy:" ,myaccuracy)
Output
array([[32, 0 ],
[ 0, 13]], dtype=int64)
Accuracy: 1.0
Q. Perform Classification on Churn Modelling Dataset ,using SVC Classifier .
Code
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score , confusion_matrix
import seaborn as sb
import matplotlib.pyplot as plt
data=pd.read_csv('Churn_Modelling.csv')
le = LabelEncoder()
data['Geography'] = le.fit_transform(data['Geography'])
data['Gender'] = le.fit_transform(data['Gender'])
data['Surname'] = le.fit_transform(data['Surname'])
x=data.drop(columns=['Exited','CustomerId','Surname'])
y=data['Exited']
svm_clf = make_pipeline(StandardScaler() , LinearSVC(C=1 , random_state = 42))
x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.3 , random_state=42)
svm_clf.fit(x_train , y_train)
svm_pred_y = svm_clf.predict(x_test)
cm = confusion_matrix(svm_pred_y , y_test)
cm
f,ax = plt.subplots(figsize=(5,5))
sb.heatmap(cm , annot=True , fmt='.0f', linecolor="pink" ,linewidths=0.5 , ax=ax)
plt.xlabel("y_pred")
plt.ylabel("y_test")
myaccuracy = accuracy_score(y_test , svm_pred_y)
print("Accuracy:" ,myaccuracy)
Output
array([[2367, 525],
[ 49, 59]], dtype=int64)
Accuracy: 0.8086666666666666
Q. Perform Classification on Digits Dataset ,using SVC Classifier .
import pandas as pd
from sklearn.datasets import load_digits
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler,LabelEncoder
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score , confusion_matrix
import seaborn as sb
import matplotlib.pyplot as plt
digits=load_digits()
data=pd.DataFrame(data=digits.data , columns = digits.feature_names)
data.head()
x=data
y=digits.target
svm_clf = make_pipeline(StandardScaler() , LinearSVC(C=1 , random_state = 42))
x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.3 , random_state=42)
svm_clf.fit(x_train , y_train)
svm_pred_y = svm_clf.predict(x_test)
cm = confusion_matrix(svm_pred_y , y_test)
cm
f,ax = plt.subplots(figsize=(5,5))
sb.heatmap(cm , annot=True , fmt='.0f', linecolor="pink" ,linewidths=0.5 , ax=ax)
plt.xlabel("y_pred")
plt.ylabel("y_test")
myaccuracy = accuracy_score(y_test , svm_pred_y)
print("Accuracy:" ,myaccuracy)
Output
array([[52, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 46, 0, 0, 1, 1, 0, 0, 0, 0],
[ 0, 0, 47, 1, 0, 1, 0, 0, 0, 0],
[ 0, 0, 0, 50, 0, 0, 0, 0, 0, 1],
[ 1, 0, 0, 0, 59, 0, 0, 0, 1, 0],
[ 0, 0, 0, 1, 0, 61, 1, 0, 1, 0],
[ 0, 0, 0, 0, 0, 1, 52, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 54, 0, 0],
[ 0, 4, 0, 1, 0, 0, 0, 0, 41, 3],
[ 0, 0, 0, 1, 0, 1, 0, 1, 0, 55]], dtype=int64)
Accuracy: 0.9574074074074074