(instruction,
                  dataset=None,
                  preprocess=True,
                  ca_threshold=None,
                  text=[],
                  test_size=0.2,
                  drop=None,
                  criterion='gini',
                  splitter='best',
                  max_depth=None,
                  min_samples_split=2,
                  min_samples_leaf=1,
                  min_weight_fraction_leaf=0.0,
                  max_leaf_nodes=None,
                  min_impurity_decrease=0.0,
                  ccp_alpha=0.0)
return {
        'id': generate_id(),
        "model": clf,
        "target": remove,
        'num_classes': num_classes,
        "accuracy": {'cross_val_score': cross_val_score(
            clf,
            X_train,
            y_train,
            cv=3), 'accuracy_score': score},
        "accuracy_score": score,
        "preprocessor": full_pipeline,
        "interpreter": label_mappings,
        'test_data': {'X': X_test, 'y': y_test}}
function to train a decision tree algorithm.
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
function to train a decision tree algorithm.
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
function to train a decision tree algorithm . : param many params : used to hyperparametrize the function . : return a dictionary object with all of the information for the algorithm .
def decision_tree(instruction,
                  dataset=None,
                  preprocess=True,
                  ca_threshold=None,
                  text=[],
                  test_size=0.2,
                  drop=None,
                  criterion='gini',
                  splitter='best',
                  max_depth=None,
                  min_samples_split=2,
                  min_samples_leaf=1,
                  min_weight_fraction_leaf=0.0,
                  max_leaf_nodes=None,
                  min_impurity_decrease=0.0,
                  ccp_alpha=0.0):
    '''
    function to train a decision tree algorithm.
    :param many params: used to hyperparametrize the function.
    :return a dictionary object with all of the information for the algorithm.
    '''
    logger("Reading in dataset")

    dataReader = DataReader(dataset)
    data = dataReader.data_generator()
    logger("Preprocessing data")
    if drop is not None:
        data.drop(drop, axis=1, inplace=True)

    data, y, remove, full_pipeline = initial_preprocessor(
        data, instruction, preprocess, ca_threshold, text)
    logger("->", "Target column found: {}".format(remove))

    X_train = data['train']
    y_train = y['train']
    X_test = data['test']
    y_test = y['test']

    # classification_column = get_similar_column(getLabelwithInstruction(instruction), data)

    # Needed to make a custom label encoder due to train test split changes
    # Can still be inverse transformed, just a bit of extra work
    y_vals = np.unique(pd.concat([y['train'], y['test']], axis=0))
    label_mappings = sklearn.preprocessing.LabelEncoder()
    label_mappings.fit(y_vals)

    y_train = label_mappings.transform(y_train)
    y_test = label_mappings.transform(y_test)

    logger("Labels being mapped to appropriate classes")
    num_classes = len(np.unique(y))

    # fitting and storing
    logger("Fitting Decision Tree")

    clf = tree.DecisionTreeClassifier(
        criterion=criterion,
        splitter=splitter,
        max_depth=max_depth,
        min_samples_split=min_samples_split,
        min_samples_leaf=min_samples_leaf,
        min_weight_fraction_leaf=min_weight_fraction_leaf,
        max_leaf_nodes=max_leaf_nodes,
        min_impurity_decrease=min_impurity_decrease,
        ccp_alpha=ccp_alpha)
    clf = clf.fit(X_train, y_train)

    score = accuracy_score(
        clf.predict(X_test),
        y_test)
    logger("->", "Score found on testing set: {}".format(score))
    logger("Stored model under 'decision_tree' key")
    clearLog()

    return {
        'id': generate_id(),
        "model": clf,
        "target": remove,
        'num_classes': num_classes,
        "accuracy": {'cross_val_score': cross_val_score(
            clf,
            X_train,
            y_train,
            cv=3), 'accuracy_score': score},
        "accuracy_score": score,
        "preprocessor": full_pipeline,
        "interpreter": label_mappings,
        'test_data': {'X': X_test, 'y': y_test}}
def decision_tree ( instruction , dataset = None , preprocess = True , ca_threshold = None , text = [ ] , test_size = 0.2 , drop = None , criterion = 'gini' , splitter = 'best' , max_depth = None , min_samples_split = 2 , min_samples_leaf = 1 , min_weight_fraction_leaf = 0.0 , max_leaf_nodes = None , min_impurity_decrease = 0.0 , ccp_alpha = 0.0 ) : logger ( "Reading in dataset" ) dataReader = DataReader ( dataset ) data = dataReader . data_generator ( ) logger ( "Preprocessing data" ) if drop is not None : data . drop ( drop , axis = 1 , inplace = True ) data , y , remove , full_pipeline = initial_preprocessor ( data , instruction , preprocess , ca_threshold , text ) logger ( "->" , "Target column found: {}" . format ( remove ) ) X_train = data [ 'train' ] y_train = y [ 'train' ] X_test = data [ 'test' ] y_test = y [ 'test' ] # classification_column = get_similar_column(getLabelwithInstruction(instruction), data) # Needed to make a custom label encoder due to train test split changes # Can still be inverse transformed, just a bit of extra work y_vals = np . unique ( pd . concat ( [ y [ 'train' ] , y [ 'test' ] ] , axis = 0 ) ) label_mappings = sklearn . preprocessing . LabelEncoder ( ) label_mappings . fit ( y_vals ) y_train = label_mappings . transform ( y_train ) y_test = label_mappings . transform ( y_test ) logger ( "Labels being mapped to appropriate classes" ) num_classes = len ( np . unique ( y ) ) # fitting and storing logger ( "Fitting Decision Tree" ) clf = tree . DecisionTreeClassifier ( criterion = criterion , splitter = splitter , max_depth = max_depth , min_samples_split = min_samples_split , min_samples_leaf = min_samples_leaf , min_weight_fraction_leaf = min_weight_fraction_leaf , max_leaf_nodes = max_leaf_nodes , min_impurity_decrease = min_impurity_decrease , ccp_alpha = ccp_alpha ) clf = clf . fit ( X_train , y_train ) score = accuracy_score ( clf . predict ( X_test ) , y_test ) logger ( "->" , "Score found on testing set: {}" . format ( score ) ) logger ( "Stored model under 'decision_tree' key" ) clearLog ( ) return { 'id' : generate_id ( ) , "model" : clf , "target" : remove , 'num_classes' : num_classes , "accuracy" : { 'cross_val_score' : cross_val_score ( clf , X_train , y_train , cv = 3 ) , 'accuracy_score' : score } , "accuracy_score" : score , "preprocessor" : full_pipeline , "interpreter" : label_mappings , 'test_data' : { 'X' : X_test , 'y' : y_test } }