Ë
    LµCjÁ  ã                   ó¢   — d Z ddlZddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 ddlZddlmZmZmZ  ee«      Zg d¢Zd	Zdd
„Zedk(  r e«        yy)zà
Train the Random Forest crop recommendation/ranking classifier.

Usage:
    python -m ml_pipeline.training.train_rf_recommendation         --input data/processed/final_feature_matrix.csv         --output-dir data/models/v1
é    N)ÚPath)ÚRandomForestClassifier)Úclassification_report)Útrain_test_split)Úcheck_minimum_samplesÚ
get_loggerÚsave_training_metadata)Úrainfall_mmÚtemp_cÚhumidityÚseason_indexÚrainfall_anomalyÚrainfall_ma3Úrainfall_lag1Úwater_req_mmÚ
min_temp_cÚ
max_temp_cÚdrought_tolerantÚgrowth_daysÚsuitability_labelc            
      ó(  — t        j                  d¬«      } | j                  dd¬«       | j                  dd¬«       | j                  «       }t	        j
                  |j                  «      }t        t        t        gz   «      t        |j                  «      z
  }|rt        d|› �«      ‚|j                  t        t        gz   ¬«      }t        t        |«      d	d
¬«       |t           j                  }|t           j                  }t	        j                   |«      j#                  «       }t$        j'                  d|› �«       d|j(                  vr,t$        j+                  dt-        |j(                  «      › d�«       t/        ||dd|¬«      \  }}}	}
t1        dddddd¬«      }|j3                  ||	«       |j5                  |«      }t7        |
|d¬«      }t$        j'                  dt7        |
|«      › �«       t9        |j:                  «      }|j=                  dd¬«       |t        t-        |j>                  «      dœ}|dz  }tA        jB                  ||«       tE        |d	d|it        t        |«      t        |«      ¬«       t$        j'                  d |› �«       y )!Nz(Train Random Forest recommendation model)Údescriptionz--inputT)Úrequiredz--output-dirz#Input is missing required columns: )ÚsubsetÚrf_recommendationéd   )Úminimumz Suitability class distribution:
Úhighud  Training data contains no 'high' suitability label â€” this is a known gap caused by insufficient market price history (the 'high' label requires both feasibility='suitable' AND a positive demand_trend, and demand_trend cannot be computed from a single-day price snapshot; see docs/data_sourcing.md). Training will proceed on the classes that ARE present (uO  ), but the live recommendation endpoint (app/services/model_runners.run_recommendation_model) will correctly report every recommendation as degraded until real historical market data is sourced and this is retrained. This is a real trained model, not a placeholder â€” it is just honestly limited by the input data available right now.gš™™™™™É?é*   )Ú	test_sizeÚrandom_stateÚstratifyéÈ   é   é   Úsqrtéÿÿÿÿ)Ún_estimatorsÚ	max_depthÚmin_samples_leafÚmax_featuresr!   Ún_jobs)Úoutput_dictzTest classification report:
)ÚparentsÚexist_ok)ÚmodelÚfeature_colsÚclasseszrf_recommendation.pklr   )Ú
model_nameÚmetricsr1   Ún_trainÚn_testzSaved model bundle to )#ÚargparseÚArgumentParserÚadd_argumentÚ
parse_argsÚpdÚread_csvÚinputÚsetÚFEATURE_COLUMNSÚTARGET_COLUMNÚcolumnsÚ
ValueErrorÚdropnar   ÚlenÚvaluesÚSeriesÚvalue_countsÚloggerÚinfoÚindexÚwarningÚlistr   r   ÚfitÚpredictr   r   Ú
output_dirÚmkdirÚclasses_ÚjoblibÚdumpr	   )ÚparserÚargsÚdfÚmissing_colsÚXÚyÚclass_countsÚX_trainÚX_testÚy_trainÚy_testr0   Úy_predÚreportrO   ÚbundleÚoutput_paths                    úYC:\Crop_Prediction\Backend\crop-ai-system\ml_pipeline\training\train_rf_recommendation.pyÚmainrd   "   sZ  € Ü×$Ñ$Ð1[Ô\€FØ
×Ñ˜	¨DÐÔ1Ø
×Ñ˜°ÐÔ6Ø×ÑÓ€Dä	�‰�T—Z‘ZÓ	 €Bä”¬-¨Ñ8Ó9¼CÀÇ
Á
»OÑK€LÙÜÐ>¸|¸nÐMÓNÐNà	�‰œ/¬]¨OÑ;ˆÓ	<€Bô œ#˜b›'Ð#6ÀÕDà
Œ?Ñ×"Ñ"€AØ
Œ=Ñ× Ñ €Aä—9‘9˜Q“<×,Ñ,Ó.€LÜ
‡K�KÐ3°L°>ÐBÔCØ�\×'Ñ'Ñ'Ü�‰ðô �\×'Ñ'Ó(Ð)ð *FðFô	
ô (8Ø	ˆ1˜¨"°qô(Ñ$€GˆV�W˜fô #ØØØØØØô€Eð 
‡I�Iˆg�wÔà�]‰]˜6Ó"€FÜ" 6¨6¸tÔD€FÜ
‡K�KÐ/Ô0EÀfÈfÓ0UÐ/VÐWÔXä�d—o‘oÓ&€JØ×Ñ˜T¨DÐÔ1ð Ü'Ü˜Ÿ™Ó'ñ€Fð
 Ð6Ñ6€KÜ
‡K�K�˜Ô$äØØ&Ø(¨&Ð1Ü$Ü�G“Ü�6‹{õô ‡K�KÐ(¨¨Ð6Õ7ó    Ú__main__)ÚreturnN)Ú__doc__r7   Úpathlibr   rR   Úsklearn.ensembler   Úsklearn.metricsr   Úsklearn.model_selectionr   Úpandasr;   Ú#ml_pipeline.training.training_utilsr   r   r	   Ú__name__rH   r?   r@   rd   © re   rc   ú<module>rq      sZ   ðñó Ý ã Ý 3Ý 1Ý 4ã ç iÑ iá	�HÓ	€ò€ð $€óM8ð` ˆzÒÙ…Fð re   