
    MCj                         d Z ddlZddlmZ ddlZddlmZmZ ddlm	Z	 ddl
mZ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 Decision Tree trend analysis classifier.

Usage:
    python -m ml_pipeline.training.train_dt_trend         --input data/processed/final_feature_matrix.csv         --output-dir data/models/v1
    N)Path)accuracy_scoreclassification_report)train_test_split)DecisionTreeClassifierexport_text)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trend_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|        t)        ||dd|      \  }}}	}
t+        dddd      }|j-                  ||	       |j/                  |      }t1        |
|      }t3        |
|d      }t$        j'                  d|d       t$        j'                  dt5        |t                      t7        |j8                        }|j;                  dd       |t        t        d}|dz  }t=        j>                  ||       tA        |d	tC        |      |dt        t        |      t        |             t$        j'                  d|        y )Nz$Train Decision Tree trend classifier)descriptionz--inputT)requiredz--output-dirz#Input is missing required columns: )subsetdt_trend   )minimumzTrend class distribution:
g?*   )	test_sizerandom_statestratify      
   )	max_depthmin_samples_splitmin_samples_leafr"   )output_dictzTest accuracy: z.3fzDecision tree structure:
)feature_names)parentsexist_ok)modelfeature_colssource_feature_colszdt_trend.pkl)accuracyr   )
model_namemetricsr/   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infor   r   fitpredictr   r   r   r   
output_dirmkdirjoblibdumpr   float)parserargsdfmissing_colsXyclass_countsX_trainX_testy_trainy_testr.   y_predr1   reportrK   bundleoutput_paths                     PC:\Crop_Prediction\Backend\crop-ai-system\ml_pipeline\training\train_dt_trend.pymainr`   *   s+   $$1WXF
	D1
6D	TZZ	 B-89C

OKL>|nMNN	/]O;	<B#b':s;
?""A
=  A99Q<,,.L
KK-l^<='7	1"q($GVWf #	E 
IIgw]]6"Fff-H"66tDF
KK/(301
KK,[o-^,_`adoo&JTD1 '.F
 ~-K
KK$"8_vN$G6{ KK(67    __main__)returnN)__doc__r6   pathlibr   rM   sklearn.metricsr   r   sklearn.model_selectionr   sklearn.treer   r   pandasr:   #ml_pipeline.training.training_utilsr	   r
   r   __name__rG   r>   r?   r`    ra   r_   <module>rm      sY       A 4 <  i i	H	 ;8| zF ra   