data science/machine learning

SVM(Support Vector Machines) Classifier

꼰대코더 2025. 4. 2. 22:29

별과 삼각형을 분류하기 위해서 가장 넓은 하늘색 공간(maximum marginal hyperplane(MMH))을 찾는 알고리즘이다.

이 공간을 계산하기 위해 인접한 Support Vectors 라 불리는 데이터 포인트를 이용하게 된다.

 

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import svm
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd
import seaborn as sns

iris = sns.load_dataset('iris')
X_iris = iris.drop(columns=['species'], axis=1)
y_iris = iris['species']

# Split dataset into random train and test subsets:
Xtrain, Xtest, ytrain, ytest = train_test_split(X_iris, y_iris, random_state=1)

# Standardize features by removing mean and scaling to unit variance:
scaler = StandardScaler()
scaler.fit(Xtrain)
Xtrain = scaler.transform(Xtrain)
Xtest = scaler.transform(Xtest)

classifier = svm.SVC(kernel='linear')
classifier.fit(Xtrain, ytrain)

# Predict y data with classifier:
y_predict = classifier.predict(Xtest)

# Print results:
print(confusion_matrix(ytest, y_predict))
print(classification_report(ytest, y_predict))