What Machine learning experiment usualy do?
How to conduct a Machine learning experiment:
How to make fair comparison among models?
What tool-kit is avaliable?
method | composition | focus |
---|---|---|
Hyper-parameter optimizers | a warper of estimator, data splitter, parameter grid | search the best set of hyper-parameters of a model |
Model validator | a warper of estimator and data splitter | validate the models |
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets
digits = datasets.load_digits()
X_digits = digits.data
y_digits = digits.target
from sklearn.decomposition import PCA
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
# scaling
# dimentionality reduction
pca = PCA()
# Learnign algorithm
logistic = SGDClassifier(loss='log', penalty='l2', early_stopping=True,
max_iter=10000, tol=1e-5, random_state=0)
# pipeline
pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)])
print(pipe)
source: https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html
optimizer().fit()
:
X,y
: dataparam_grid
: parameter gridestimator
: class with fit functionscoring
: evaluation metriccv
: Splitter (default 5-fold cross validation)Hyper-parameter optimizers | description |
---|---|
model_selection.GridSearchCV() | Exhaustive search over specified parameter values for an estimator. |
model_selection.HalvingGridSearchCV() | Search over specified parameter values with successive halving. |
model_selection.ParameterGrid() | Grid of parameters with a discrete number of values for each. |
model_selection.ParameterSampler() | Generator on parameters sampled from given distributions. |
model_selection.RandomizedSearchCV() | Randomized search on hyper parameters. |
model_selection.HalvingRandomSearchCV() | Randomized search on hyper parameters. |
%time
# Parameters of pipelines can be set using ‘__’ separated parameter names:
param_grid = {
'pca__n_components': [5, 20, 30, 40, 50, 64],
'logistic__alpha': np.logspace(-4, 4, 5),
}
cv=5
search = GridSearchCV(pipe, param_grid, iid=False, cv=cv, scoring='recall_macro', n_jobs=cv)
search.fit(X_digits, y_digits)
print("Best parameter (CV score=%0.3f):" % search.best_score_)
print(search.best_params_)
d = {'mean_test_score': search.cv_results_.get('mean_test_score'),
'std_test_score': search.cv_results_.get('std_test_score'),
'rank_test_score': search.cv_results_.get('rank_test_score')}
pd.DataFrame(data= search.cv_results_.get('params')).join(pd.DataFrame(data= d))
validator()
:
X,y
: dataestimator
: class with fit functionscoring
: evaluation metriccv
: Splitter (default 5-fold cross validation)Model validator | validating on |
---|---|
sklearn.model_selection.cross_val_score() |
single score |
sklearn.model_selection.cross_validate() |
by score |
sklearn.model_selection.validation_curve() |
hyper-parameter by score |
sklearn.model_selection.learning_curve() |
trained sample by score |
%time
from sklearn.model_selection import cross_validate
from sklearn.metrics import recall_score
scoring = ['precision_macro', 'recall_macro']
cv=5
scores = cross_validate(pipe, X_digits, y_digits, scoring=scoring, cv=cv, return_estimator=True, n_jobs=cv)
[print(x,":",
np.mean(scores.get('test_'+ x)).round(4),
'+/-',np.std(scores.get('test_'+ x)).round(4))
for x in scoring]
Two categories of the splitting for non TimeSeries data:
Two categories of the Sampling:
One categories TimeSeries data
Less-frequently-used Splitter Classes | Description |
---|---|
model_selection.LeaveOneGroupOut() | Leave One Group Out cross-validator |
model_selection.LeavePGroupsOut() | Leave P Group(s) Out cross-validator |
model_selection.LeaveOneOut() | Leave-One-Out cross-validator |
model_selection.LeavePOut() | Leave-P-Out cross-validator |
model_selection.PredefinedSplit() | Predefined split cross-validator |
model_selection.RepeatedKFold() | Repeated K-Fold cross validator. |
model_selection.RepeatedStratifiedKFold() | Repeated Stratified K-Fold cross validator. |
method | description |
---|---|
linear_model.ElasticNetCV([l1_ratio, eps, …]) |
Elastic Net model with iterative fitting along a regularization path |
linear_model.LarsCV([fit_intercept, …]) |
Cross-validated Least Angle Regression model |
linear_model.LassoCV([eps, n_alphas, …]) |
Lasso linear model with iterative fitting along a regularization path |
linear_model.LassoLarsCV([fit_intercept, …]) |
Cross-validated Lasso, using the LARS algorithm |
linear_model.LogisticRegressionCV([Cs, …]) |
Logistic Regression CV (aka logit, MaxEnt) classifier. |
linear_model.MultiTaskElasticNetCV([…]) |
Multi-task L1/L2 ElasticNet with built-in cross-validation. |
linear_model.MultiTaskLassoCV([eps, …]) |
Multi-task L1/L2 Lasso with built-in cross-validation. |
linear_model.OrthogonalMatchingPursuitCV([…]) |
Cross-validated Orthogonal Matching Pursuit model (OMP) |
linear_model.RidgeCV([alphas, …]) |
Ridge regression with built-in cross-validation. |
linear_model.RidgeClassifierCV([alphas, …]) |
Ridge classifier with built-in cross-validation. |
Scoring |
Function |
Comment |
---|---|---|
‘accuracy’ |
||
‘balanced_accuracy’ |
||
‘top_k_accuracy’ |
||
‘average_precision’ |
||
‘neg_brier_score’ |
||
‘f1’ |
for binary targets |
|
‘f1_micro’ |
micro-averaged |
|
‘f1_macro’ |
macro-averaged |
|
‘f1_weighted’ |
weighted average |
|
‘f1_samples’ |
by multilabel sample |
|
‘neg_log_loss’ |
requires |
|
‘precision’ etc. |
suffixes apply as with ‘f1’ |
|
‘recall’ etc. |
suffixes apply as with ‘f1’ |
|
‘jaccard’ etc. |
suffixes apply as with ‘f1’ |
|
‘roc_auc’ |
||
‘roc_auc_ovr’ |
||
‘roc_auc_ovo’ |
||
‘roc_auc_ovr_weighted’ |
||
‘roc_auc_ovo_weighted’ |
Scoring |
Function |
Comment |
---|---|---|
‘explained_variance’ |
||
‘max_error’ |
||
‘neg_mean_absolute_error’ |
||
‘neg_mean_squared_error’ |
||
‘neg_root_mean_squared_error’ |
||
‘neg_mean_squared_log_error’ |
||
‘neg_median_absolute_error’ |
||
‘r2’ |
||
‘neg_mean_poisson_deviance’ |
||
‘neg_mean_gamma_deviance’ |
||
‘neg_mean_absolute_percentage_error’ |
Scoring |
Function |
Comment |
---|---|---|
‘adjusted_mutual_info_score’ |
||
‘adjusted_rand_score’ |
||
‘completeness_score’ |
||
‘fowlkes_mallows_score’ |
||
‘homogeneity_score’ |
||
‘mutual_info_score’ |
||
‘normalized_mutual_info_score’ |
||
‘rand_score’ |
||
‘v_measure_score’ |