Introduction
Support Vector Machine helps us separate different groups in data by drawing a clear line or boundary between them.
from sklearn import svm model = svm.SVC(kernel='linear', C=1.0) model.fit(X_train, y_train) predictions = model.predict(X_test)
model = svm.SVC(kernel='linear')model = svm.SVC(kernel='rbf')model = svm.SVC(kernel='poly', degree=3)
from sklearn import svm from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Load simple flower data iris = load_iris() X = iris.data y = iris.target # Use only two classes for simplicity X = X[y != 2] y = y[y != 2] # Split data into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42) # Create SVM model with linear kernel model = svm.SVC(kernel='linear', C=1.0) # Train the model model.fit(X_train, y_train) # Predict on test data predictions = model.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, predictions) print(f"Predictions: {predictions}") print(f"Accuracy: {accuracy:.2f}")
from sklearn.svm import SVC import numpy as np X = np.array([[0, 0], [1, 1]]) y = [0, 1] model = SVC(kernel='linear') model.fit(X, y) pred = model.predict([[2, 2]]) print(pred)
from sklearn.svm import SVC X = [[1, 2], [2, 3], [3, 4]] y = [0, 1] model = SVC() model.fit(X, y)
Pass the test and this pattern moves unknown