<jupyter_start><jupyter_text>Heuristic 1: All cars pass<jupyter_code>y_pred_h1 = np.ones((len(y),), dtype=int)
y_pred_h1[0:10]<jupyter_output><empty_output><jupyter_text>Heuristic 2: All cars older than 14 years fail<jupyter_code>my_mode = df_clean[df_clean['RESULT'] == 0].mode()['VEHICLE_AGE'].iloc[0]
my_mean = df_clean[df_clean['RESULT'] == 0].mean()['VEHICLE_AGE']
print(f"mode age: {my_mode}, mean age: {my_mean}")
y_pred_h2 = (df_clean['VEHICLE_AGE'] < 15).astype(int)<jupyter_output><empty_output><jupyter_text>Decision tree classifier with 'VEHICLE_AGE' and 'MILE_YEAR' features<jupyter_code>DT = DecisionTreeClassifier(max_depth=10)
DT.fit(Xtrain, ytrain)
DT.score(Xtrain, ytrain)
DT2 = DecisionTreeClassifier()
DT2.fit(Xtrain, ytrain)
DT2.score(Xtrain, ytrain)
print(f"DT feature importance: {dict(zip(Xtrain.columns, DT.feature_importances_))}")
print(f"DT2 feature importance: {dict(zip(Xtrain.columns, DT2.feature_importances_))}")
print(f"DT depth {DT.get_depth()}")
print(f"DT2 depth {DT2.get_depth()}")
#"The importance of a feature is computed as the (normalized) total reduction of the criterion brought by that feature.
#It is also known as the Gini importance."(sklearn)
y_pred_DT = DT.predict(X)
y_pred_DT2 = DT2.predict(X)<jupyter_output><empty_output><jupyter_text>A quick reminder on what precision and recall are:accuracy = total_correct_predictions / total_predictions precision = true_positives / (true_positives + false_positives) recall = true_positives / (true_positives + false_negatives) F-Measure = (2 * Precision * Recall) / (Precision + Recall)   Note: For our purposes, a 'positive' is when a car fails!<jupyter_code>precision_h1 = precision_score(y, y_pred_h1, average='binary', pos_label=0)
precision_h2 = precision_score(y, y_pred_h2, average='binary', pos_label=0)
precision_DT = precision_score(y, y_pred_DT, average='binary', pos_label=0)
precision_DT2 = precision_score(y, y_pred_DT2, average='binary', pos_label=0)
recall_h1 = recall_score(y, y_pred_h1, average='binary', pos_label=0)
recall_h2 = recall_score(y, y_pred_h2, average='binary', pos_label=0)
recall_DT = recall_score(y, y_pred_DT, average='binary', pos_label=0)
recall_DT2 = recall_score(y, y_pred_DT2, average='binary', pos_label=0)
accuracy_h1 = accuracy_score(y, y_pred_h1)
accuracy_h2 = accuracy_score(y, y_pred_h2)
accuracy_DT = accuracy_score(y, y_pred_DT)
accuracy_DT2 = accuracy_score(y, y_pred_DT2)
data = {"precision": [precision_h1, precision_h2, precision_DT, precision_DT2], "recall": [recall_h1, recall_h2, 
                                                                                           recall_DT, recall_DT2],
       "accuracy": [accuracy_h1, accuracy_h2, accuracy_DT, accuracy_DT2]}
df_prec_rec = pd.DataFrame(data=data, index=["Heuristic_1", "Heuristic_2", "Decision Tree", "Decision Tree 2"])
df_prec_rec
plot_confusion_matrix(DT, Xtrain, ytrain, display_labels=["Fail", "Pass"])
plot_confusion_matrix(DT2, Xtrain, ytrain, display_labels=["Fail", "Pass"])
plot_confusion_matrix(DT, Xtest, ytest, display_labels=["Fail", "Pass"])
plot_confusion_matrix(DT2, Xtest, ytest, display_labels=["Fail", "Pass"])
ytest.value_counts()
path = DT2.cost_complexity_pruning_path(Xtrain, ytrain)
ccp_alphas = path.ccp_alphas
ccp_alphas = ccp_alphas[:-1]
len(ccp_alphas)
#path = DT2.cost_complexity_pruning_path(Xtrain, ytrain)
#ccp_alphas = path.ccp_alphas
#ccp_alphas = ccp_alphas[:-1]

#this takes my computer an hour

DTS = []
counter = 0
for ccp_alpha in ccp_alphas:
    dt = DecisionTreeClassifier(random_state=0, ccp_alpha=ccp_alpha)
    dt.fit(Xtrain, ytrain)
    DTS.append(DT)
    print(counter)
    counter+=1
train_scores = [dt.score(Xtrain, ytrain) for dt in DTS]
test_scores = [dt.score(Xtest, ytest) for dt in DTS]
fig, ax = plt.subplots()
ax.set_xlabel("alpha")
ax.set_label("accuracy")
ax.set_title("Accuracy vs alpha for training and testing sets")
ax.plot(ccp_alphas, train_scores, marker='o', label='train', drawstyle="steps-post")
ax.plot(ccp_alphas, test_scores, marker='o', label='test', drawstyle="steps-post")
ax.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13437195715676728, 'fraction_numerical': 0.01801363193768257, 'mean_word_length': 6.8565965583174, 'pattern_counts': {'":': 3, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '371330', 'license': 'MIT', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1540, 'n_words': 303, 'path': 'notebooks/baseline_model_eeva.ipynb', 'repo_name': 'Guli-Y/wimlds_emissions', 'size': '4108'}