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

51 lines
5.0 KiB
Plaintext
Raw Normal View History

2025-03-02 22:25:33 +01:00
§
3³g'ãóŠddlZddlZddlmZddlmZmZddej de
fdZ dd „Z dd Z
d
efdZdefdZdefdZddZdS)éN)ÚRandomForestRegressor)Úmean_squared_errorÚmean_absolute_errorçš™™™™™é?ÚdfÚ train_ratiocóÌtt|¦«|z¦«}|jd| ¦«}|j|d ¦«}||fS)z=
Splits df into train and test sets chronologically.
N)ÚintÚlenÚilocÚcopy)rrÚ split_indexÚdf_trainÚdf_tests ú?c:\Users\moham\OneDrive\ml_bot_trading\models\model_training.pyÚtime_based_splitrs_õ•c˜"g”g  Ñ,€KØŒw +€HØŒgkl#×*€GØ  ÐóécóÚ|tdd¬¦«}| ||¦«|j}tj|¦«ddd}|d|}|jdd|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(sð ÐÝ)°sÈÐLˆ à
‡M‚M!ÔÐØÔ0€KåŒj˜Ñ% d  +€Gؘ-˜<˜(€KØ
ŒF111k "€EØ  ÐrrrcóTt||¬¦«}| ||¦«|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Ñ TÔ T€B؇F‚Fˆ7ÔÐØ
€IrÚreturncóv| |¦«}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_predr0r1s rÚevaluate_regressionr7+sAð
]Š]˜
"€FÝ
˜V 
,€CÝ
˜f 
-€Cؘ #rÚmodelscóÂddlm}i}| ¦«D]A\}}| ||¦«| |¦«} ||| ¦«}
|
||<ŒB|S©zm
Trains and evaluates each model in 'models' dict.
Returns a dict of MSE results for each model.
r)r©Úsklearn.metricsrÚitemsrr2© r*r+r4r5r8rÚresultsÚ
model_namer3r6r0s rÚtrain_and_evaluate_reg_modelsrA7s€ð
€GØ#Ÿ\š\™^œ^ðˆ

Š '˜˜&ˆØ Ð  ¨ÑØ
ÑÐØ €NrcóÂddlm}i}| ¦«D]A\}}| ||¦«| |¦«} ||| ¦«}
|
||<ŒB|Sr:r;r>s rrArAFs€ð
€GØ#Ÿ\š\™^œ^ðˆ

Š '˜˜&ˆØ Ð  ¨ÑØ
ÑÐØ €Nrécó6t|¦«}||dzz}g}t|¦«D]o}|dz|z}|dz|z}||kr|}|jd|} |jd|}
|j||} |j||} | | |
| | f¦«Œp|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]