SVM Kernel
SVM Kernel
In [ ]: import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [ ]: x1=np.linspace(-5.0,5.0,100)
y1=np.sqrt(5**2-x1**2) # radious=5
y1=np.hstack([y1,-y1])
x1=np.hstack([x1,-x1])
In [ ]: plt.scatter(y,x)
plt.scatter(y1,x1)
In [ ]: df1=pd.DataFrame(np.vstack([y,x]).T,columns=['X1','X2'])
df1['Y']=0
df2=pd.DataFrame(np.vstack([y1,x1]).T,columns=['X1','X2'])
df2['Y']=1
df=df1.append(df2)
df.head(3)
In [ ]: y
In [ ]: y_train
In [ ]: df.head()
Polynomial Kernel
In [ ]: y
In [ ]: X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=0
In [ ]: X_train
In [ ]: import plotly.express as px
fig=px.scatter_3d(df,x='X1',y='X2',z='X1*X2',color='Y')
fig.show()
In [ ]: fig=px.scatter_3d(df,x='X1_square',y='X2_square',z='X1*X2',color='Y')
fig.show()
# Ploynomial kernel will be shown
In [ ]: classifier=SVC(kernel='linear')
classifier.fit(X_train,y_train)
y_pred=classifier.predict(X_test)
accuracy_score(y_test,y_pred)
RBF Kernel
In [ ]: