<jupyter_start><jupyter_text>Ensemble Methods - Random Forst  Business CasesSource: [Behavioral Risk Factor Surveillance System](https://www.kaggle.com/cdc/behavioral-risk-factor-surveillance-system)<jupyter_code>import itertools
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve<jupyter_output><empty_output><jupyter_text>Setup & Options<jupyter_code>plt.style.use('fivethirtyeight')
plt.rcParams['font.size'] = 18
images_dir = 'images'
seed = 50<jupyter_output><empty_output><jupyter_text>Data Mining<jupyter_code>df = pq.read_table('data/public-health-surveys_2011-2015.parquet').to_pandas()
df.head()
labels = np.array(df.pop('label'))
labels
X_train, X_test, y_train, y_test = train_test_split(df,
                                                    labels,
                                                    stratify = labels,
                                                    test_size = .3,
                                                    random_state = seed)
X_train.shape
X_test.shape
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>X_train = X_train.fillna(X_train.mean())
X_test = X_test.fillna(X_test.mean())<jupyter_output><empty_output><jupyter_text>Feature Selection<jupyter_code>n_nodes = []
max_depths = []
clf = RandomForestClassifier(n_estimators = 100,
                             random_state = seed,
                             max_features = 'sqrt',
                             n_jobs = 1,
                             verbose = 1)
clf.fit(X_train, y_train)
for independent_tree in clf.estimators_:
    tree = independent_tree.tree_
    n_nodes.append(tree.node_count)
    max_depths.append(tree.max_depth)
print(f'Average number of nodes: {int(np.mean(n_nodes))}')
print(f'Average maximum depth: {int(np.mean(max_depths))}')<jupyter_output>Average maximum depth: 46<jupyter_text>Predictive Modelling  Training PredictionsPredicting on training data is done to demonstrate overfitting.<jupyter_code>training_predictions = clf.predict(X_train)
training_propabilities = clf.predict_proba(X_train)[:, 1]
predictions = clf.predict(X_test)
propabilities = clf.predict_proba(X_test)[:, 1]<jupyter_output>[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 100 out of 100 | elapsed:    1.3s finished<jupyter_text>Evaluation<jupyter_code>def plot(base_fpr, base_tpr, model_fpr, model_tpr):
    plt.figure(figsize=(8,6))
    plt.rcParams['font.size'] = 16
    
    plt.plot(base_fpr, base_tpr, 'b', label='baseline')
    plt.plot(model_fpr, model_tpr, 'r', label='model')
    plt.legend()
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curves')
    plt.show()
def stats(recall: float, precision: float, roc: float) -> dict:
    return dict(recall=recall, precision=precision, roc=roc)
def evaluate_model(predictions, probs, train_predictions, train_probs):
    """
    Compare machine learning model to baseline performance.
    Computes statistics and shows ROC curve.
    """

    baseline = stats(recall_score(y_test, [1 for _ in range(len(y_test))]),
                     precision_score(y_test, [1 for _ in range(len(y_test))]),
                     .5)
    results = stats(recall_score(y_test, predictions),
                    precision_score(y_test, predictions),
                    roc_auc_score(y_test, probs))
    train_results = stats(recall_score(y_train, train_predictions),
                          precision_score(y_train, train_predictions),
                          roc_auc_score(y_train, train_probs))

    for metric in ['recall', 'precision', 'roc']:
        print(
            f'{metric.capitalize()}' + 
            f'\n\tBaseline: {round(baseline[metric], 2)} ' +
            f'\n\tTest: {round(results[metric], 2)} ' +
            f'\n\tTrain: {round(train_results[metric], 2)}')

    # Calculate false positive rates and true positive rates
    base_fpr, base_tpr, _ = roc_curve(y_test, [1 for _ in range(len(y_test))])
    model_fpr, model_tpr, _ = roc_curve(y_test, probs)

    # Plot both curves
    plot(base_fpr, base_tpr, model_fpr, model_tpr)
evaluate_model(predictions, propabilities, training_predictions, training_propabilities)
def plot_confusion_matrix(cm,
                          classes,
                          normalize=False,
                          title='Confusion Matrix',
                          cmap=plt.cm.Oranges):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    Source: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    # Plot the confusion matrix
    plt.figure(figsize = (10, 10))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title, size = 24)
    plt.colorbar(aspect=4)
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45, size = 14)
    plt.yticks(tick_marks, classes, size = 14)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    
    # Labeling the plot
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt), fontsize = 20,
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")
        
    plt.grid(None)
    plt.tight_layout()
    plt.ylabel('True label', size = 18)
    plt.xlabel('Predicted label', size = 18)
# Confusion matrix
cm = confusion_matrix(y_test, predictions)
plot_confusion_matrix(cm, classes = ['Poor Health', 'Good Health'],
                      title = 'Health Confusion Matrix')

plt.savefig(f'{images_dir}/cm.png')<jupyter_output>Confusion matrix, without normalization
{'fraction_non_alphanumeric': 0.11144435674822416, 'fraction_numerical': 0.010734017363851617, 'mean_word_length': 3.1196358907672304, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '562861', 'license': 'MIT', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2002, 'n_words': 413, 'path': '001_codealong.ipynb', 'repo_name': 'tigerarcades/nf-datascience-ensemble_methods_001-exercises', 'size': '6335'}