<jupyter_start><jupyter_text>_Lambda School Data Science — Tree Ensembles_  Decision Trees — with ipywidgets!  Notebook requirements- [ipywidgets](https://ipywidgets.readthedocs.io/en/stable/examples/Using%20Interact.html): works in Jupyter but [doesn't work on Google Colab](https://github.com/googlecolab/colabtools/issues/60issuecomment-462529981)- [mlxtend.plotting.plot_decision_regions](http://rasbt.github.io/mlxtend/user_guide/plotting/plot_decision_regions/): `pip install mlxtend`  Regressing a wave<jupyter_code>import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

# Example from http://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html
def make_data():
    import numpy as np
    rng = np.random.RandomState(1)
    X = np.sort(5 * rng.rand(80, 1), axis=0)
    y = np.sin(X).ravel()
    y[::5] += 2 * (0.5 - rng.rand(16))
    return X, y

X, y = make_data()

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42)

plt.scatter(X_train, y_train)
plt.scatter(X_test, y_test);
from sklearn.tree import DecisionTreeRegressor

def regress_wave(max_depth):
    tree = DecisionTreeRegressor(max_depth=max_depth)
    tree.fit(X_train, y_train)
    print('Train R^2 score:', tree.score(X_train, y_train))
    print('Test R^2 score:', tree.score(X_test, y_test))
    plt.scatter(X_train, y_train)
    plt.scatter(X_test, y_test)
    plt.step(X, tree.predict(X))
    plt.show()
from ipywidgets import interact
interact(regress_wave, max_depth=(1,8,1));<jupyter_output><empty_output><jupyter_text>Classifying a curve<jupyter_code>import numpy as np
curve_X = np.random.rand(1000, 2) 
curve_y = np.square(curve_X[:,0]) + np.square(curve_X[:,1]) < 1.0
curve_y = curve_y.astype(int)
from sklearn.linear_model import LogisticRegression
from mlxtend.plotting import plot_decision_regions

lr = LogisticRegression(solver='lbfgs')
lr.fit(curve_X, curve_y)
plot_decision_regions(curve_X, curve_y, lr, legend=False)
plt.axis((0,1,0,1));
from sklearn.tree import DecisionTreeClassifier

def classify_curve(max_depth):
    tree = DecisionTreeClassifier(max_depth=max_depth)
    tree.fit(curve_X, curve_y)
    plot_decision_regions(curve_X, curve_y, tree, legend=False)
    plt.axis((0,1,0,1))
    plt.show()
interact(classify_curve, max_depth=(1,8,1));<jupyter_output><empty_output><jupyter_text>Titanic survival, by age & fare<jupyter_code>import seaborn as sns
from sklearn.impute import SimpleImputer

titanic = sns.load_dataset('titanic')
imputer = SimpleImputer()
titanic_X = imputer.fit_transform(titanic[['age', 'fare']])
titanic_y = titanic['survived'].values
from sklearn.linear_model import LogisticRegression
from mlxtend.plotting import plot_decision_regions

lr = LogisticRegression(solver='lbfgs')
lr.fit(titanic_X, titanic_y)
plot_decision_regions(titanic_X, titanic_y, lr, legend=False);
plt.axis((0,75,0,175));
def classify_titanic(max_depth):
    tree = DecisionTreeClassifier(max_depth=max_depth)
    tree.fit(titanic_X, titanic_y)
    plot_decision_regions(titanic_X, titanic_y, tree, legend=False)
    plt.axis((0,75,0,175))
    plt.show()
interact(classify_titanic, max_depth=(1,8,1));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12709757613424488, 'fraction_numerical': 0.022374145431945307, 'mean_word_length': 6.366132723112128, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '7990', 'license': 'MIT', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1225, 'n_words': 212, 'path': 'module1-decision-trees/LS_DS_411B_ipywidgets_Decision_Trees.ipynb', 'repo_name': 'brit228/DS-Unit-4-Sprint-1-Tree-Ensembles', 'size': '3218'}