<jupyter_start><jupyter_text>Adaboost Decision Stumps<jupyter_code>import sys, itertools
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import sklearn, time, math
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import StratifiedKFold
from sklearn.metrics import confusion_matrix

if '../tools' not in sys.path:
    sys.path.append('../tools')
from helper_functions import * 

import warnings
warnings.filterwarnings('ignore')
X, Y = load_all_fv(3,3)
accs = []
for n in range(50, 400, 50):
    stumps = AdaBoostClassifier(n_estimators=n, base_estimator=DecisionTreeClassifier(max_depth=1))
    skf = StratifiedKFold(Y, n_folds=8, shuffle=True, random_state=1)
    accs.append(np.average(cross_val_score(stumps, X, Y, cv=skf, n_jobs=-1)))
    print('finished', n)
plt.scatter(range(50, 400, 50), accs)
plt.xlabel('number of stumps')
plt.ylabel('score')
plt.title('AdaBoost Convergence (8-fold validation)')
plt.show()
max(accs)
trX, teX, trY, teY = train_test_split(X, Y, stratify=Y,
                                      random_state=1, test_size=(1/8))

stumps = AdaBoostClassifier(n_estimators=300, base_estimator=DecisionTreeClassifier(max_depth=1))
stumps.fit(trX, trY)

print('n_estimators {}, train data accuracy {}, test accuracy {}'.format(
        300, stumps.score(trX, trY), stumps.score(teX, teY)))
plot_confusion_matrix(confusion_matrix(teY, stumps.predict(teX)), title='Confusion Matrix for AdaBoost(300)')
plt.show()<jupyter_output>n_estimators 300, train data accuracy 0.3, test accuracy 0.24166666666666667
{'fraction_non_alphanumeric': 0.11037891268533773, 'fraction_numerical': 0.03130148270181219, 'mean_word_length': 5.9541984732824424, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '377462', 'license': 'Apache-2.0', 'n_tokens_mistral': 691, 'n_tokens_neox': 634, 'n_words': 144, 'path': 'notebooks/fv-decisionstumps.ipynb', 'repo_name': 'PrincetonUniversity/vox-music-classification', 'size': '1821'}