Welcome to the world of Machine Learning !

SVM Python Code Implementation

In this code we have used iris dataset of sklearn library. You can copy the code and execute it in juypter or PyCharm.

from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# Create an SVM classifier with a linear kernel
clf = SVC(kernel='linear')
clf.fit(X_train, y_train)
# Use the trained classifier to make predictions on the test set
y_pred = clf.predict(X_test)
# Evaluate the accuracy of the classifier
acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)

The output of the above code:

Accuracy: 1.0
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1 0 0 0 2 1 1 0 0]

Advertisement

Advertisement