Skip to content
  • SVM, Decision Trees & Random Forests · Data Science

SVM, Decision Trees & Random Forests

LearnViaNotes @learnvianotes· 2 min read

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
ParameterEffect
kernellinear (straight boundary), rbf (curved), poly
CLow = wider margin, tolerates mistakes; high = fits training data tightly (risk of overfitting)
gammaFor 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)']
SVMDecision treeRandom forest
IdeaWidest-margin boundarySequence of feature questionsMany trees voting
InterpretableLowHigh (you can read the tree)Medium (feature importances)
Scaling featuresNeededNot neededNot needed
Overfitting riskTune C, gammaHigh — limit depthLow

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.

Something wrong with this note? Open it to report