0% found this document useful (0 votes)
4 views

SVM Kernel

The document discusses the concept of kernels in Support Vector Machines (SVM), specifically focusing on polynomial and RBF kernels. It includes code snippets for generating datasets, training SVM classifiers, and visualizing the data in 3D. The document also demonstrates the process of calculating accuracy scores for the classifiers using different kernels.

Uploaded by

officialmail.bs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SVM Kernel

The document discusses the concept of kernels in Support Vector Machines (SVM), specifically focusing on polynomial and RBF kernels. It includes code snippets for generating datasets, training SVM classifiers, and visualizing the data in 3D. The document also demonstrates the process of calculating accuracy scores for the classifiers using different kernels.

Uploaded by

officialmail.bs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2/4/25, 1:02 PM Support Vector machine concept of kernel

In [ ]: import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

In [ ]: x=np.linspace(-5.0,5.0,100) # To generate an array of 100 equally spaced points


y=np.sqrt(10**2-x**2) # equation for a semicircle with radius 10.
y=np.hstack([y,-y])
x=np.hstack([x,-x])

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 [ ]: #Independent and dependent feature


X=df.iloc[:,:2]
y=df.Y

In [ ]: y

In [ ]: # Splitting dataset into training and testing


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

In [ ]: y_train

In [ ]: from sklearn.svm import SVC


classifier=SVC(kernel="linear")
classifier.fit(X_train,y_train)

In [ ]: from sklearn.metrics import accuracy_score


y_pred=classifier.predict(X_test)
accuracy_score(y_test,y_pred)

In [ ]: df.head()

Polynomial Kernel

localhost:8888/doc/tree/Support Vector machine concept of kernel.ipynb? 1/2


2/4/25, 1:02 PM Support Vector machine concept of kernel

In [ ]: # We need to find out component of polynomial kernel


#X1,X2,X1_square,X2_square,X1*X2
df['X1_square']=df['X1']**2
df['X2_square']=df['X2']**2
df['X1*X2']=(df['X1']*df['X2'])
df.head()

In [ ]: # splitting independent and dependent features


X=df[['X1','X2','X1_square','X2_square','X1*X2']]
y=df['Y']

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 [ ]:

localhost:8888/doc/tree/Support Vector machine concept of kernel.ipynb? 2/2

You might also like