Files
AlphaFlow-MT5-ML-DL-Trading…/models/__pycache__/model_training.cpython-312.pyc
T

54 lines
4.6 KiB
Plaintext
Raw Normal View History

2025-03-02 22:25:33 +01:00
Ë
3³g'ãó’ddlZddlZddlmZddlmZmZddejde
fdZ ddZ ddZ
d efd
Zd efd Zd efd
ZddZy)éN)ÚRandomForestRegressor)Úmean_squared_errorÚmean_absolute_errorÚdfÚ train_ratiocó¬tt|«|z«}|jd|j«}|j|dj«}||fS)z=
Splits df into train and test sets chronologically.
N)ÚintÚlenÚilocÚcopy)rrÚ split_indexÚdf_trainÚdf_tests úFc:\Users\moham\OneDrive\AlphaFlow Trading Bot\models\model_training.pyÚtime_based_splitrsTô”c˜"“g  Ñ,€KØw‰w Ð$×+€HØg‰gk#×*€GØ  ÐócóÊ|
tdd¬«}|j||«|j}tj|«ddd}|d|}|j
dd|f}||fS)
Uses a random forest (or a user-provided estimator) to rank feature importances,
then keeps the top 'max_features'.
Returns (X_new, selected_features_indices).
Nédé*©Ú n_estimatorsÚ random_stateéÿÿÿÿ)rÚfitÚfeature_importances_ÚnpÚargsortr )ÚyÚ estimatorÚ max_featuresÚ importancesÚindicesÚ top_indicesÚX_news rÚselect_features_rf_regr&soð ÐÜ)°sÈÔLˆ à
‡MM!Ø×0€Käj‰j˜Ó%¡d¨ +€Gؘ-˜(€KØ
F‰F1k "€EØ  ÐrcóDt||¬«}|j||«|S)zF
Trains a RandomForestRegressor and returns the fitted model.
r)rr)ÚX_trainÚy_trainrrÚrfs rÚtrain_random_forest_regr+#s#ô
¨LÀ|Ô T€B؇FFˆ7Ø
€IrÚreturncó^|j|«}t||«}t||«}||dœS)zj
Evaluates a regression model with MSE and MAE.
Returns a dict with {'mse': ..., 'mae': ...}.
)ÚmseÚmae)Úpredictrr)ÚmodelÚX_testÚy_testÚy_predr.r/s rÚevaluate_regressionr5+s5ð
]‰]˜
"€FÜ
˜V 
,€CÜ
˜f 
-€Cؘ #rÚmodelscó¨ddlm}i}|j«D]6\}}|j||«|j |«} ||| «}
|
||<Œ8|S©zm
Trains and evaluates each model in 'models' dict.
Returns a dict of MSE results for each model.
r)r©Úsklearn.metricsrÚitemsrr0© r(r)r2r3r6rÚresultsÚ
model_namer1r4r.s rÚtrain_and_evaluate_reg_modelsr?7sZõ
€GØ#Ÿ\™\ž^ш

'˜˜Ù  ¨Ó0ˆØ
Òð 
€Nrcó¨ddlm}i}|j«D]6\}}|j||«|j |«} ||| «}
|
||<Œ8|Sr8r9r<s rr?r?FsZõ
€GØ#Ÿ\™\ž^ш

'˜˜Ù  ¨Ó0ˆØ
Òð 
€Nrcó t|«}||dzz}g}t|«D]j}|dz|z}|dz|z}||kDr|}|jd|} |jd|}
|j||} |j||} |j| |
| | f«Œl|S)a
Splits the data X, y into multiple chronological folds.
For example, with n_splits=3, we do:
- Fold 1: Train [0 : fold1], Test [fold1 : fold2]
- Fold 2: Train [0 : fold2], Test [fold2 : fold3]