Unhide jupyter code cells and outputs.
parent
61d786b2ca
commit
b0b9edccc4
|
@ -13,7 +13,7 @@
|
|||
# name: straw2analysis
|
||||
# ---
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
# %matplotlib inline
|
||||
import os
|
||||
import sys
|
||||
|
@ -41,20 +41,23 @@ if nb_dir not in sys.path:
|
|||
|
||||
# %% [markdown]
|
||||
# ## Set script's parameters
|
||||
#
|
||||
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}}
|
||||
cv_method_str = '5kfold' # logo, half_logo, 5kfold # Cross-validation method (could be regarded as a hyperparameter)
|
||||
n_sl = 3 # Number of largest/smallest accuracies (of particular CV) outputs
|
||||
undersampling = True # (bool) If True this will train and test data on balanced dataset (using undersampling method)
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
model_input = pd.read_csv("../data/stressfulness_event_with_target_0_ver2/input_appraisal_stressfulness_event_mean.csv")
|
||||
# model_input = model_input[model_input.columns.drop(list(model_input.filter(regex='empatica_temperature')))]
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
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['target'].value_counts()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
# bins = [-10, 0, 10] # bins for z-scored targets
|
||||
bins = [-1, 0, 4] # bins for stressfulness (0-4) target
|
||||
model_input['target'], edges = pd.cut(model_input.target, bins=bins, labels=['low', 'high'], retbins=True, right=True) #['low', 'medium', 'high']
|
||||
|
@ -64,7 +67,7 @@ model_input['target'] = model_input['target'].astype(str).apply(lambda x: 0 if x
|
|||
|
||||
model_input['target'].value_counts()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
# UnderSampling
|
||||
if undersampling:
|
||||
model_input.groupby("pid").count()
|
||||
|
@ -77,7 +80,7 @@ if undersampling:
|
|||
model_input["target"].value_counts()
|
||||
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
if cv_method_str == 'half_logo':
|
||||
model_input['pid_index'] = model_input.groupby('pid').cumcount()
|
||||
model_input['pid_count'] = model_input.groupby('pid')['pid'].transform('count')
|
||||
|
@ -90,7 +93,7 @@ else:
|
|||
data_x, data_y, data_groups = model_input.drop(["target", "pid"], axis=1), model_input["target"], model_input["pid"]
|
||||
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
categorical_feature_colnames = ["gender", "startlanguage"]
|
||||
additional_categorical_features = [col for col in data_x.columns if "mostcommonactivity" in col or "homelabel" in col]
|
||||
categorical_feature_colnames += additional_categorical_features
|
||||
|
@ -110,7 +113,7 @@ numerical_features = data_x.drop(categorical_feature_colnames, axis=1)
|
|||
train_x = pd.concat([numerical_features, categorical_features], axis=1)
|
||||
train_x.dtypes
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
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':
|
||||
cv_method = LeaveOneGroupOut()
|
||||
|
@ -120,14 +123,16 @@ if cv_method_str == 'logo' or cv_method_str == 'half_logo':
|
|||
groups=data_groups,
|
||||
)
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
imputer = SimpleImputer(missing_values=np.nan, strategy='median')
|
||||
|
||||
# %% [markdown]
|
||||
# ### Baseline: Dummy Classifier (most frequent)
|
||||
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false} nteract={"transient": {"deleting": false}}
|
||||
dummy_class = DummyClassifier(strategy="most_frequent")
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
dummy_classifier = cross_validate(
|
||||
dummy_class,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -138,7 +143,7 @@ dummy_classifier = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(dummy_classifier['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(dummy_classifier['test_accuracy']))
|
||||
print("Precision", np.mean(dummy_classifier['test_precision']))
|
||||
|
@ -150,10 +155,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(dummy_classifier['test_accur
|
|||
# %% [markdown]
|
||||
# ### Logistic Regression
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
logistic_regression = linear_model.LogisticRegression()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
log_reg_scores = cross_validate(
|
||||
logistic_regression,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -163,7 +168,7 @@ log_reg_scores = cross_validate(
|
|||
n_jobs=-1,
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(log_reg_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(log_reg_scores['test_accuracy']))
|
||||
print("Precision", np.mean(log_reg_scores['test_precision']))
|
||||
|
@ -175,10 +180,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(log_reg_scores['test_accurac
|
|||
# %% [markdown]
|
||||
# ### Support Vector Machine
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
svc = svm.SVC()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
svc_scores = cross_validate(
|
||||
svc,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -188,7 +193,7 @@ svc_scores = cross_validate(
|
|||
n_jobs=-1,
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(svc_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(svc_scores['test_accuracy']))
|
||||
print("Precision", np.mean(svc_scores['test_precision']))
|
||||
|
@ -200,10 +205,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(svc_scores['test_accuracy'],
|
|||
# %% [markdown]
|
||||
# ### Gaussian Naive Bayes
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
gaussian_nb = naive_bayes.GaussianNB()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
gaussian_nb_scores = cross_validate(
|
||||
gaussian_nb,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -214,7 +219,7 @@ gaussian_nb_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(gaussian_nb_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(gaussian_nb_scores['test_accuracy']))
|
||||
print("Precision", np.mean(gaussian_nb_scores['test_precision']))
|
||||
|
@ -226,10 +231,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(gaussian_nb_scores['test_acc
|
|||
# %% [markdown]
|
||||
# ### Stochastic Gradient Descent Classifier
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
sgdc = linear_model.SGDClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
sgdc_scores = cross_validate(
|
||||
sgdc,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -240,7 +245,7 @@ sgdc_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(sgdc_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(sgdc_scores['test_accuracy']))
|
||||
print("Precision", np.mean(sgdc_scores['test_precision']))
|
||||
|
@ -252,10 +257,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(sgdc_scores['test_accuracy']
|
|||
# %% [markdown]
|
||||
# ### K-nearest neighbors
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
knn = neighbors.KNeighborsClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
knn_scores = cross_validate(
|
||||
knn,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -266,7 +271,7 @@ knn_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(knn_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(knn_scores['test_accuracy']))
|
||||
print("Precision", np.mean(knn_scores['test_precision']))
|
||||
|
@ -278,10 +283,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(knn_scores['test_accuracy'],
|
|||
# %% [markdown]
|
||||
# ### Decision Tree
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
dtree = tree.DecisionTreeClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
dtree_scores = cross_validate(
|
||||
dtree,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -292,7 +297,7 @@ dtree_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(dtree_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(dtree_scores['test_accuracy']))
|
||||
print("Precision", np.mean(dtree_scores['test_precision']))
|
||||
|
@ -304,10 +309,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(dtree_scores['test_accuracy'
|
|||
# %% [markdown]
|
||||
# ### Random Forest Classifier
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
rfc = ensemble.RandomForestClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
rfc_scores = cross_validate(
|
||||
rfc,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -319,7 +324,7 @@ rfc_scores = cross_validate(
|
|||
scoring=('accuracy', 'precision', 'recall', 'f1'),
|
||||
return_estimator=True
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(rfc_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(rfc_scores['test_accuracy']))
|
||||
print("Precision", np.mean(rfc_scores['test_precision']))
|
||||
|
@ -331,7 +336,7 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(rfc_scores['test_accuracy'],
|
|||
# %% [markdown]
|
||||
# ### Feature importance (RFC)
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
rfc_es_fimp = pd.DataFrame(columns=list(train_x.columns))
|
||||
for idx, estimator in enumerate(rfc_scores['estimator']):
|
||||
feature_importances = pd.DataFrame(estimator.feature_importances_,
|
||||
|
@ -353,10 +358,10 @@ train_x['empatica_temperature_cr_stdDev_X_SO_mean'].value_counts()
|
|||
# %% [markdown]
|
||||
# ### Gradient Boosting Classifier
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
gbc = ensemble.GradientBoostingClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
gbc_scores = cross_validate(
|
||||
gbc,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -367,7 +372,7 @@ gbc_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(gbc_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(gbc_scores['test_accuracy']))
|
||||
print("Precision", np.mean(gbc_scores['test_precision']))
|
||||
|
@ -379,10 +384,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(gbc_scores['test_accuracy'],
|
|||
# %% [markdown]
|
||||
# ### LGBM Classifier
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
lgbm = LGBMClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
lgbm_scores = cross_validate(
|
||||
lgbm,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -393,7 +398,7 @@ lgbm_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(lgbm_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(lgbm_scores['test_accuracy']))
|
||||
print("Precision", np.mean(lgbm_scores['test_precision']))
|
||||
|
@ -405,10 +410,10 @@ print(f"Smallest {n_sl} ACC:", np.sort(np.partition(lgbm_scores['test_accuracy']
|
|||
# %% [markdown]
|
||||
# ### XGBoost Classifier
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
xgb_classifier = xg.sklearn.XGBClassifier()
|
||||
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
xgb_classifier_scores = cross_validate(
|
||||
xgb_classifier,
|
||||
X=imputer.fit_transform(train_x),
|
||||
|
@ -419,7 +424,7 @@ xgb_classifier_scores = cross_validate(
|
|||
error_score='raise',
|
||||
scoring=('accuracy', 'precision', 'recall', 'f1')
|
||||
)
|
||||
# %% jupyter={"source_hidden": true}
|
||||
# %% jupyter={"source_hidden": false, "outputs_hidden": false}
|
||||
print("Acc (median)", np.nanmedian(xgb_classifier_scores['test_accuracy']))
|
||||
print("Acc (mean)", np.mean(xgb_classifier_scores['test_accuracy']))
|
||||
print("Precision", np.mean(xgb_classifier_scores['test_precision']))
|
||||
|
@ -428,4 +433,4 @@ print("F1", np.mean(xgb_classifier_scores['test_f1']))
|
|||
print(f"Largest {n_sl} ACC:", np.sort(-np.partition(-xgb_classifier_scores['test_accuracy'], n_sl)[:n_sl])[::-1])
|
||||
print(f"Smallest {n_sl} ACC:", np.sort(np.partition(xgb_classifier_scores['test_accuracy'], n_sl)[:n_sl]))
|
||||
|
||||
# %%
|
||||
# %% jupyter={"outputs_hidden": false, "source_hidden": false}
|
||||
|
|
Loading…
Reference in New Issue