13  Fitting a Model

13.1 Fitting a model

13.2 Type of (supervised) models

13.2.1 Baseline

null_model() |> set_mode("classification")
null_model() |> set_mode("regression")
from sklearn.dummy import DummyClassifier, DummyRegressor

DummyClassifier(strategy="most_common")
DummyRegressor(strategy="mean")

13.2.2 Linear Regression

Regression
linear_reg()
Classification
logistic_reg()
Regression
from sklearn.linear_model import LinearRegression

LinearRegression()
Classification
from sklearn.linear_model import LogisticRegression

LogisticRegression(penalty="none")

13.2.3 Regularized Regression

Regression
linear_reg(penalty = 0.1, mixture = 0) |> set_mode("regression")
linear_reg(penalty = 0.1, mixture = 1) |> set_mode("regression")
linear_reg(penalty = 0.1, mixture = 0.5) |> set_mode("regression")
Classification
linear_reg(penalty = 0.1, mixture = 0) |> set_mode("classification")
linear_reg(penalty = 0.1, mixture = 1) |> set_mode("classification")
linear_reg(penalty = 0.1, mixture = 0.5) |> set_mode("classification")

Parameters alpha (regularization strength) and C are linked by the following relationship: \(\alpha = \frac{1}{2C}\).

Regression
from sklearn.linear_model import Ridge, Lasso, ElasticNet

Ridge(alpha=0.1) # ElasticNet(alpha=0.1, l1_ratio=0)
Lasso(alpha=0.1) # ElasticNet(alpha=0.1, l1_ratio=1)
ElasticNet(alpha=0.1, l1_ratio=0.5)
Classification
from sklearn.linear_model import RidgeClassifier, LogisticRegression

RidgeClassifier(alpha=0.1) # LogisticRegression(penalty="l2", C=5)
LogisticRegression(penalty="l1", C=5)
LogisticRegression(penalty="elasticnet", C=5, l1_ratio=0.5)

13.2.4 K-Nearest Neighbors

nearest_neighbor(neighbors = 5, dist_power = 2) |> set_mode("regression")
nearest_neighbor(neighbors = 5, dist_power = 2) |> set_mode("classification")
from sklearn.neighbors import KNeighborsRegressor, KNeighborsClassifier

KNeighborsRegressor(n_neighbors=5, p=2)
KNeighborsClassifier(n_neighbors=5, p=2)

13.2.5 Support Vector Machine

Regression
svm_linear(cost = 5, margin = 0.01) |> set_mode("regression")
svm_poly(cost = 5, degree = 3, margin = 0.01) |> set_mode("regression")
svm_rbf(cost = 5, rbf_sigma = 20, margin = 0.01) |> set_mode("regression")
Classification
svm_linear(cost = 5) |> set_mode("classification")
svm_poly(cost = 5, degree = 3) |> set_mode("classification")
svm_rbf(cost = 5, rbf_sigma = 20) |> set_mode("classification")
Regression
from sklearn.svm import LinearSVR, SVR

LinearSVR(C=5, epsilon=0.01)
SVR(kernel="poly", C=5, degree=3, epsilon=0.01)
SVR(kernel="rbf", C=5, gamma=0.2, epsilon=0.01)
Classification
from sklearn.svm import LinearSVC, SVC

LinearSVC(C=5)
SVC(kernel="poly", C=5, degree=3)
SVC(kernel="rbf", C=5, gamma=0.2)

13.2.6 Classification and Regression Trees

decision_tree() |> set_mode("regression")
decision_tree() |> set_mode("classification")
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier

DecisionTreeRegressor()
DecisionTreeClassifier()

13.2.7 Random Forest

rand_forest() |> set_mode("regression")
rand_forest() |> set_mode("classification")
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier

RandomForestRegressor()
RandomForestClassifier()

13.2.8 Bagging Trees

bag_tree() |> set_mode("regression")
bag_tree() |> set_mode("classification")
from sklearn.ensemble import BaggingRegressor, BaggingClassifier
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier

BaggingRegressor(base_estimator=DecisionTreeRegressor())
BaggingClassifier(base_estimator=DecisionTreeClassifier())

13.2.9 Gradient Boosting Machine

boost_tree() |> set_mode("regression")
boost_tree() |> set_mode("classification")
from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier

GradientBoostingRegressor()
GradientBoostingClassifier()