Support Vector Machine(SVM) | Program6 III Bsc Computer Science| M.S University | Web Academy

Опубликовано: 13 Март 2026
на канале: Web Academy
100
4

Program 6

from sklearn import svm,datasets
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
iris=datasets.load_iris()
Iris=pd.read_csv('e:/Iris.csv')
X=iris.data[:,:2]
y=iris.target
#split the dataset into training (75%) and testing (25%) sets
x_train,x_test,y_train,y_test=train_test_split(X,y,random_state=0,test_size=0.25)
clf=svm.SVC(kernel='linear',C=1).fit(x_train,y_train)
classifier_predictions=clf.predict(x_test)
print("Accuracy:",accuracy_score(y_test,classifier_predictions)*100)
h=0.02
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,h),
np.arange(y_min,y_max,h))
xx.shape
Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
plt.contourf(xx,yy,Z,cmap=plt.cm.coolwarm,alpha=0.3)
plt.scatter(X[:,0],X[:,1],c=y,cmap=plt.cm.coolwarm)
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.xticks(())
plt.yticks(())
plt.title("Support Vector Machine")
plt.show()


What is Support Vector Machine?
Support Vector Machine(SVM) is a supervised machine learning
algorithm used for both classification and regression.

The objective of SVM algorithm is to find a hyper plane in an
N-dimensional space that distinctly classifies the data points.
In Python, an SVM classifier can be developed using the sklearn library.