SVM
Tue Nov 19 2024 02:37:16 GMT+0000 (Coordinated Universal Time)
Saved by
@wtlab
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
# Step 1: Load the Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Step 2: Select only two classes ('setosa' and 'versicolor')
X = X[y != 2] # Remove 'virginica' class
y = y[y != 2] # Keep only classes 0 and 1
# Step 3: Split the dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Step 4: Train the SVC model
model = SVC(kernel='linear')
model.fit(X_train, y_train)
# Step 5: Make predictions and evaluate
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
content_copyCOPY
Comments