<jupyter_start><jupyter_text>ASSIGNMENT optionsReplicate code from the lesson or other examples. [Do it "the hard way" or with the "<NAME> method."](https://docs.google.com/document/d/1ubOw9B3Hfip27hF2ZFnW3a3z9xAgrUDRReOEo-FHCVs/edit)Work with one of these datasets- [Bank Marketing](https://archive.ics.uci.edu/ml/datasets/Bank+Marketing)- [Synthetic Financial Dataset For Fraud Detection](https://www.kaggle.com/ntnu-testimon/paysim1)- Any imbalanced binary classification datasetContinue improving your model. Measure validation performance with a variety of classification metrics, which could include:- Accuracy- Precision- Recall- F1- ROC AUCTry one of the other options mentioned for imbalanced classes- The [imbalance-learn](https://github.com/scikit-learn-contrib/imbalanced-learn) library can be used to "oversample the minority class, undersample the majority class, or synthesize new minority classes."- You can see how to "adjust the decision threshold" in a great blog post, [Visualizing Machine Learning Thresholds to Make Better Business Decisions](https://blog.insightdatascience.com/visualizing-machine-learning-thresholds-to-make-better-business-decisions-4ab07f823415).<jupyter_code>import pandas as pd
import numpy as np
import category_encoders as ce
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, classification_report
from sklearn.metrics import roc_auc_score
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
import seaborn as sns

df= pd.read_csv("synthetic_bank_fraud.csv")
df.head()

# x = (df.groupby("nameDest").agg({"nameDest":np.size}).sort_values("nameDest",ascending=False) >=15)==True
# x["nameDest"]==True
y= df["isFraud"]
X = df.drop(["isFraud","isFlaggedFraud","step","nameOrig","nameDest"],axis=1)

u = X["type"].unique()
for i in u:
    X[i] = X["type"].apply(lambda x : 1 if i==x else 0)
X.head()
X["cashout_drain"] = X["oldbalanceOrg"] * X["CASH_OUT"]
X["transfer_drain"] = X["oldbalanceOrg"] *X["TRANSFER"]
X =X.drop(["type"],axis=1)
def train_validation_test_split(
    X, y, train_size=0.8, val_size=0.1, test_size=0.1, 
    random_state=None, shuffle=True):
        
    assert train_size + val_size + test_size == 1
    
    X_train_val, X_test, y_train_val, y_test = train_test_split(
        X, y, test_size=test_size, random_state=random_state, shuffle=shuffle)
    
    X_train, X_val, y_train, y_val = train_test_split(
        X_train_val, y_train_val, test_size=val_size/(train_size+val_size), 
        random_state=random_state, shuffle=shuffle)
    
    return X_train, X_val, X_test, y_train, y_val, y_test
X_train, X_val, X_test, y_train, y_val, y_test = train_validation_test_split(X,y)
class_weight = {0:1,1:75}
pipeline = make_pipeline(
    PCA(10),
    LogisticRegression(class_weight=class_weight)
)
pipeline.fit(X_train,y_train)
y_pred_val = pipeline.predict(X_val)
print("accuracy score: ", accuracy_score(y_val,y_pred_val))
print("ROC: ", roc_auc_score(y_val,y_pred_val) )
print(classification_report(y_val, y_pred_val))
confusion_matrix(y_val, y_pred_val)
def confusion_viz(y_true, y_pred):
    matrix = confusion_matrix(y_true,y_pred)
    return sns.heatmap(matrix, annot=True,
                      fmt=',', linewidths=1,linecolor='grey',
                      square=True,
                      xticklabels=['Predicted\nNO', 'Predicted\nYES'], 
                       yticklabels=['Actual\nNO', 'Actual\nYES'])
confusion_viz(y_val, y_pred_val)
# u = X["type"].unique()
# for i in u:
#     X[i] = X["type"].apply(lambda x : 1 if i==x else 0)<jupyter_output><empty_output><jupyter_text>Test<jupyter_code>y_pred_test = pipeline.predict(X_test)
print("accuracy score: ", accuracy_score(y_test,y_pred_test))
print("ROC: ", roc_auc_score(y_test,y_pred_test) )
print(classification_report(y_test, y_pred_test))
confusion_matrix(y_val, y_pred_val)
def confusion_viz(y_true, y_pred):
    matrix = confusion_matrix(y_true,y_pred)
    return sns.heatmap(matrix, annot=True,
                      fmt=',', linewidths=1,linecolor='grey',
                      square=True,
                      xticklabels=['Predicted\nNO', 'Predicted\nYES'], 
                       yticklabels=['Actual\nNO', 'Actual\nYES'])
confusion_viz(y_val, y_pred_val)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11984851860102473, 'fraction_numerical': 0.009801737580752952, 'mean_word_length': 5.018766756032171, 'pattern_counts': {'":': 1, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '86578', 'license': 'MIT', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1539, 'n_words': 320, 'path': 'module-2-validate-classification-problems/assignment.ipynb', 'repo_name': 'crawftv/DS-Unit-2-Sprint-4-Model-Validation', 'size': '4500'}