# train.py
### command> python train.py --fold 0 --model decision_tree_gini
import argparse 
import os

import joblib
import pandas as pd
from sklearn import metrics

import config
import dispatcher


def run(fold, model):
    # read the training data with folds 
    df = pd.read_csv(config.TRAINING_FILE)

    # training data is where kfold is not equal to provided fold
    # also, note that we reset the index
    df_train = df[df.kfold != fold].reset_index(drop=True)

    # validation data is where kfold is equal to provided fold
    df_valid = df[df.kfold == fold].reset_index(drop=True) 
    
    # drop the label column from dataframe and convert it to
    # a numpy array by using .values.

    # target is label column in the dataframe
    x_train = df_train.drop("target", axis=1).values 
    y_train = df_train.target.values

    # similarly, for validation, we have
    x_valid = df_valid.drop("target", axis=1).values 
    y_valid = df_valid.target.values

    # fetch the model from model_dispatcher
    clf = dispatcher.model[model]

    # fir the model on training data
    clf.fit(x_train, y_train)

    # create predictions for validation samples
    preds = clf.predict(x_valid)

    # calculate & print accuracy
    accuracy = metrics.accuracy_score(y_valid, preds) 
    print(f"Fold={fold}, Accuracy={accuracy}")

    # save the model
    joblib.dump( 
        clf,
        os.path.join(config.MODEL_OUTPUT, f"dt_{fold}.bin")
    )


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument( "--fold",type=int )
    parser.add_argument( "--model",type=str )
    args = parser.parse_args()

    run( fold=args.fold,model=args.model )

# train.py
### command> python train.py --fold 0 --model decision_tree_gini
import argparse 
import os

import joblib
import pandas as pd
from sklearn import metrics

import config
import dispatcher


def run(fold, model):
    # read the training data with folds 
    df = pd.read_csv(config.TRAINING_FILE)

    # training data is where kfold is not equal to provided fold
    # also, note that we reset the index
    df_train = df[df.kfold != fold].reset_index(drop=True)

    # validation data is where kfold is equal to provided fold
    df_valid = df[df.kfold == fold].reset_index(drop=True) 
    
    # drop the label column from dataframe and convert it to
    # a numpy array by using .values.

    # target is label column in the dataframe
    x_train = df_train.drop("target", axis=1).values 
    y_train = df_train.target.values

    # similarly, for validation, we have
    x_valid = df_valid.drop("target", axis=1).values 
    y_valid = df_valid.target.values

    # fetch the model from model_dispatcher
    clf = dispatcher.model[model]

    # fir the model on training data
    clf.fit(x_train, y_train)

    # create predictions for validation samples
    preds = clf.predict(x_valid)

    # calculate & print accuracy
    accuracy = metrics.accuracy_score(y_valid, preds) 
    print(f"Fold={fold}, Accuracy={accuracy}")

    # save the model
    joblib.dump( 
        clf,
        os.path.join(config.MODEL_OUTPUT, f"dt_{fold}.bin")
    )


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument( "--fold",type=int )
    parser.add_argument( "--model",type=str )
    args = parser.parse_args()

    run( fold=args.fold,model=args.model )

# train.py ### command> python train.py --fold 0 --model decision_tree_gini # read the training data with folds # training data is where kfold is not equal to provided fold # also, note that we reset the index # validation data is where kfold is equal to provided fold # drop the label column from dataframe and convert it to # a numpy array by using .values. # target is label column in the dataframe # similarly, for validation, we have # fetch the model from model_dispatcher # fir the model on training data # create predictions for validation samples # calculate & print accuracy # save the model