<jupyter_start><jupyter_text>EDA / Data Viz<jupyter_code>earthquake = pd.merge(train_values.head(int(len(train_values) * 0.1)), train_labels.head(int(len(train_labels) * 0.1)), on = 'building_id')
earthquake
# referenced https://seaborn.pydata.org/generated/seaborn.catplot.html

sns.catplot(x = "foundation_type", data = earthquake, kind = "count", hue = 'damage_grade').set(xlabel = 'Foundation Type', ylabel = 'Frequency');
plt.suptitle('Damage Grade by Foundation Type', y = 1.05);
# referenced https://seaborn.pydata.org/generated/seaborn.catplot.html

sns.countplot(x = "damage_grade", data = earthquake)
plt.xlabel('Damage Grade')
plt.ylabel('Frequency')
plt.title('Damage Grade Count');<jupyter_output><empty_output><jupyter_text>----  Label Encode<jupyter_code># Label Encode categorical features

le = LabelEncoder()
train_enc = train_values.apply(le.fit_transform)
train_enc<jupyter_output><empty_output><jupyter_text>Modeling with 10% of data- For faster processing<jupyter_code># grab first 10% of rows

train_enc_10pct = train_enc.head(int(len(train_values) * 0.1))
train_labels_10pct = train_labels.head(int(len(train_labels) * 0.1))<jupyter_output><empty_output><jupyter_text>Baseline + TTS<jupyter_code># baseline model

train_labels_10pct['damage_grade'].value_counts(normalize = True)
# establish X + y

X = train_enc_10pct.drop(columns = ['building_id'])
y = train_labels_10pct['damage_grade']
# tts

X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y, random_state = 123)<jupyter_output><empty_output><jupyter_text>Modeling<jupyter_code># Random Forest

pipe_forest = make_pipeline(StandardScaler(), RandomForestClassifier(n_jobs = -1, random_state = 123))

params = {'randomforestclassifier__max_depth' : [6, 7, 8, 9, 10, 11],
          'randomforestclassifier__max_features' : [15, 20, 30, 35]}

grid_forest = GridSearchCV(pipe_forest, param_grid = params)

grid_forest.fit(X_train, y_train)

print(f'Train Score: {grid_forest.score(X_train, y_train)}')
print(f'Test Score: {grid_forest.score(X_test, y_test)}')

grid_forest.best_params_
# grab feature importances -- CONSOLIDATED

forest_fi_df = pd.DataFrame({'importances': grid_forest.best_estimator_.named_steps['randomforestclassifier'].feature_importances_, 
                             'name': X_train.columns}).sort_values('importances', ascending = False)
forest_fi_df[:5]
# extract permutation importances
pimports = permutation_importance(grid_forest.best_estimator_, X_test, y_test, n_repeats = 10, n_jobs = -1)
#sort by averages
sort_idx = pimports.importances_mean.argsort()
#create a Dataframe sorted by importance
imp_df = pd.DataFrame(pimports.importances[sort_idx].T, columns = X_test.columns[sort_idx])
imp_df
#draw a boxplot -- first 10 features

imp_df.iloc[:, :10].plot(kind = 'box', vert = False)
# next 10 features

imp_df.iloc[:, 10:20].plot(kind = 'box', vert = False)
# next 10 features

imp_df.iloc[:, 20:30].plot(kind = 'box', vert = False)
# last 8 features

imp_df.iloc[:, 30:-1].plot(kind = 'box', vert = False)
# confusion matrix
plot_confusion_matrix(grid_forest, X_test, y_test, cmap = 'mako');<jupyter_output><empty_output><jupyter_text>- True Positives:     - Damage Grade 1: 207    - Damage Grade 2: 3,085    - Damage Grade 3: 1,196<jupyter_code># Extra Trees

pipe_trees = make_pipeline(StandardScaler(), ExtraTreesClassifier(n_jobs = -1, random_state = 123))

params = {'extratreesclassifier__max_depth' : [6, 7, 8, 9, 10, 11],
          'extratreesclassifier__max_features' : [15, 20, 30, 35]}

grid_trees = GridSearchCV(pipe_trees, param_grid = params)

grid_trees.fit(X_train, y_train)

print(f'Train Score: {grid_trees.score(X_train, y_train)}')
print(f'Test Score: {grid_trees.score(X_test, y_test)}')

grid_trees.best_params_
# grab feature importances -- CONSOLIDATED

trees_fi_df = pd.DataFrame({'importances': grid_trees.best_estimator_.named_steps['extratreesclassifier'].feature_importances_, 
                             'name': X_train.columns}).sort_values('importances', ascending = False)
trees_fi_df[:5]
# confusion matrix
plot_confusion_matrix(grid_trees, X_test, y_test, cmap = 'Reds');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1342925659472422, 'fraction_numerical': 0.02565947242206235, 'mean_word_length': 6.279232111692845, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '560155', 'license': 'CC0-1.0', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1500, 'n_words': 282, 'path': 'Workspace/veronica_le_all_cols.ipynb', 'repo_name': 'vleong1/modeling_earthquake_damage', 'size': '4170'}