<jupyter_start><jupyter_text>Student Name: <NAME><jupyter_code>#TODO: import the modules
from sklearn.model_selection import train_test_split 
from sklearn.tree import DecisionTreeClassifier 
from sklearn.model_selection import RandomizedSearchCV
from sklearn import metrics
from sklearn import tree
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Step 1 – Explain any overfitting or underfitting in the previous model There seemed to be a bit of underfitting in the previous model. While the tree seemed to have a deal of depth it mainly focused on a singular attribute of the data. While that singular attribute does contribute quite a bit there are subtle nuances in the other attributes, that can actually lead to dramatic differences in the resulting tip. Lacking these other attributes would definitely cause a problem in the accuracy.  Step 2 – Load the data and extract the features<jupyter_code>#TODO: load the data and extract the features: 'VendorID', RatecodeID', 'PULocationID', 'DOLocationID', 'passenger_count', 'trip_distance', 'fare_amount', 'extra', 'mta_tax', 'tolls_amount', 'improvement_surcharge', 'total_amount', 'trip_type', 'congestion_surcharge', 'tip'
features = ['VendorID', 'RatecodeID', 'PULocationID', 'DOLocationID',
            'passenger_count', 'trip_distance', 'fare_amount', 'extra',
            'mta_tax', 'tolls_amount', 'improvement_surcharge', 'total_amount',
            'trip_type', 'congestion_surcharge', 'tip']

file_path = 'https://raw.githubusercontent.com/davashu/module-05-decision-trees-part-2-clarkech95/master/trip_data.csv?token=<KEY>'
df = pd.read_csv(file_path, usecols = features)
X = df.iloc[: , 0:14]
y = df.iloc[: , 14]<jupyter_output><empty_output><jupyter_text>Step 3 – Split the dataThe test set should comprise 30% of the dataset. Ensure the data is randomized and that stratified samples are obtained.<jupyter_code>#TODO: Split the dataset into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3,
                                                   random_state = 48,
                                                   stratify = y) #TODO: fill in the required options<jupyter_output><empty_output><jupyter_text>Step 4 – Instantiate the Decision Tree Classifer<jupyter_code>dt_estimator = DecisionTreeClassifier()<jupyter_output><empty_output><jupyter_text>Step 5 – Create a dictionary that contains the decision tree parameters<jupyter_code>params = {
    'criterion': ['gini', 'entropy'], #a list of options for criterion.
    'max_depth': [7, 9, 14], #TODO: provide a list of three numbers between 5 and 15 inclusive.
    'min_samples_split':[114, 117, 144, 194, 213, 226, 333, 372, 427, 480] #TODO: provide a list of ten numbers between 100 and 500.
}<jupyter_output><empty_output><jupyter_text>Step 6 – Perform the Random searchProvide the dictionary from step 5 above and use the decison tree from step 4 as the estimator.<jupyter_code>rand_search = RandomizedSearchCV(estimator=dt_estimator, 
                                 param_distributions=params, 
                                 random_state=7, 
                                 scoring='accuracy', 
                                 verbose=3, #display messages while training
                                 n_iter= 74,#TODO: set n_iter to a value between 50 and 100
                                 cv= 10) #TODO: set the number of folds to 10 for the cross validation


estimator = rand_search.fit(X_train, y_train)


print('Best parameters:', estimator.best_params_)
print('Best score:', estimator.best_score_) #note: if the score is low, increase n_iter or adjust the values in step 5.<jupyter_output>C:\Users\clark\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py:282: UserWarning: The total space of parameters 60 is smaller than n_iter=74. Running 60 iterations. For exhaustive searches, use GridSearchCV.
  % (grid_size, self.n_iter, grid_size), UserWarning)<jupyter_text>Step 7 – Train the decision tree<jupyter_code>#Instantiate the Decision Tree Classifer object 
dt_classifier = DecisionTreeClassifier(criterion= 'entropy', #TODO: use the value from the random search results.
                                       max_depth= 14, #TODO: use the value from the random search results.
                                       min_samples_split = 114 #TODO: use the value from the random search results.
                                      )

#Train Decision Tree Classifer using the `fit` method
dt_classifier.fit(X_train, y_train)

#Make predictions with `X_test` and store the result in a variable called `y_pred
y_pred = dt_classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>Step 8 – Evaluate the accuracy<jupyter_code>#TODO: calculate and display the accuracy score using `y_test` and `y_pred`
metrics.accuracy_score(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>Step 9 – Visualize the decision tree<jupyter_code>#TODO: Display the decision tree
fig, ax = plt.subplots(figsize=(12, 12))
viz = tree.plot_tree(dt_classifier, feature_names = features, max_depth = 5, fontsize = 10)
viz<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08414985590778098, 'fraction_numerical': 0.020365033621517772, 'mean_word_length': 3.8337975858867224, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '540055', 'license': 'MIT', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1512, 'n_words': 510, 'path': 'DecisionTreePt2.ipynb', 'repo_name': 'clarkech95/decision-trees-pt2', 'size': '5239'}