<jupyter_start><jupyter_text>Bulldozer sales Price > Tree based model to predict bulldozer sales price<jupyter_code>import os
os.listdir()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv('TrainAndValid.csv')
pd.options.display.max_columns = 100
df.head()
df['SalePrice'].hist()
df.select_dtypes('object').columns
df['saledate'] = pd.to_datetime(df['saledate'])
df['saleYear'] = df['saledate'].dt.year
df['saleMonth'] = df['saledate'].dt.month
df.drop(['saledate'],axis=1,inplace=True)
df.shape
df['YearMade']
cat_cols = list(df.select_dtypes('object').columns)
print(cat_cols)
from sklearn.preprocessing import LabelEncoder

for col in cat_cols:
    df[col] = pd.Categorical(df[col]).codes + 1
df = df.fillna(df.median())<jupyter_output><empty_output><jupyter_text>Train, Test Split<jupyter_code>from sklearn.model_selection import train_test_split
X_train,X_val,y_train,y_val = train_test_split(df.drop('SalePrice',axis=1),df['SalePrice'])
X_train.shape, X_val.shape
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X_train.iloc[:20000], y_train.iloc[:20000])
model.score(X_val,y_val)
model = RandomForestRegressor(n_estimators=40, max_features=0.5, min_samples_leaf=3)
model.fit(X_train, y_train)
model.score(X_val,y_val)<jupyter_output><empty_output><jupyter_text>Interpretation<jupyter_code>from pdpbox import pdp, get_dataset, info_plots

X_train_samp = X_train[X_train['YearMade']>1920]
pdp_year = pdp.pdp_isolate(
    model=model, dataset=X_train_samp, model_features=X_train_samp.columns, feature='YearMade'
)
fig, axes = pdp.pdp_plot(pdp_year, 'YearMade')
X_train_samp.shape
import shap
explainer = shap.TreeExplainer(model)
X_train_less = X_train_samp.sample(500)
X_train_less.shape
shap_values = explainer.shap_values(X_train_less)
shap.summary_plot(shap_values, X_train_less)
# shap.summary_plot(shap_values, X_train_less, plot_type="bar")
%%time
inter_rf = pdp.pdp_interact(
    model=model, dataset=X_train_less, model_features=X_train_less.columns, 
    features=['YearMade', 'saleYear']
)
fig, axes = pdp.pdp_interact_plot(
    inter_rf, ['YearMade', 'saleYear'], x_quantile=True, plot_type='grid', plot_pdp=True,
    figsize=(13,13)
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13226366001734605, 'fraction_numerical': 0.013876843018213356, 'mean_word_length': 8.6125, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '660019', 'license': 'Apache-2.0', 'n_tokens_mistral': 937, 'n_tokens_neox': 894, 'n_words': 139, 'path': '_notebooks/2020-04-08-bulldozers.ipynb', 'repo_name': 'supriyamk2/Learnings', 'size': '2306'}