<jupyter_start><jupyter_text>Lambda School Data Science, Unit 2: Predictive Modeling Applied Modeling, Module 2You will use your portfolio project dataset for all assignments this sprint. AssignmentComplete these tasks for your project, and document your work.- [X] Plot the distribution of your target.     - Classification problem: Are your classes imbalanced? Then, don't use just accuracy.    - Regression problem: Is your target skewed? If so, let's discuss in Slack.- [X] Continue to clean and explore your data. Make exploratory visualizations.- [X] Fit a model. Does it beat your baseline?- [X] Try xgboost.- [X] Get your model's permutation importances.You should try to complete an initial model today, because the rest of the week, we're making model interpretation visualizations. ReadingTop recommendations in _**bold italic:**_ Permutation Importances- _**[Kaggle / <NAME>: Machine Learning Explainability](https://www.kaggle.com/dansbecker/permutation-importance)**_- [<NAME>: Interpretable Machine Learning](https://christophm.github.io/interpretable-ml-book/feature-importance.html) (Default) Feature Importances  - [<NAME>: Selecting good features, Part 3, Random Forests](https://blog.datadive.net/selecting-good-features-part-iii-random-forests/)  - [Terence Parr, et al: Beware Default Random Forest Importances](https://explained.ai/rf-importance/index.html) Gradient Boosting  - [A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning](https://machinelearningmastery.com/gentle-introduction-gradient-boosting-algorithm-machine-learning/)  - _**[A Kaggle Master Explains Gradient Boosting](http://blog.kaggle.com/2017/01/23/a-kaggle-master-explains-gradient-boosting/)**_  - [_An Introduction to Statistical Learning_](http://www-bcf.usc.edu/~gareth/ISL/ISLR%20Seventh%20Printing.pdf) Chapter 8  - [Gradient Boosting Explained](http://arogozhnikov.github.io/2016/06/24/gradient_boosting_explained.html)  - _**[Boosting](https://www.youtube.com/watch?v=GM3CDQfQ4sw) (2.5 minute video)**_<jupyter_code>from google.colab import files
uploaded = files.upload()
!unzip new-york-city-airbnb-open-data.zip
import pandas as pd

df = pd.read_csv('AB_NYC_2019.csv')
df
import pandas_profiling

pandas_profiling.ProfileReport(df)
# baseline mae

from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import numpy as np

y = df['price']
y_pred = [y.mean()] * len(y)

print('Mean Baseline:')
print('Mean Absolute Error:', mean_absolute_error(y, y_pred))
from sklearn.model_selection import train_test_split

train, test = train_test_split(df, train_size=0.80, test_size=0.20,
                              random_state=42)
train, val = train_test_split(train, train_size=0.80, test_size=0.20, 
                             random_state=42)
def wrangle(X):
  X = X.copy()
  
  cols_with_zeros = ['number_of_reviews', 'availability_365', 'reviews_per_month']
  for col in cols_with_zeros:
        X[col] = X[col].replace(0, np.nan)
        X[col+'_MISSING'] = X[col].isnull()
        
  # Drop id (always varies, random)
  unusable_variance = ['id', 'name']
  X = X.drop(columns=unusable_variance)
  
  # return the wrangled dataframe
  return X 
train = wrangle(train)
val = wrangle(val)
test = wrangle(test)
# Arrange into X features matrix and y vector target

target = 'price'
X_train = train.drop(columns=target)
y_train = train[target]
X_val = val.drop(columns=target)
y_val = val[target]
X_test = test.drop(columns=target)
y_test = test[target]
!pip install category_encoders
import category_encoders as ce
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import make_pipeline

pipeline = make_pipeline(
    ce.OrdinalEncoder(), 
    SimpleImputer(strategy='mean'), 
    RandomForestRegressor(n_estimators=100, random_state=42, n_jobs=-1)
)

# Fit on train, score on val
pipeline.fit(X_train, y_train)
print('Validation R^2:', -pipeline.score(X_val, y_val))
y_pred = pipeline.predict(X_val)
print('Validation MAE:', mean_absolute_error(y_val, y_pred))
# YAY! Validation MAE improved on baseline by $20!!
from xgboost import XGBRegressor

pipeline = make_pipeline(
    ce.OrdinalEncoder(),
    XGBRegressor(n_estimators=100, random_state=42, n_jobs=-1)
)

pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_val)
print('Validation MAE:', mean_absolute_error(y_val, y_pred))
# Get feature importances
rf = pipeline.named_steps['xgbregressor']
importances = pd.Series(rf.feature_importances_, X_train.columns)

# Plot feature importances
%matplotlib inline
import matplotlib.pyplot as plt

n=10
plt.figure(figsize=(10, n/2))
plt.title(f'Top {n} features')
importances.sort_values()[-n:].plot.barh(color='grey')
plt.show()
feature = 'room_type'
X_val[feature].head()
X_val[feature].value_counts(normalize=True)
import numpy as np

X_val_permuted = X_val.copy()
X_val_permuted[feature] = np.random.permutation(X_val[feature])
X_val_permuted[feature].head()
X_val_permuted[feature].value_counts(normalize=True)
# Get the permutation importances
# No need to refit pipeline

score_with = mean_absolute_error(y_val, y_pred)
y_pred2 = pipeline.predict(X_val_permuted)

score_without = mean_absolute_error(y_val, y_pred2)

print(f'Validation MAE with {feature}: {score_with}')
print(f'Validation MAE with {feature} permuted: {score_without}')
print(f'Permutation Importance: {score_with - score_without}')
feature = 'latitude'

X_val_permuted = X_val.copy()
X_val_permuted[feature] = np.random.permutation(X_val[feature])

score_with = mean_absolute_error(y_val, y_pred)
y_pred3 = pipeline.predict(X_val_permuted)

score_without = mean_absolute_error(y_val, y_pred3)

print(f'Validation MAE with {feature}: {score_with}')
print(f'Validation MAE with {feature} permuted: {score_without}')
print(f'Permutation Importance: {score_with - score_without}')
import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(10,10))
ax = sns.distplot(df['price'] )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11615154536390827, 'fraction_numerical': 0.013459621136590228, 'mean_word_length': 5.72513966480447, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 6, 'lorem ipsum': 0, 'www.': 3, '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': '432849', 'license': 'MIT', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2013, 'n_words': 481, 'path': 'module2/assignment_applied_modeling_2.ipynb', 'repo_name': 'ocalzada/DS-Unit-2-Applied-Modeling', 'size': '6037'}