SVM (Polynomial Kernel)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CSE 3008 -L16+L17

NAME: G. LAKSHMI LASYA


REG NO: 22BCE9602

PROGRAM:
SVM(Polynomial kernel)

CODE:
import numpy as np

import matplotlib.pyplot as plt

fromsklearn import datasets

fromsklearn.model_selectionimport train_test_split

fromsklearn.svmimport SVC

fromsklearn.metrics import classification_report, confusion_matrix

#Load a dataset (Iris)

iris = datasets.load_iris()

X =iris.data

y= iris.target

#Useonly the first two features for visualization

X =X[:, :2]

#Split thedataset into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

#Create an SVMmodel with a polynomial kernel

svm_model = SVC(kernel='poly', degree=3, coef0=1, C=1)


#Fit the model

svm_model.fit(X_train, y_train)

#Makepredictions

y_pred = svm_model.predict(X_test)

#Evaluatethemodel

print("ConfusionMatrix:")

print(confusion_matrix(y_test, y_pred))

print("\nClassification Report:")

print(classification_report(y_test, y_pred))

#Function to visualize decision boundaries

def plot_decision_boundaries(X, y, model):

x_min, x_max =X[:, 0].min() -1, X[:, 0].max() +1

y_min, y_max =X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy= np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1))

Z =model.predict(np.c_[xx.ravel(), yy.ravel()])

Z =Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, alpha=0.8)

plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o')

plt.title("SVMwithPolynomialKernel")

plt.xlabel("Feature1")

plt.ylabel("Feature 2")

plt.show()

#Plot decision boundaries using the trainingset

plot_decision_boundaries(X_train, y_train, svm_model)


OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy