<jupyter_start><jupyter_text>Decision Tree ClassifierIn this section, we do a statistical modelling of the data using Decision Tree.<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn import metrics
from sklearn.metrics import classification_report, roc_curve, confusion_matrix
from sklearn.model_selection import cross_val_score
from sklearn.metrics import roc_auc_score
from sklearn.tree import DecisionTreeClassifier
# Read the accident data from csv
df_traffic = pd.read_csv('data_wrangled.csv', 
                         index_col = 'Date Of Stop_Time Of Stop',
                         parse_dates = True,
                         infer_datetime_format = True)

df_traffic['V_sage'] = (df_traffic.V_Age - df_traffic.V_Age.min())/\
                  (df_traffic.V_Age.max() - df_traffic.V_Age.min())

not_relevant = ['Latitude', 'Longitude', 'Year', 'Description', 
                'Make', 'Model', 'Color', 'datetime', 'V_Age']
df_traffic.drop(labels = not_relevant, axis = 1,  inplace = True)
# Get X and y from data 
y = df_traffic['Violation Type_Warning']
df_X1 = df_traffic.drop('Violation Type_Warning', axis = 1)
X = df_X1.values

# now,  do a Logistic regression to the data. 
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                   test_size = 0.5, 
                                    random_state=77)
# Setup the hyperparameter grid 
param_grid = {'max_depth': [39], 
             'min_samples_leaf': np.arange(1,500,2), 
             'min_weight_fraction_leaf': [0], 
             'random_state' : [77], 'class_weight': ['balanced']}

# Instantiate a model
model_tree = DecisionTreeClassifier()

# Instantiate the GridSearchCV object: 
model_cv = RandomizedSearchCV(model_tree, param_grid, 
                              cv = 5, 
                              n_jobs = -1)

# Fit it to the data
model_cv.fit(X, y)

# Print the tuned parameters and score
print("Tuned Decision Tree Parameters: {}".format(model_cv.best_params_)) 
print("Best score is {}".format(model_cv.best_score_))
model_tree = DecisionTreeClassifier(max_depth = 39, 
                                    min_weight_fraction_leaf = 0,
                                    min_samples_leaf = 127,
                                    class_weight = 'balanced',
                                    random_state = 77)

model_tree.fit(X_train, y_train)

y_pred = model_tree.predict(X_test)

print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred,  target_names = ['Citaton', 'Warning']))

y_pred_prob = model_tree.predict_proba(X_test)[:,1]

fpr_dt,tpr_dt, thresholds = roc_curve(y_test, y_pred_prob)

print("AUC: {}".format(roc_auc_score(y_test, y_pred_prob)))

plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_dt, tpr_dt, 'g',label = 'Decision Tree Classifier')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.legend()
plt.show()

from joblib import dump, load
dump(model_tree, 'Decision_tree.joblib')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10516372795969774, 'fraction_numerical': 0.010390428211586901, 'mean_word_length': 3.036848792884371, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '322593', 'license': 'Condor-1.1', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1008, 'n_words': 243, 'path': 'Decision Tree Classifier.ipynb', 'repo_name': 'lekhnath12/Capstone-project-I', 'size': '3176'}