This program trains an SVM to separate two types of iris flowers using their features. It then tests the model and shows predictions and accuracy.
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}")