Add StrtifiedKFold with shuffling as a default CV method.

ml_pipeline
Primoz 2022-12-09 13:46:13 +01:00
parent ac03b36c0f
commit 6507b053c5
2 changed files with 12 additions and 13 deletions

View File

@ -26,7 +26,7 @@ import pandas as pd
import seaborn as sns import seaborn as sns
from sklearn import linear_model, svm, naive_bayes, neighbors, tree, ensemble from sklearn import linear_model, svm, naive_bayes, neighbors, tree, ensemble
from sklearn.model_selection import LeaveOneGroupOut, cross_validate from sklearn.model_selection import LeaveOneGroupOut, cross_validate, StratifiedKFold
from sklearn.dummy import DummyClassifier from sklearn.dummy import DummyClassifier
from sklearn.impute import SimpleImputer from sklearn.impute import SimpleImputer
@ -47,20 +47,19 @@ import machine_learning.model
# %% [markdown] # %% [markdown]
# ## Set script's parameters # ## Set script's parameters
cv_method_str = 'logo' # logo, halflogo, 5kfold # Cross-validation method (could be regarded as a hyperparameter) cv_method_str = 'logo' # logo, half_logo, 5kfold # Cross-validation method (could be regarded as a hyperparameter)
n_sl = 1 # Number of largest/smallest accuracies (of particular CV) outputs n_sl = 1 # Number of largest/smallest accuracies (of particular CV) outputs
# %% jupyter={"source_hidden": true} # %% jupyter={"source_hidden": true}
model_input = pd.read_csv("../data/stressfulness_event_nonstandardized/input_appraisal_stressfulness_event_mean.csv") model_input = pd.read_csv("../data/intradaily_30_min_all_targets/input_JCQ_job_demand_mean.csv")
# %% jupyter={"source_hidden": true} # %% jupyter={"source_hidden": true}
index_columns = ["local_segment", "local_segment_label", "local_segment_start_datetime", "local_segment_end_datetime"] index_columns = ["local_segment", "local_segment_label", "local_segment_start_datetime", "local_segment_end_datetime"]
model_input.set_index(index_columns, inplace=True) model_input.set_index(index_columns, inplace=True)
model_input['target'].value_counts()
# %% jupyter={"source_hidden": true} # %% jupyter={"source_hidden": true}
# bins = [-10, -1, 1, 10] # bins for z-scored targets bins = [-10, 0, 10] # bins for z-scored targets
bins = [0, 1, 4] # bins for stressfulness (1-4) target # bins = [1, 2.5, 4] # bins for stressfulness (1-4) target
model_input['target'], edges = pd.cut(model_input.target, bins=bins, labels=['low', 'high'], retbins=True, right=True) #['low', 'medium', 'high'] model_input['target'], edges = pd.cut(model_input.target, bins=bins, labels=['low', 'high'], retbins=True, right=True) #['low', 'medium', 'high']
model_input['target'].value_counts(), edges model_input['target'].value_counts(), edges
# model_input = model_input[model_input['target'] != "medium"] # model_input = model_input[model_input['target'] != "medium"]
@ -68,7 +67,7 @@ model_input['target'] = model_input['target'].astype(str).apply(lambda x: 0 if x
model_input['target'].value_counts() model_input['target'].value_counts()
if cv_method_str == 'halflogo': if cv_method_str == 'half_logo':
model_input['pid_index'] = model_input.groupby('pid').cumcount() model_input['pid_index'] = model_input.groupby('pid').cumcount()
model_input['pid_count'] = model_input.groupby('pid')['pid'].transform('count') model_input['pid_count'] = model_input.groupby('pid')['pid'].transform('count')
@ -101,7 +100,7 @@ train_x = pd.concat([numerical_features, categorical_features], axis=1)
train_x.dtypes train_x.dtypes
# %% jupyter={"source_hidden": true} # %% jupyter={"source_hidden": true}
cv_method = None # Defaults to 5 k-folds in cross_validate method cv_method = StratifiedKFold(n_splits=5, shuffle=True) # Defaults to 5 k-folds in cross_validate method
if cv_method_str == 'logo' or cv_method_str == 'half_logo': if cv_method_str == 'logo' or cv_method_str == 'half_logo':
cv_method = LeaveOneGroupOut() cv_method = LeaveOneGroupOut()
cv_method.get_n_splits( cv_method.get_n_splits(

View File

@ -26,7 +26,7 @@ import pandas as pd
import seaborn as sns import seaborn as sns
from scipy import stats from scipy import stats
from sklearn.model_selection import LeaveOneGroupOut, cross_validate from sklearn.model_selection import LeaveOneGroupOut, cross_validate, StratifiedKFold
from sklearn.impute import SimpleImputer from sklearn.impute import SimpleImputer
from sklearn.dummy import DummyClassifier from sklearn.dummy import DummyClassifier
@ -52,8 +52,8 @@ from machine_learning.classification_models import ClassificationModels
# %% [markdown] # %% [markdown]
# ## Set script's parameters # ## Set script's parameters
n_clusters = 5 # Number of clusters (could be regarded as a hyperparameter) n_clusters = 3 # Number of clusters (could be regarded as a hyperparameter)
cv_method_str = 'logo' # logo, halflogo, 5kfold # Cross-validation method (could be regarded as a hyperparameter) cv_method_str = 'half_logo' # logo, half_logo, 5kfold # Cross-validation method (could be regarded as a hyperparameter)
n_sl = 1 # Number of largest/smallest accuracies (of particular CV) outputs n_sl = 1 # Number of largest/smallest accuracies (of particular CV) outputs
# %% jupyter={"source_hidden": true} # %% jupyter={"source_hidden": true}
@ -109,7 +109,7 @@ for k in range(n_clusters):
model_input_subset['target'].value_counts() model_input_subset['target'].value_counts()
if cv_method_str == 'halflogo': if cv_method_str == 'half_logo':
model_input_subset['pid_index'] = model_input_subset.groupby('pid').cumcount() model_input_subset['pid_index'] = model_input_subset.groupby('pid').cumcount()
model_input_subset['pid_count'] = model_input_subset.groupby('pid')['pid'].transform('count') model_input_subset['pid_count'] = model_input_subset.groupby('pid')['pid'].transform('count')
@ -140,7 +140,7 @@ for k in range(n_clusters):
train_x = pd.concat([numerical_features, categorical_features], axis=1) train_x = pd.concat([numerical_features, categorical_features], axis=1)
# Establish cv method # Establish cv method
cv_method = None # Defaults to 5 k-folds in cross_validate method cv_method = StratifiedKFold(n_splits=5, shuffle=True) # Defaults to 5 k-folds in cross_validate method
if cv_method_str == 'logo' or cv_method_str == 'half_logo': if cv_method_str == 'logo' or cv_method_str == 'half_logo':
cv_method = LeaveOneGroupOut() cv_method = LeaveOneGroupOut()
cv_method.get_n_splits( cv_method.get_n_splits(