<jupyter_start><jupyter_text>STAT451: Machine Learning -- L07: Ensemble Methods Part 1/2 STAT 451: Intro to Machine Learning (Fall 2021)  Instructor: <NAME> (<EMAIL>)<jupyter_code>%load_ext watermark
%watermark -p scikit-learn,mlxtend<jupyter_output>scikit-learn: 1.0
mlxtend     : 0.19.0<jupyter_text>Random Forests<jupyter_code>from sklearn import model_selection
from sklearn.model_selection import train_test_split
from sklearn import datasets


data = datasets.load_breast_cancer()
X, y = data.data, data.target

X_temp, X_test, y_temp, y_test = \
    train_test_split(X, y, test_size=0.3, random_state=123, stratify=y)

X_train, X_valid, y_train, y_valid = \
    train_test_split(X_temp, y_temp, test_size=0.2, random_state=123, stratify=y_temp)

print('Train/Valid/Test sizes:', y_train.shape[0], y_valid.shape[0], y_test.shape[0])
from sklearn.ensemble import RandomForestClassifier


forest = RandomForestClassifier(n_estimators=100,
                                random_state=1)

forest.fit(X_train, y_train)
    
print("Training Accuracy: %0.2f" % forest.score(X_train, y_train))
print("Validation Accuracy: %0.2f" % forest.score(X_valid, y_valid))
print("Test Accuracy: %0.2f" % forest.score(X_test, y_test))
from sklearn.ensemble import ExtraTreesClassifier


forest = ExtraTreesClassifier(n_estimators=100,
                              random_state=1)

forest.fit(X_train, y_train)
    
    
print("Training Accuracy: %0.2f" % forest.score(X_train, y_train))
print("Validation Accuracy: %0.2f" % forest.score(X_valid, y_valid))
print("Test Accuracy: %0.2f" % forest.score(X_test, y_test))<jupyter_output>Training Accuracy: 1.00
Validation Accuracy: 1.00
Test Accuracy: 0.98
{'fraction_non_alphanumeric': 0.11775147928994083, 'fraction_numerical': 0.036686390532544376, 'mean_word_length': 5.01779359430605, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '46567', 'license': 'MIT', 'n_tokens_mistral': 679, 'n_tokens_neox': 628, 'n_words': 123, 'path': '07-ensembles/code/07-05_random-forests.ipynb', 'repo_name': 'ajshedivy/stat451-machine-learning-fs21', 'size': '1711'}