<jupyter_start><jupyter_text>Lambda School Data Science*Unit 2, Sprint 3, Module 2*--- Permutation & BoostingYou will use your portfolio project dataset for all assignments this sprint. AssignmentComplete these tasks for your project, and document your work.- [ ] If you haven't completed assignment 1, please do so first.- [ ] Continue to clean and explore your data. Make exploratory visualizations.- [ ] Fit a model. Does it beat your baseline? - [ ] Try xgboost.- [ ] 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.But, if you aren't ready to try xgboost and permutation importances with your dataset today, that's okay. You can practice with another dataset instead. You may choose any dataset you've worked with previously.The data subdirectory includes the Titanic dataset for classification and the NYC apartments dataset for regression. You may want to choose one of these datasets, because example solutions will be available for each.<jupyter_code>import pandas as pd

df = pd.read_csv('data/steam.csv')
df.head()
#set target to owners 
target = 'owners'
df[target].value_counts(normalize=True)
# it looks like it will be a classification problem that is strongly distributed to the smaller values (tailing left)
# train, test, and val should be split by time using 'release_date'
df.head()
df['required_age'].value_counts()
df['steamspy_tags'].value_counts()
target = 'owners'
features = ['english', 'required_age', 'achievements', 'positive_ratings', 'negative_ratings',
       'average_playtime', 'median_playtime', 'price']
#to be cleaned later 'developer', 'publisher', 'genres', 'platforms', 'release_date', 
# features that should be dropped include:
# 'appid', 'name', 'categories' and 'steamspy_tags' (as they're nearly the same as 'genre')
# need to encode genre tags so values are separated

# thankfully, it looks like with the target I've selected there will be no time-travelling in my model
from sklearn.model_selection import train_test_split

# get train and test sets from the original dataframe
X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42)
print(X_train.shape, X_test.shape)

# get val by splitting train again
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
print(X_train.shape, X_val.shape)
#make some exploratory visualizations
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
ax = sns.pairplot(df[features])
for feature in features:
    sns.scatterplot(x=feature, y=target, data=df, alpha=0.1)
    plt.show()
ax = sns.distplot(df[target].value_counts())
ax = sns.distplot(y)
y = df['price']
y_log = np.log1p(y)
ax = sns.distplot(y_log)
# determine a baseline
df[target].value_counts(normalize=True)
#fit a random forest classifier
import category_encoders as ce
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier

RFC_pipeline = make_pipeline(
    ce.OrdinalEncoder(),
    SimpleImputer(strategy='mean'),
    RandomForestClassifier(n_estimators=100, random_state=42, n_jobs = -1)
)
RFC_pipeline.fit(X_train, y_train)
print('Validation accuracy', RFC_pipeline.score(X_val, y_val))
#try XGBoost!
from xgboost import XGBClassifier

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

pipeline.fit(X_train, y_train)
from sklearn.metrics import accuracy_score

y_pred = pipeline.predict(X_val)
print('Validation Accuracy', accuracy_score(y_val, y_pred))
#Get Permutation importance for the features
transformer = make_pipeline(
    ce.OrdinalEncoder(),
    SimpleImputer(strategy='median')
)

X_train_t = transformer.fit_transform(X_train)
X_val_t = transformer.fit_transform(X_val)

RFC_prep_model = RandomForestClassifier(n_estimators=100, random_state=42, n_jobs = -1)
RFC_prep_model.fit(X_train_t, y_train)
import eli5
from eli5.sklearn import PermutationImportance

permuter = PermutationImportance(
    RFC_prep_model,
    scoring='accuracy',
    n_iter=5,
    random_state=42,
)

permuter.fit(X_val_t, y_val)
f_names = X_val.columns.tolist()
pd.Series(permuter.feature_importances_, f_names).sort_values(ascending=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09282796187017703, 'fraction_numerical': 0.008624602814344077, 'mean_word_length': 5.929245283018868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '543355', 'license': 'MIT', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1390, 'n_words': 440, 'path': 'module2/Eric_Ramon_DS_232_assignment.ipynb', 'repo_name': 'nephylum/DS-Unit-2-Applied-Modeling', 'size': '4406'}