-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_pipeline.py
72 lines (60 loc) · 3.51 KB
/
default_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -------------------- GENERATED WITH PHOTON WIZARD (beta) ------------------------------
# PHOTON Project Folder: C:/Users/j_boeh06/Sciebo/Forschung/PythonProjects/multimodal_ER
import pandas as pd
import numpy as np
from photonai.base import Hyperpipe, PipelineElement, OutputSettings, Switch, Preprocessing
from photonai.optimization import Categorical, IntegerRange, FloatRange
from sklearn.model_selection import RepeatedKFold, KFold
# Specify how results are going to be saved
# Define hyperpipe
hyperpipe = Hyperpipe('defaultpipelinemultimodaler',
project_folder="C:/Users/j_boeh06/Sciebo/Forschung/PythonProjects/multimodal_ER",
optimizer="grid_search",
optimizer_params={},
metrics=['mean_squared_error', 'mean_absolute_error', 'explained_variance', 'pearson_correlation',
'r2'],
best_config_metric="mean_squared_error",
outer_cv=RepeatedKFold(n_splits=10, random_state=42, n_repeats=10),
inner_cv=KFold(n_splits=10, shuffle=True, random_state=42),
verbosity=1)
# Add Simple Imputer to impute missing
hyperpipe += PipelineElement("SimpleImputer")
# Add preprocessing: Robust Scaler
hyperpipe += PipelineElement("RobustScaler", hyperparameters={},
test_disabled=False, with_centering=True, with_scaling=True)
# Add feature engineering via Switch
# Tests three options PCA, off (when PCA disabled = true) and FSelect
transformer_switch = Switch('TransformerSwitch')
transformer_switch += PipelineElement("PCA", hyperparameters={'n_components':None},
test_disabled=True)
transformer_switch += PipelineElement("FRegressionSelectPercentile", hyperparameters={'percentile': [5, 10, 50]},
test_disabled=False)
hyperpipe += transformer_switch
# Add estimator
# Defaults as defined in the model zoo default parameters: https://github.com/wwu-mmll/photonai/blob/main/photonai/base/model_zoo.py
estimator_switch = Switch('EstimatorSwitch')
# I could save some time here by removing 1e-8 (0.00000001) and 1e8 (100,000,000) without losing too much
estimator_switch += PipelineElement("SVR", hyperparameters={'C': [1e-8, 1e-6, 1e-4, 1e-2, 1, 1e2, 1e4, 1e6, 1e8], 'kernel': ['linear', 'rbf']},
max_iter=1000)
estimator_switch += PipelineElement("RandomForestRegressor", hyperparameters={'max_features': ['sqrt', 'log2'],
'min_samples_leaf': [0.01, 0.1, 0.2]})
hyperpipe += estimator_switch
# Load data
# Adjust this to the modality and its data sheet!
df = pd.read_excel('C:/Users/j_boeh06/Sciebo/Forschung/PythonProjects/multimodal_ER/features.xlsx')
X = np.asarray(df.iloc[:, 1:100])
y = np.asarray(df.iloc[:, 0])
# Fit hyperpipe
hyperpipe.fit(X, y)
# Feature Importances
r = hyperpipe.get_permutation_feature_importances(n_repeats=50, random_state=0)
print(r)
for i in r["mean"].argsort()[::-1]:
if r["mean"][i] - 2 * r["std"][i] > 0:
# Hier Anpassungen von mir: Bezug auf df, Schreiben der entsprechenden Spalten-Header vor die Durchschnitts- und SD-Werte
print(f"{df.columns[i]:<8}: "
f"{r['mean'][i]:.3f}"
f" +/- {r['std'][i]:.3f}")
# Schreiben der Feature Importances in eine CSV-Datei
feature_importance = pd.DataFrame(r)
r.to_csv('feature_importances.csv')