<jupyter_start><jupyter_text>Classfication Model<jupyter_code>brownfield_subset = brownfields[['Cleanup Required', 'Amt of Assessment Funding', 'Cntmnt Fnd-Arsenic',
                                 #'Past Use: Greenspace (arces)', 'Past Use: Residential (arces)',
                                 #'Past Use: Commercial (arces)', 'Past Use: Industrial (arces)',
                                 'Property  State',
                                 'Cntmnt Fnd-Asbestos', 'Cntmnt Fnd-Chromium', 'Cntmnt Fnd-Lead',
                                 'Cntmnt Fnd-Mercury', 'Cntmnt Fnd-PAHs', 'Cntmnt Fnd-PCBs', 
                                 'Cntmnt Fnd-Petroleum',
 'Cntmnt Fnd-SVOCs',
 'Cntmnt Fnd-VOCs',
 '2010 % Below Poverty',
                                 'ASSESSMENT YEAR'#,'2010 Median Income', '2010 % Low Income', '2010 % Vacant Housing', '2010 % Unemployed'
]]
brownfield_subset
brownfield_subset['Cleanup Required'].value_counts()
brownfield_subset['Cntmnt Fnd-Arsenic'].value_counts(dropna=False)
brownfield_subset['Cntmnt Fnd-Lead'].value_counts(dropna=False)
brownfield_subset['Property  State'].value_counts(dropna=False)
#brownfield_subset['Amount of Cleanup Funding'].value_counts(dropna=False)
#funding = brownfield_subset[['Amt of Assessment Funding', 'Amount of Cleanup Funding']]
#funding_clean = funding.dropna()
import seaborn as sns
#sns.set_theme(style="whitegrid")
ax = sns.scatterplot(x='Amt of Assessment Funding', y = 'Amount of Cleanup Funding', data=brownfields)
ax.set_xlim(0,500000)
ax.set_ylim(0,6000000)
brownfield_subset_dummy = pd.get_dummies(brownfield_subset, columns = ['Cntmnt Fnd-Arsenic',
 'Cntmnt Fnd-Asbestos',
 'Cntmnt Fnd-Chromium',
 'Cntmnt Fnd-Lead',
 'Cntmnt Fnd-Mercury',
 'Cntmnt Fnd-PAHs',
 'Cntmnt Fnd-PCBs',
 'Cntmnt Fnd-Petroleum',
 'Cntmnt Fnd-SVOCs',
 'Cntmnt Fnd-VOCs', 'Property  State'])
brownfield_subset_dummy
full_rows_brown = brownfield_subset_dummy.dropna(subset=['Amt of Assessment Funding', '2010 % Below Poverty', 
                                                         'ASSESSMENT YEAR'])
full_rows_brown['Cleanup Required'].value_counts()
full_rows_brown = full_rows_brown[(full_rows_brown['Cleanup Required']=='Y') |
                                 (full_rows_brown['Cleanup Required']=='N') |
                                  (full_rows_brown['Cleanup Required']=='U')]
full_rows_brown['Cleanup Required'] = full_rows_brown['Cleanup Required'].map({'Y': 1, 'N': 0, 'U':2})
full_rows_brown['Cleanup Required'].value_counts()/len(full_rows_brown)
full_rows_brown<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>X = full_rows_brown.drop('Cleanup Required', axis = 1)
y = full_rows_brown['Cleanup Required']

#42 = 9 in train set

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
X_train.shape
    if classifier_name == 'SVC':
        svc_c = trial.suggest_float('svc_c', 1e-10, 1e10, log=True)
        classifier_obj = sklearn.svm.SVC(C=svc_c, gamma='auto')
        
    if classifier_name == 'KNN':
        neigh = trial.suggest_int("n_neighbors", 1, 2000)
        classifier_obj = sklearn.neighbors.KNeighborsClassifier(n_neighbors = neigh)
#Step 1. Define an objective function to be maximized.
def objective(trial):

    classifier_name = trial.suggest_categorical("classifier", [ "RandomForest", 'KNN'])
    
    # Step 2. Setup values for the hyperparameters:
        
    if classifier_name == 'KNN':
        neigh = trial.suggest_int("n_neighbors", 1, 100)
        classifier_obj = sklearn.neighbors.KNeighborsClassifier(n_neighbors = neigh)
        
    if classifier_name == 'RandomForest':
        rf_n_estimators = trial.suggest_int("rf_n_estimators", 10, 300)
        rf_max_depth = trial.suggest_int("rf_max_depth", 2, 25, log=True)
        rf_max_features = trial.suggest_int("rf_max_features", 1, 60)
        rf_max_samples  = trial.suggest_int("rf_max_samples", 1, 20000)
        classifier_obj = ensemble.RandomForestClassifier(
            max_depth=rf_max_depth, n_estimators=rf_n_estimators,
            max_features=rf_max_features, max_samples=rf_max_samples
        )

    # Step 3: Scoring method:
    score = model_selection.cross_val_score(classifier_obj, X_train, y_train, n_jobs=-1, cv=5)
    accuracy = score.mean()
    return accuracy

# Step 4: Running it
study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=20)
# Getting the best trial:
print(f"The best trial is : \n{study.best_trial}")

# Getting the best score:
print(f"The best value is : \n{study.best_value}")

# Getting the best parameters:
print(f"The best parameters are : \n{study.best_params}")
randomForest_clf_optuna = RandomForestClassifier(n_estimators = study.best_params['rf_n_estimators'], 
                                                 max_depth=study.best_params['rf_max_depth'], 
                                                 max_features=study.best_params['rf_max_features'],
                                                 max_samples=study.best_params['rf_max_samples'], n_jobs=-1)
randomForest_clf_optuna.fit(X_train, y_train)
y_test_pred = randomForest_clf_optuna.predict(X_test)
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_test_pred)
from sklearn.metrics import plot_confusion_matrix
class_names = ['No Cleanup', 'Cleanup Required', 'Undetermined']
disp = plot_confusion_matrix(randomForest_clf_optuna, X_test, y_test,
                             display_labels=class_names,
                             cmap=plt.cm.Blues,
                             normalize=None)
plt.title('Confusion Matrix: Random Forest')

plt.show()
print(classification_report(y_test, y_test_pred, target_names=class_names))
# feature names for later use with importance
feature_list = list(X_train.columns)
#feature_list

# get importance
importance = randomForest_clf_optuna.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
	print('Feature: %s, Score: %.5f' % (feature_list[i],v))
# plot feature importance
plt.figure(figsize=(15,6))
pyplot.bar(feature_list, importance)
pyplot.xticks(rotation=90)
pyplot.ylabel('Relative Feature Importance')
pyplot.title('Variable/Feature Importance')
pyplot.show()
#svc_disp = plot_roc_curve(randomForest_clf_optuna, X_test, y_test)
#plt.plot([0,1], [0,1], 'k--') #dashed diagonal
#svc_disp_opt = plot_roc_curve(randomForest_clf_optuna, X_test, y_test)

#plt.show()
# AUC Friday night: 0.92 - for binary y, n classification
# Y, N, U
# Accuracy = sunday: 0.71 - added Assessment year
# Accuracy = sunday: 0.76 - added Assessment year, and State<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11691242852843815, 'fraction_numerical': 0.016551309058080048, 'mean_word_length': 3.664561403508772, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '106608', 'license': 'MIT', 'n_tokens_mistral': 2412, 'n_tokens_neox': 2215, 'n_words': 431, 'path': 'scripts/slpitz/classification_cleanup_required.ipynb', 'repo_name': 'datakind/Mar21-environmental-impact', 'size': '6646'}