sciml 0.0.7__py3-none-any.whl → 0.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sciml/__init__.py +1 -1
- sciml/models.py +269 -0
- sciml/pipelines.py +262 -1
- {sciml-0.0.7.dist-info → sciml-0.0.8.dist-info}/METADATA +1 -1
- sciml-0.0.8.dist-info/RECORD +9 -0
- sciml-0.0.7.dist-info/RECORD +0 -8
- {sciml-0.0.7.dist-info → sciml-0.0.8.dist-info}/LICENSE +0 -0
- {sciml-0.0.7.dist-info → sciml-0.0.8.dist-info}/WHEEL +0 -0
- {sciml-0.0.7.dist-info → sciml-0.0.8.dist-info}/top_level.txt +0 -0
sciml/__init__.py
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
__all__ = ["utils", "pipelines"]
|
2
|
+
__all__ = ["utils", "pipelines", "models"]
|
sciml/models.py
ADDED
@@ -0,0 +1,269 @@
|
|
1
|
+
import numpy as np
|
2
|
+
import copy
|
3
|
+
import itertools
|
4
|
+
import warnings
|
5
|
+
from xgboost import XGBRegressor
|
6
|
+
from sklearn.metrics import mean_squared_error
|
7
|
+
from sklearn.model_selection import train_test_split
|
8
|
+
|
9
|
+
class SmartForest:
|
10
|
+
"""
|
11
|
+
SmartForest: A deep, intelligent decision forest model for complex sequential and tabular data.
|
12
|
+
|
13
|
+
SmartForest blends ideas from deep forests (cascade forest structures), LSTM-style forget gates,
|
14
|
+
and ensemble learning using XGBoost. It is especially suited for time series or structured tabular data
|
15
|
+
where layer-wise feature expansion and memory-inspired filtering can enhance performance.
|
16
|
+
|
17
|
+
Key Features:
|
18
|
+
-------------
|
19
|
+
- Deep cascade of XGBoost regressors
|
20
|
+
- Optional Multi-Grained Scanning (MGS) for local feature extraction
|
21
|
+
- Forget-gate-inspired mechanism to regulate information flow across layers
|
22
|
+
- Early stopping to prevent overfitting
|
23
|
+
- Full retention of best-performing model (lowest validation RMSE)
|
24
|
+
|
25
|
+
Parameters:
|
26
|
+
-----------
|
27
|
+
n_estimators_per_layer : int
|
28
|
+
Number of XGBoost regressors per layer.
|
29
|
+
|
30
|
+
max_layers : int
|
31
|
+
Maximum number of layers (depth) in the model.
|
32
|
+
|
33
|
+
early_stopping_rounds : int
|
34
|
+
Number of layers with no improvement before early stopping is triggered.
|
35
|
+
|
36
|
+
param_grid : dict
|
37
|
+
Grid of XGBoost hyperparameters to search over.
|
38
|
+
|
39
|
+
use_gpu : bool
|
40
|
+
If True, use GPU-accelerated training (CUDA required).
|
41
|
+
|
42
|
+
gpu_id : int
|
43
|
+
ID of GPU to use (if use_gpu=True).
|
44
|
+
|
45
|
+
window_sizes : list of int
|
46
|
+
Enables Multi-Grained Scanning if non-empty, with specified sliding window sizes.
|
47
|
+
|
48
|
+
forget_factor : float in [0, 1]
|
49
|
+
Simulates LSTM-style forget gate; higher values forget more past information.
|
50
|
+
|
51
|
+
verbose : int
|
52
|
+
Verbosity level (0 = silent, 1 = progress updates).
|
53
|
+
|
54
|
+
Methods:
|
55
|
+
--------
|
56
|
+
fit(X, y, X_val=None, y_val=None):
|
57
|
+
Train the SmartForest model layer by layer, using optional validation for early stopping.
|
58
|
+
|
59
|
+
predict(X):
|
60
|
+
Make predictions on new data using the trained cascade structure.
|
61
|
+
|
62
|
+
get_best_model():
|
63
|
+
Returns a copy of the best model and the corresponding RMSE from validation.
|
64
|
+
|
65
|
+
Example:
|
66
|
+
--------
|
67
|
+
>>> model = SmartForest(n_estimators_per_layer=5, max_layers=10, window_sizes=[2, 3], forget_factor=0.2)
|
68
|
+
>>> model.fit(X_train, y_train, X_val, y_val)
|
69
|
+
>>> y_pred = model.predict(X_val)
|
70
|
+
>>> best_model, best_rmse = model.get_best_model()
|
71
|
+
"""
|
72
|
+
def __init__(self, n_estimators_per_layer = 5, max_layers = 10, early_stopping_rounds = 3, param_grid = None,
|
73
|
+
use_gpu = False, gpu_id = 0, window_sizes = [], forget_factor = 0, verbose = 1):
|
74
|
+
self.n_estimators_per_layer = n_estimators_per_layer
|
75
|
+
self.max_layers = max_layers
|
76
|
+
self.early_stopping_rounds = early_stopping_rounds
|
77
|
+
self.param_grid = param_grid or {
|
78
|
+
"objective": ["reg:squarederror"],
|
79
|
+
"random_state": [42],
|
80
|
+
'seed': [0],
|
81
|
+
'n_estimators': [100],
|
82
|
+
'max_depth': [6],
|
83
|
+
'min_child_weight': [4],
|
84
|
+
'subsample': [0.8],
|
85
|
+
'colsample_bytree': [0.8],
|
86
|
+
'gamma': [0],
|
87
|
+
'reg_alpha': [0],
|
88
|
+
'reg_lambda': [1],
|
89
|
+
'learning_rate': [0.05],
|
90
|
+
}
|
91
|
+
self.use_gpu = use_gpu
|
92
|
+
self.gpu_id = gpu_id
|
93
|
+
self.window_sizes = window_sizes
|
94
|
+
self.forget_factor = forget_factor
|
95
|
+
self.layers = []
|
96
|
+
self.best_model = None
|
97
|
+
self.best_rmse = float("inf")
|
98
|
+
self.verbose = verbose
|
99
|
+
|
100
|
+
def _get_param_combinations(self):
|
101
|
+
keys, values = zip(*self.param_grid.items())
|
102
|
+
return [dict(zip(keys, v)) for v in itertools.product(*values)]
|
103
|
+
|
104
|
+
def _multi_grained_scanning(self, X, y):
|
105
|
+
new_features = []
|
106
|
+
for window_size in self.window_sizes:
|
107
|
+
if X.shape[1] < window_size:
|
108
|
+
continue
|
109
|
+
for start in range(X.shape[1] - window_size + 1):
|
110
|
+
window = X[:, start:start + window_size]
|
111
|
+
if y is None:
|
112
|
+
new_features.append(window)
|
113
|
+
continue
|
114
|
+
|
115
|
+
param_combos = self._get_param_combinations()
|
116
|
+
for params in param_combos:
|
117
|
+
if self.use_gpu:
|
118
|
+
params['tree_method'] = 'hist'
|
119
|
+
params['device'] = 'cuda'
|
120
|
+
model = XGBRegressor(**params)
|
121
|
+
model.fit(window, y)
|
122
|
+
preds = model.predict(window).reshape(-1, 1)
|
123
|
+
new_features.append(preds)
|
124
|
+
return np.hstack(new_features) if new_features else X
|
125
|
+
|
126
|
+
def _apply_forget_gate(self, X, layer_index):
|
127
|
+
forget_weights = np.random.rand(X.shape[1]) * self.forget_factor
|
128
|
+
return X * (1 - forget_weights)
|
129
|
+
|
130
|
+
def _fit_layer(self, X, y, X_val=None, y_val=None, layer_index=0):
|
131
|
+
layer = []
|
132
|
+
layer_outputs = []
|
133
|
+
param_combos = self._get_param_combinations()
|
134
|
+
X = self._apply_forget_gate(X, layer_index)
|
135
|
+
|
136
|
+
for i in range(self.n_estimators_per_layer):
|
137
|
+
best_rmse = float('inf')
|
138
|
+
best_model = None
|
139
|
+
|
140
|
+
for params in param_combos:
|
141
|
+
if self.use_gpu:
|
142
|
+
params['tree_method'] = 'hist'
|
143
|
+
params['device'] = 'cuda'
|
144
|
+
|
145
|
+
model = XGBRegressor(**params)
|
146
|
+
model.fit(X, y)
|
147
|
+
|
148
|
+
if X_val is not None:
|
149
|
+
preds_val = model.predict(X_val)
|
150
|
+
rmse = np.sqrt(mean_squared_error(y_val, preds_val))
|
151
|
+
if rmse < best_rmse:
|
152
|
+
best_rmse = rmse
|
153
|
+
best_model = model
|
154
|
+
else:
|
155
|
+
best_model = model
|
156
|
+
|
157
|
+
preds = best_model.predict(X).reshape(-1, 1)
|
158
|
+
layer.append(best_model)
|
159
|
+
layer_outputs.append(preds)
|
160
|
+
|
161
|
+
output = np.hstack(layer_outputs)
|
162
|
+
return layer, output
|
163
|
+
|
164
|
+
def fit(self, X, y, X_val=None, y_val=None):
|
165
|
+
X_current = self._multi_grained_scanning(X, y)
|
166
|
+
X_val_current = self._multi_grained_scanning(X_val, y_val) if X_val is not None else None
|
167
|
+
no_improve_rounds = 0
|
168
|
+
|
169
|
+
for layer_index in range(self.max_layers):
|
170
|
+
if self.verbose: print(f"Training Layer {layer_index + 1}")
|
171
|
+
layer, output = self._fit_layer(X_current, y, X_val_current, y_val, layer_index)
|
172
|
+
self.layers.append(layer)
|
173
|
+
X_current = np.hstack([X_current, output])
|
174
|
+
|
175
|
+
if X_val is not None:
|
176
|
+
val_outputs = []
|
177
|
+
for reg in layer:
|
178
|
+
n_features = reg.n_features_in_
|
179
|
+
preds = reg.predict(X_val_current[:, :n_features]).reshape(-1, 1)
|
180
|
+
val_outputs.append(preds)
|
181
|
+
val_output = np.hstack(val_outputs)
|
182
|
+
X_val_current = np.hstack([X_val_current, val_output])
|
183
|
+
|
184
|
+
y_pred = self.predict(X_val)
|
185
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
186
|
+
if self.verbose: print(f"Validation RMSE: {rmse:.4f}")
|
187
|
+
|
188
|
+
if rmse < self.best_rmse:
|
189
|
+
self.best_rmse = rmse
|
190
|
+
self.best_model = copy.deepcopy(self.layers)
|
191
|
+
no_improve_rounds = 0
|
192
|
+
if self.verbose: print(f"✅ New best RMSE: {self.best_rmse:.4f}")
|
193
|
+
else:
|
194
|
+
no_improve_rounds += 1
|
195
|
+
if no_improve_rounds >= self.early_stopping_rounds:
|
196
|
+
if self.verbose: print("Early stopping triggered.")
|
197
|
+
break
|
198
|
+
|
199
|
+
def predict(self, X):
|
200
|
+
X_current = self._multi_grained_scanning(X, None)
|
201
|
+
X_current = self._apply_forget_gate(X_current, layer_index=-1)
|
202
|
+
|
203
|
+
for layer in self.layers:
|
204
|
+
layer_outputs = []
|
205
|
+
for reg in layer:
|
206
|
+
n_features = reg.n_features_in_
|
207
|
+
preds = reg.predict(X_current[:, :n_features]).reshape(-1, 1)
|
208
|
+
layer_outputs.append(preds)
|
209
|
+
output = np.hstack(layer_outputs)
|
210
|
+
X_current = np.hstack([X_current, output])
|
211
|
+
|
212
|
+
final_outputs = []
|
213
|
+
for reg in self.layers[-1]:
|
214
|
+
n_features = reg.n_features_in_
|
215
|
+
final_outputs.append(reg.predict(X_current[:, :n_features]).reshape(-1, 1))
|
216
|
+
return np.mean(np.hstack(final_outputs), axis=1)
|
217
|
+
|
218
|
+
def get_best_model(self):
|
219
|
+
return self.best_model, self.best_rmse
|
220
|
+
|
221
|
+
"""
|
222
|
+
# ============================== Test Example ==============================
|
223
|
+
from sklearn.datasets import load_diabetes
|
224
|
+
|
225
|
+
warnings.simplefilter('ignore')
|
226
|
+
|
227
|
+
X, y = load_diabetes(return_X_y=True)
|
228
|
+
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)
|
229
|
+
|
230
|
+
# Hyperparameter grid
|
231
|
+
param_grid = {
|
232
|
+
"objective": ["reg:squarederror"],
|
233
|
+
"random_state": [42],
|
234
|
+
'seed': [0],
|
235
|
+
'n_estimators': [100],
|
236
|
+
'max_depth': [6],
|
237
|
+
'min_child_weight': [4],
|
238
|
+
'subsample': [0.8],
|
239
|
+
'colsample_bytree': [0.8],
|
240
|
+
'gamma': [0],
|
241
|
+
'reg_alpha': [0],
|
242
|
+
'reg_lambda': [1],
|
243
|
+
'learning_rate': [0.05],
|
244
|
+
}
|
245
|
+
|
246
|
+
# Create the model with Multi-Grained Scanning enabled (with window sizes 2 and 3)
|
247
|
+
df_reg = SmartForest(
|
248
|
+
n_estimators_per_layer = 5,
|
249
|
+
max_layers = 10,
|
250
|
+
early_stopping_rounds = 5,
|
251
|
+
param_grid = param_grid,
|
252
|
+
use_gpu = False,
|
253
|
+
gpu_id = 0,
|
254
|
+
window_sizes = [], # Enables MGS if e.g., [2, 3], else empty disables MGS.
|
255
|
+
forget_factor = 0., # Set forget factor to simulate forget gate behavior
|
256
|
+
verbose = 1
|
257
|
+
)
|
258
|
+
|
259
|
+
df_reg.fit(X_train, y_train, X_val, y_val)
|
260
|
+
|
261
|
+
# Predict on validation set and evaluate
|
262
|
+
y_pred = df_reg.predict(X_val)
|
263
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
264
|
+
print("\nFinal RMSE:", rmse)
|
265
|
+
|
266
|
+
# Output best model and RMSE
|
267
|
+
best_model, best_rmse = df_reg.get_best_model()
|
268
|
+
print("\nBest validation RMSE:", best_rmse)
|
269
|
+
"""
|
sciml/pipelines.py
CHANGED
@@ -171,4 +171,265 @@ def train_lstm(X_train, y_train, nfeature, ntime, verbose = 2, epochs = 200, bat
|
|
171
171
|
# es = EarlyStopping(monitor='loss', mode='min', verbose=1)
|
172
172
|
# model.fit(X_train.reshape(-1, nsites, nfeats), y_train, epochs=100, batch_size=256, verbose=2, callbacks=[es])
|
173
173
|
model.fit(X_train, y_train, epochs = epochs, batch_size = batch_size, verbose=verbose)
|
174
|
-
return model
|
174
|
+
return model
|
175
|
+
|
176
|
+
|
177
|
+
'''
|
178
|
+
# ========================================================================================================
|
179
|
+
import numpy as np
|
180
|
+
from xgboost import XGBRegressor
|
181
|
+
from sklearn.metrics import mean_squared_error
|
182
|
+
|
183
|
+
class XGBoostDeepForestRegressor:
|
184
|
+
def __init__(self, n_estimators_per_layer=2, max_layers=20, early_stopping_rounds=2):
|
185
|
+
self.n_estimators_per_layer = n_estimators_per_layer
|
186
|
+
self.max_layers = max_layers
|
187
|
+
self.early_stopping_rounds = early_stopping_rounds
|
188
|
+
self.layers = []
|
189
|
+
|
190
|
+
def _fit_layer(self, X, y):
|
191
|
+
layer = []
|
192
|
+
layer_outputs = []
|
193
|
+
for _ in range(self.n_estimators_per_layer):
|
194
|
+
reg = XGBRegressor()
|
195
|
+
reg.fit(X, y)
|
196
|
+
preds = reg.predict(X).reshape(-1, 1)
|
197
|
+
layer.append(reg)
|
198
|
+
layer_outputs.append(preds)
|
199
|
+
output = np.hstack(layer_outputs)
|
200
|
+
return layer, output
|
201
|
+
|
202
|
+
def fit(self, X, y, X_val=None, y_val=None):
|
203
|
+
X_current = X.copy()
|
204
|
+
best_rmse = float("inf")
|
205
|
+
no_improve_rounds = 0
|
206
|
+
|
207
|
+
for layer_index in range(self.max_layers):
|
208
|
+
print(f"Training Layer {layer_index + 1}")
|
209
|
+
layer, output = self._fit_layer(X_current, y)
|
210
|
+
self.layers.append(layer)
|
211
|
+
X_current = np.hstack([X_current, output])
|
212
|
+
|
213
|
+
if X_val is not None:
|
214
|
+
y_pred = self.predict(X_val)
|
215
|
+
# rmse = mean_squared_error(y_val, y_pred, squared=False)
|
216
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
217
|
+
print(f"Validation RMSE: {rmse:.4f}")
|
218
|
+
|
219
|
+
if rmse < best_rmse:
|
220
|
+
best_rmse = rmse
|
221
|
+
no_improve_rounds = 0
|
222
|
+
else:
|
223
|
+
no_improve_rounds += 1
|
224
|
+
if no_improve_rounds >= self.early_stopping_rounds:
|
225
|
+
print("Early stopping triggered.")
|
226
|
+
break
|
227
|
+
|
228
|
+
def predict(self, X):
|
229
|
+
X_current = X.copy()
|
230
|
+
for layer in self.layers:
|
231
|
+
layer_outputs = []
|
232
|
+
for reg in layer:
|
233
|
+
n_features = reg.n_features_in_
|
234
|
+
preds = reg.predict(X_current[:, :n_features]).reshape(-1, 1)
|
235
|
+
layer_outputs.append(preds)
|
236
|
+
output = np.hstack(layer_outputs)
|
237
|
+
X_current = np.hstack([X_current, output])
|
238
|
+
|
239
|
+
# Final prediction = average of last layer regressors
|
240
|
+
final_outputs = []
|
241
|
+
for reg in self.layers[-1]:
|
242
|
+
n_features = reg.n_features_in_
|
243
|
+
final_outputs.append(reg.predict(X_current[:, :n_features]).reshape(-1, 1))
|
244
|
+
return np.mean(np.hstack(final_outputs), axis=1)
|
245
|
+
|
246
|
+
|
247
|
+
from sklearn.datasets import load_diabetes
|
248
|
+
from sklearn.model_selection import train_test_split
|
249
|
+
from sklearn.metrics import mean_squared_error
|
250
|
+
|
251
|
+
X, y = load_diabetes(return_X_y=True)
|
252
|
+
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)
|
253
|
+
|
254
|
+
df_reg = XGBoostDeepForestRegressor(n_estimators_per_layer=2, max_layers=5)
|
255
|
+
df_reg.fit(X_train, y_train, X_val, y_val)
|
256
|
+
|
257
|
+
y_pred = df_reg.predict(X_val)
|
258
|
+
# rmse = mean_squared_error(y_val, y_pred, squared=False)
|
259
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
260
|
+
print("Final RMSE:", rmse)
|
261
|
+
|
262
|
+
# ----------------------------------------------------------------------------------------------------
|
263
|
+
|
264
|
+
import numpy as np
|
265
|
+
from xgboost import XGBRegressor
|
266
|
+
from sklearn.metrics import mean_squared_error
|
267
|
+
import itertools
|
268
|
+
|
269
|
+
class XGBoostDeepForestRegressor:
|
270
|
+
def __init__(self, n_estimators_per_layer=2, max_layers=20, early_stopping_rounds=2, param_grid=None, use_gpu=True, gpu_id=0):
|
271
|
+
self.n_estimators_per_layer = n_estimators_per_layer
|
272
|
+
self.max_layers = max_layers
|
273
|
+
self.early_stopping_rounds = early_stopping_rounds
|
274
|
+
self.param_grid = param_grid or {
|
275
|
+
'max_depth': [3],
|
276
|
+
'learning_rate': [0.1],
|
277
|
+
'n_estimators': [100]
|
278
|
+
}
|
279
|
+
self.use_gpu = use_gpu
|
280
|
+
self.gpu_id = gpu_id
|
281
|
+
self.layers = []
|
282
|
+
|
283
|
+
def _get_param_combinations(self):
|
284
|
+
keys, values = zip(*self.param_grid.items())
|
285
|
+
return [dict(zip(keys, v)) for v in itertools.product(*values)]
|
286
|
+
|
287
|
+
def _fit_layer(self, X, y, X_val=None, y_val=None):
|
288
|
+
layer = []
|
289
|
+
layer_outputs = []
|
290
|
+
param_combos = self._get_param_combinations()
|
291
|
+
|
292
|
+
for i in range(self.n_estimators_per_layer):
|
293
|
+
best_rmse = float('inf')
|
294
|
+
best_model = None
|
295
|
+
|
296
|
+
for params in param_combos:
|
297
|
+
# Set GPU support parameters in XGBRegressor
|
298
|
+
if self.use_gpu:
|
299
|
+
params['tree_method'] = 'hist' # Use hist method
|
300
|
+
params['device'] = 'cuda' # Enable CUDA for GPU
|
301
|
+
|
302
|
+
model = XGBRegressor(**params)
|
303
|
+
model.fit(X, y)
|
304
|
+
|
305
|
+
if X_val is not None:
|
306
|
+
preds_val = model.predict(X_val)
|
307
|
+
rmse = np.sqrt(mean_squared_error(y_val, preds_val))
|
308
|
+
if rmse < best_rmse:
|
309
|
+
best_rmse = rmse
|
310
|
+
best_model = model
|
311
|
+
else:
|
312
|
+
best_model = model
|
313
|
+
|
314
|
+
final_model = best_model
|
315
|
+
preds = final_model.predict(X).reshape(-1, 1)
|
316
|
+
layer.append(final_model)
|
317
|
+
layer_outputs.append(preds)
|
318
|
+
|
319
|
+
output = np.hstack(layer_outputs)
|
320
|
+
return layer, output
|
321
|
+
|
322
|
+
def fit(self, X, y, X_val=None, y_val=None):
|
323
|
+
X_current = X.copy()
|
324
|
+
X_val_current = X_val.copy() if X_val is not None else None
|
325
|
+
|
326
|
+
best_rmse = float("inf")
|
327
|
+
no_improve_rounds = 0
|
328
|
+
|
329
|
+
for layer_index in range(self.max_layers):
|
330
|
+
print(f"Training Layer {layer_index + 1}")
|
331
|
+
layer, output = self._fit_layer(X_current, y, X_val_current, y_val)
|
332
|
+
self.layers.append(layer)
|
333
|
+
X_current = np.hstack([X_current, output])
|
334
|
+
|
335
|
+
if X_val is not None:
|
336
|
+
val_outputs = []
|
337
|
+
for reg in layer:
|
338
|
+
n_features = reg.n_features_in_
|
339
|
+
preds = reg.predict(X_val_current[:, :n_features]).reshape(-1, 1)
|
340
|
+
val_outputs.append(preds)
|
341
|
+
val_output = np.hstack(val_outputs)
|
342
|
+
X_val_current = np.hstack([X_val_current, val_output])
|
343
|
+
|
344
|
+
y_pred = self.predict(X_val)
|
345
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
346
|
+
print(f"Validation RMSE: {rmse:.4f}")
|
347
|
+
|
348
|
+
if rmse < best_rmse:
|
349
|
+
best_rmse = rmse
|
350
|
+
no_improve_rounds = 0
|
351
|
+
else:
|
352
|
+
no_improve_rounds += 1
|
353
|
+
if no_improve_rounds >= self.early_stopping_rounds:
|
354
|
+
print("Early stopping triggered.")
|
355
|
+
break
|
356
|
+
|
357
|
+
def predict(self, X):
|
358
|
+
X_current = X.copy()
|
359
|
+
for layer in self.layers:
|
360
|
+
layer_outputs = []
|
361
|
+
for reg in layer:
|
362
|
+
n_features = reg.n_features_in_
|
363
|
+
preds = reg.predict(X_current[:, :n_features]).reshape(-1, 1)
|
364
|
+
layer_outputs.append(preds)
|
365
|
+
output = np.hstack(layer_outputs)
|
366
|
+
X_current = np.hstack([X_current, output])
|
367
|
+
|
368
|
+
final_outputs = []
|
369
|
+
for reg in self.layers[-1]:
|
370
|
+
n_features = reg.n_features_in_
|
371
|
+
final_outputs.append(reg.predict(X_current[:, :n_features]).reshape(-1, 1))
|
372
|
+
return np.mean(np.hstack(final_outputs), axis=1)
|
373
|
+
|
374
|
+
|
375
|
+
from sklearn.datasets import load_diabetes
|
376
|
+
from sklearn.model_selection import train_test_split
|
377
|
+
from sklearn.metrics import mean_squared_error
|
378
|
+
|
379
|
+
# Load dataset
|
380
|
+
X, y = load_diabetes(return_X_y=True)
|
381
|
+
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=42)
|
382
|
+
|
383
|
+
# Hyperparameter grid
|
384
|
+
param_grid = {
|
385
|
+
'max_depth': [3, 4],
|
386
|
+
'learning_rate': [0.1, 0.05],
|
387
|
+
'n_estimators': [50, 100]
|
388
|
+
}
|
389
|
+
|
390
|
+
# Create and fit the model with GPU enabled
|
391
|
+
df_reg = XGBoostDeepForestRegressor(
|
392
|
+
n_estimators_per_layer=2,
|
393
|
+
max_layers=5,
|
394
|
+
early_stopping_rounds=2,
|
395
|
+
param_grid=param_grid,
|
396
|
+
use_gpu=True, # Enable GPU acceleration
|
397
|
+
gpu_id=0 # Default to the first GPU
|
398
|
+
)
|
399
|
+
|
400
|
+
df_reg.fit(X_train, y_train, X_val, y_val)
|
401
|
+
|
402
|
+
# Final evaluation
|
403
|
+
y_pred = df_reg.predict(X_val)
|
404
|
+
rmse = np.sqrt(mean_squared_error(y_val, y_pred))
|
405
|
+
print("Final RMSE:", rmse)
|
406
|
+
|
407
|
+
# ----------------------------------------------------------------------------------------------------
|
408
|
+
|
409
|
+
xgb_params = {
|
410
|
+
"objective": "reg:squarederror",
|
411
|
+
"random_state": 0,
|
412
|
+
'seed': 0,
|
413
|
+
'n_estimators': 100,
|
414
|
+
'max_depth': 6,
|
415
|
+
'min_child_weight': 4,
|
416
|
+
'subsample': 0.8,
|
417
|
+
'colsample_bytree': 0.8,
|
418
|
+
'gamma': 0,
|
419
|
+
'reg_alpha': 0,
|
420
|
+
'reg_lambda': 1,
|
421
|
+
'learning_rate': 0.05,
|
422
|
+
}
|
423
|
+
|
424
|
+
from xgboost import XGBRegressor
|
425
|
+
regr = XGBRegressor(**xgb_params)
|
426
|
+
|
427
|
+
regr.fit(X_train, y_train)
|
428
|
+
y_pred = regr.predict(X_val)
|
429
|
+
|
430
|
+
|
431
|
+
from scipy import stats
|
432
|
+
|
433
|
+
stats.linregress(y_val, y_pred)
|
434
|
+
|
435
|
+
'''
|
@@ -0,0 +1,9 @@
|
|
1
|
+
sciml/__init__.py,sha256=6iQAGgCEMuw4yoLBzZDax46a45LZgzEeNSHQMdmcBSQ,58
|
2
|
+
sciml/models.py,sha256=p6cw3SxTQaOtFhJx8KdW0Z2QtxBlSBlVPHETTNCjJ2w,9880
|
3
|
+
sciml/pipelines.py,sha256=CJolleJakoEQc-EV-v6NovP3bDb1hif7SvObXdaLXdY,15268
|
4
|
+
sciml/utils.py,sha256=u5DzQJV4aCZ-p7sY56Fxzj8WDGYOgn1rOTeGzAw0vwY,1831
|
5
|
+
sciml-0.0.8.dist-info/LICENSE,sha256=dX4jBmkgQPWc_TfYkXtKQzVIgZQWFuHZ8vQjV4sEeV4,1060
|
6
|
+
sciml-0.0.8.dist-info/METADATA,sha256=uMCtigVwS2e0abqbvfbLZca6iZnkdDTBXtbjdg34yIA,313
|
7
|
+
sciml-0.0.8.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
8
|
+
sciml-0.0.8.dist-info/top_level.txt,sha256=dS_7aBCZFKQE3myPy5sh4USjQZCZyGg382-YxUUYcdw,6
|
9
|
+
sciml-0.0.8.dist-info/RECORD,,
|
sciml-0.0.7.dist-info/RECORD
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
sciml/__init__.py,sha256=Asqzx08kEOBLv_IRE20VlHxZu9XgydyrzIMUDRE-qiU,48
|
2
|
-
sciml/pipelines.py,sha256=5qfeHdxGhF-GMu-rTiInPv5metXiT32uSENIDFd2Ths,6333
|
3
|
-
sciml/utils.py,sha256=u5DzQJV4aCZ-p7sY56Fxzj8WDGYOgn1rOTeGzAw0vwY,1831
|
4
|
-
sciml-0.0.7.dist-info/LICENSE,sha256=dX4jBmkgQPWc_TfYkXtKQzVIgZQWFuHZ8vQjV4sEeV4,1060
|
5
|
-
sciml-0.0.7.dist-info/METADATA,sha256=363EbWoSVqR9qAdhOfeVD8RiP6DfcalvDiZECJ6LW3s,313
|
6
|
-
sciml-0.0.7.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
7
|
-
sciml-0.0.7.dist-info/top_level.txt,sha256=dS_7aBCZFKQE3myPy5sh4USjQZCZyGg382-YxUUYcdw,6
|
8
|
-
sciml-0.0.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|