Three classic classifiers on the Iris dataset (150 flowers, 4 measurements, 3 species) — how they decide, and what each is good at.
Support Vector Machine (SVM)
Python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
iris = datasets.load_iris()
X = iris.data[:, :2] # only 2 features (sepal length & width) so it can be plotted in 2-D
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
model = SVC(kernel="rbf", C=1.0, gamma="auto").fit(X_train, y_train)
print(f"Accuracy: {accuracy_score(y_test, model.predict(X_test)):.2f}") # Accuracy: 0.80
Parameter
Effect
kernel
linear (straight boundary), rbf (curved), poly
C
Low = wider margin, tolerates mistakes; high = fits training data tightly (risk of overfitting)
gamma
For rbf: how far one sample's influence reaches — high gamma makes wiggly boundaries
Decision Tree
Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.metrics import accuracy_score
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)
model = DecisionTreeClassifier(criterion="entropy", max_depth=3, random_state=0).fit(X_train, y_train)
print(f"Accuracy: {accuracy_score(y_test, model.predict(X_test)):.2f}") # Accuracy: 0.98
print(export_text(model, feature_names=iris.feature_names).splitlines()[0]) # |--- petal width (cm) <= 0.75
Random Forest
Python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)
model = RandomForestClassifier(n_estimators=100, max_depth=3, random_state=0).fit(X_train, y_train)
print(f"Accuracy: {accuracy_score(y_test, model.predict(X_test)):.2f}") # Accuracy: 0.98
ranked = sorted(zip(model.feature_importances_, iris.feature_names), reverse=True)
print([name for _, name in ranked[:2]]) # ['petal length (cm)', 'petal width (cm)']
SVM
Decision tree
Random forest
Idea
Widest-margin boundary
Sequence of feature questions
Many trees voting
Interpretable
Low
High (you can read the tree)
Medium (feature importances)
Scaling features
Needed
Not needed
Not needed
Overfitting risk
Tune C, gamma
High — limit depth
Low
Free for students and teachers
Write notes like this one
Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.