You are viewing a public preview
Sign in to save your progress, do assignments, join discussions and get feedback.
This lesson pulls together the algorithms you have learned — Linear Regression, Logistic Regression, Decision Tree, Random Forest and SVM — into one algorithm-selection reasoning exercise. Do it after you have completed units 2.1 through 2.8.
The task is in the Assignment section below.
By the end of this lesson you can:
Key idea: Choosing an algorithm is a reasoned argument, not a guess: start from the type of target, then weigh explainability, the shape of the relationship, the size of the data and the preprocessing each algorithm needs.
This capstone brings together the five algorithms from units 2.1 to 2.8: Linear Regression, Logistic Regression, decision trees, Random Forest and the Support Vector Machine. The task itself is in the Assignment section of the lesson. These notes give you a checklist for writing a well-argued answer.
Look at the column you need to predict.
| Target | Problem type | Candidate algorithms |
|---|---|---|
| a continuous number (salary, house price) | regression | Linear Regression, Random Forest (RandomForestRegressor) |
| a category (Junior/Senior, approve/reject, spam/normal) | classification | Logistic Regression, decision tree, Random Forest, SVM |
Remember the naming trap from unit 2.2: Logistic Regression is a classification algorithm.
Scenario: a small clinic has 300 patient records with 6 numeric measurements and wants to predict whether a patient needs a follow-up visit (yes/no). Doctors want to see why a patient was flagged.
Reasoning step by step:
max_depth=3). An SVM would also suit the small size, but it is hard to explain to doctors.from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
candidates = {
"baseline": make_pipeline(StandardScaler(), LogisticRegression()),
"tree": DecisionTreeClassifier(max_depth=3, random_state=100),
"forest": RandomForestClassifier(random_state=100),
}
for name, model in candidates.items():
model.fit(x_train, y_train)
print(name, model.score(x_test, y_test))
Expected result: three accuracy scores. If the shallow tree is close to the forest, choose the tree because it can be explained; if the baseline is as good as both, choose the baseline.
Flashcards · Capstone Practice: Choosing an Algorithm for Your Problem
7 wordsA task whose target is a continuous number.
A task whose target is a category or class label.
How easily people can understand why a model made a particular prediction.
A simple first model that gives a reference score for judging more complex models.
Factors used to choose a model: target type, explainability, linearity, data size and preprocessing needs.
A decision tree with limited depth, which is easier to explain and less likely to overfit.
A powerful but less explainable model used to see how much accuracy an explainable model gives up.