<jupyter_start><jupyter_text>Lambda School Data Science*Unit 2, Sprint 3, Module 3*--- Model Interpretation 1You will use your portfolio project dataset for all assignments this sprint. AssignmentComplete these tasks for your project, and document your work.- [ ] Continue to iterate on your project: data cleaning, exploration, feature engineering, modeling.- [ ] Make at least 1 partial dependence plot to explain your model.- [ ] Share at least 1 visualization on Slack.If you have not yet completed an initial model yet for your portfolio project, you can do today's assignment using your Tanzania Waterpumps model. An example solution will be provided.**Multi-class classification** will result in multiple Partial Dependence Plots (one for each class). Stretch Goals- [ ] Make multiple PDPs with 1 feature in isolation.- [ ] Make multiple PDPs with 2 features in interaction. - [ ] Use Plotly to make a 3D PDP.- [ ] Make PDPs with categorical feature(s). Use Ordinal Encoder, outside of a pipeline, to encode your data first. If there is a natural ordering, then take the time to encode it that way, instead of random integers. Then use the encoded data with pdpbox. Get readable category names on your plot, instead of integer category codes. Links- [<NAME>: Interpretable Machine Learning — Partial Dependence Plots](https://christophm.github.io/interpretable-ml-book/pdp.html) + [animated explanation](https://twitter.com/ChristophMolnar/status/1066398522608635904)- [Kaggle / <NAME>: Machine Learning Explainability — Partial Dependence Plots](https://www.kaggle.com/dansbecker/partial-plots)- [Plotly: 3D PDP example](https://plot.ly/scikit-learn/plot-partial-dependence/partial-dependence-of-house-value-on-median-age-and-average-occupancy)<jupyter_code>%%capture
import sys

# If you're on Colab:
if 'google.colab' in sys.modules:
    DATA_PATH = 'https://raw.githubusercontent.com/LambdaSchool/DS-Unit-2-Applied-Modeling/master/data/'
    !pip install category_encoders==2.*
    !pip install eli5
    !pip install pdpbox

# If you're working locally:
else:
    DATA_PATH = '../data/'
import pandas as pd
from sklearn.model_selection import train_test_split

train = pd.read_csv('sample_data/california_housing_train.csv')
test = pd.read_csv('sample_data/california_housing_test.csv')

train, val = train_test_split(train, train_size=0.8)
train.head()
test.head()
target = 'median_house_value'
features = train.columns.drop(target)

X_train = train[features]
y_train = train[target]

X_val = val[features]
y_val = val[target]

X_test = test[features]
from sklearn.metrics import r2_score
from xgboost import XGBRegressor
from sklearn.pipeline import make_pipeline

pipeline = make_pipeline(
    XGBRegressor(n_estimators=200, objective='reg:squarederror', n_jobs=-1, random_state=42)
)

pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_val)
print(f'Gradient Boosting R^2 Score: {r2_score(y_val, y_pred)}')
import matplotlib.pyplot as plt
import seaborn as sns
from pdpbox.pdp import pdp_isolate, pdp_plot

sns.set()
plt.rcParams['figure.dpi'] = 72

feature = 'median_income'

isolated = pdp_isolate(
    model = pipeline,
    dataset = X_val,
    model_features = features,
    feature = feature
)

pdp_plot(isolated, feature_name=feature, plot_lines=True, frac_to_plot=100)
plt.ylim(0, 375000)
from pdpbox.pdp import pdp_interact, pdp_interact_plot

feature = ['median_income', 'housing_median_age']

interaction = pdp_interact(
    model = pipeline,
    dataset = X_val,
    model_features = features,
    features = feature
)

pdp_interact_plot(interaction, plot_type='grid', feature_names=feature)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0960591133004926, 'fraction_numerical': 0.0150519978106185, 'mean_word_length': 5.215986394557823, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '303815', 'license': 'MIT', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1123, 'n_words': 351, 'path': 'module3/Stephen_P_LS_DS_233_assignment.ipynb', 'repo_name': 'FuriouStyles/DS-Unit-2-Applied-Modeling', 'size': '3668'}