Skip to content

Commit f9fb748

Browse files
pyiron-runnerprabhath-c
authored andcommitted
Format black
1 parent 6eac299 commit f9fb748

4 files changed

Lines changed: 587 additions & 516 deletions

File tree

ml_nodes.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
This module contains nodes for for machine learning workflows using sk-learn models.
55
"""
6+
67
import pandas as pd
78
import numpy as np
89

@@ -21,7 +22,7 @@ def MLDataSplitter(
2122
train_fraction: float = 0.70,
2223
validation_fraction: float = 0.15,
2324
test_fraction: float = 0.15,
24-
random_state: int = 42
25+
random_state: int = 42,
2526
):
2627
"""
2728
Splits dataframe into train, validation, and test sets. This node prevents data leakage when connected correctly.
@@ -61,10 +62,7 @@ def MLDataSplitter(
6162
temp_fraction = validation_fraction + test_fraction
6263

6364
X_train, X_temp, y_train, y_temp = train_test_split(
64-
X_numeric,
65-
y,
66-
test_size=temp_fraction,
67-
random_state=random_state
65+
X_numeric, y, test_size=temp_fraction, random_state=random_state
6866
)
6967

7068
# -----------------------------
@@ -77,33 +75,30 @@ def MLDataSplitter(
7775
X_temp,
7876
y_temp,
7977
test_size=(1 - validation_size_adjusted),
80-
random_state=random_state
78+
random_state=random_state,
8179
)
8280

8381
return X_train, X_validation, X_test, y_train, y_validation, y_test
8482

8583

86-
8784
@as_function_node
88-
def train_regressor(X_train:pd.DataFrame, y_train:pd.DataFrame, r_type: str = None):
85+
def train_regressor(X_train: pd.DataFrame, y_train: pd.DataFrame, r_type: str = None):
8986
"""
9087
trains a regressor
9188
"""
92-
if r_type!= None:
93-
if r_type=="linear":
89+
if r_type != None:
90+
if r_type == "linear":
9491
reg = LinearRegression().fit(X_train, y_train)
95-
if r_type=="tree":
92+
if r_type == "tree":
9693
reg = RandomForestRegressor().fit(X_train, y_train)
9794
return reg
9895

9996

100-
101-
102-
10397
# =========================================================
10498
# 2) MODEL EVALUATION FUNCTION
10599
# =========================================================
106100

101+
107102
@as_function_node
108103
def EvaluateRegressionModel(model, X_test, y_test):
109104
"""
@@ -122,11 +117,7 @@ def EvaluateRegressionModel(model, X_test, y_test):
122117
r2 = r2_score(y_test, y_pred)
123118
mse = mean_squared_error(y_test, y_pred)
124119
mae = mean_absolute_error(y_test, y_pred)
125-
out = {
126-
"R2": r2,
127-
"MSE": mse,
128-
"MAE": mae
129-
}
120+
out = {"R2": r2, "MSE": mse, "MAE": mae}
130121
return out
131122

132123

@@ -135,13 +126,8 @@ def EvaluateRegressionModel(model, X_test, y_test):
135126
# =========================================================
136127

137128

138-
@as_function_node
139-
def ChooseBestModel(
140-
model_1,
141-
model_2,
142-
X_validation,
143-
y_validation
144-
):
129+
@as_function_node
130+
def ChooseBestModel(model_1, model_2, X_validation, y_validation):
145131
"""
146132
Compares two regression models on VALIDATION DATA.
147133
@@ -189,14 +175,8 @@ def ChooseBestModel(
189175
best_model = model_2
190176

191177
results = {
192-
"model_1": {
193-
"R2": r2_1,
194-
"RMSE": rmse_1
195-
},
196-
"model_2": {
197-
"R2": r2_2,
198-
"RMSE": rmse_2
199-
}
178+
"model_1": {"R2": r2_1, "RMSE": rmse_1},
179+
"model_2": {"R2": r2_2, "RMSE": rmse_2},
200180
}
201181

202-
return best_model, results
182+
return best_model, results

0 commit comments

Comments
 (0)