
    LCj                         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_recommendationd   )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	TZZ	 B-89C

OKL>|nMNN	/]O;	<B #b'#6D
?""A
=  A99Q<,,.L
KK3L>BC\''' \''() *FF	
 (8	1"q($GVWf #E 
IIgw]]6"F"66tDF
KK/0Eff0U/VWXdoo&JTD1 ''F
 66K
KK$&(&1$G6{ KK(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   