<jupyter_start><jupyter_text>**Data preprocessing**<jupyter_code>import numpy as np
import pandas as pd
import matplotlib #
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn #
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score, plot_roc_curve,confusion_matrix
from sklearn.model_selection import KFold
print("numpy: {}".format(np.__version__))
print("pandas: {}".format(pd.__version__))
print("matplotlib: {}".format(matplotlib.__version__))
print("seaborn: {}".format(sns.__version__))
print("sklearn: {}".format(sklearn.__version__))
from google.colab import files
uploaded = files.upload()
data_path = "creditcard.csv"
df = pd.read_csv(data_path)
df.head()<jupyter_output><empty_output><jupyter_text>* Create data frames for the normal points and for the fraudulent points.<jupyter_code>normal = df[df.Class == 0].sample(frac=0.5, random_state=2020).reset_index(drop=True)
anomaly = df[df.Class == 1]<jupyter_output><empty_output><jupyter_text>* Add some code to check the shapes.<jupyter_code>print(f"Normal: {normal.shape}")
print(f"Anomaly: {anomaly.shape}")<jupyter_output>Normal: (142158, 31)
Anomaly: (492, 31)<jupyter_text>* Split up normal points and anomalies into training and testing sets.<jupyter_code>normal_train, normal_test = train_test_split(normal, test_size = 0.2, random_state = 2020)
anomaly_train, anomaly_test = train_test_split(anomaly, test_size = 0.2, random_state = 2020)<jupyter_output><empty_output><jupyter_text>* Create your training and validation splits.<jupyter_code>normal_train, normal_validate = train_test_split(normal_train, test_size = 0.25, random_state = 2020)
anomaly_train, anomaly_validate = train_test_split(anomaly_train, test_size = 0.25, random_state = 2020)<jupyter_output><empty_output><jupyter_text>* Create your final training, testing, and validation sets.* Concatenate the respective normal and anomaly data splits.* Define x_train, x_test, and x_validate.<jupyter_code>x_train = pd.concat((normal_train, anomaly_train))
x_test = pd.concat((normal_test, anomaly_test))
x_validate = pd.concat((normal_validate, anomaly_validate))<jupyter_output><empty_output><jupyter_text>* Define y_train, y_test, and y_validate.<jupyter_code>y_train = np.array(x_train["Class"])
y_test = np.array(x_test["Class"])
y_validate = np.array(x_validate["Class"])<jupyter_output><empty_output><jupyter_text>* Drop the column Class in the x sets.<jupyter_code>x_train = x_train.drop("Class", axis=1)
x_test = x_test.drop("Class", axis=1)
x_validate = x_validate.drop("Class", axis=1)<jupyter_output><empty_output><jupyter_text>* Get the shapes of the sets.<jupyter_code>print("Training sets:\nx_train: {} y_train: {}".format(x_train.shape, y_train.shape))
print("\nTesting sets:\nx_test: {} y_test: {}".format(x_test.shape, y_test.shape))
print("\nValidation sets:\nx_validate: {} y_validate: {}".format(x_validate.shape, y_validate.shape))<jupyter_output>Training sets:
x_train: (85588, 30) y_train: (85588,)

Testing sets:
x_test: (28531, 30) y_test: (28531,)

Validation sets:
x_validate: (28531, 30) y_validate: (28531,)<jupyter_text>* Scale everything down by normalizing the data.* Normalizes all of the data such that the mean is 0 and the standard deviation is 1.<jupyter_code>scaler = StandardScaler()
scaler.fit(pd.concat((normal, anomaly)).drop("Class", axis=1))

x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
x_validate = scaler.transform(x_validate)<jupyter_output><empty_output><jupyter_text>**Model Training** *  Define the logistic regression model.<jupyter_code>sk_model = LogisticRegression(random_state=None, max_iter=400, solver='newton-cg').fit(x_train, y_train)<jupyter_output><empty_output><jupyter_text>**Model Evaluation** * Find the accuracy using the built-in score function of the model.<jupyter_code>eval_acc = sk_model.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>* Get the predictions and calculate the AUC score.<jupyter_code>preds = sk_model.predict(x_test)
auc_score = roc_auc_score(y_test, preds)<jupyter_output><empty_output><jupyter_text>* Print out the scores.<jupyter_code>print(f"Auc Score: {auc_score:.3%}")
print(f"Eval Accuracy: {eval_acc:.3%}")<jupyter_output>Auc Score: 84.331%
Eval Accuracy: 99.867%<jupyter_text>* Generate the ROC curve for the trained logistic regression model.<jupyter_code>roc_plot = plot_roc_curve(sk_model, x_test, y_test, name='Scikit-learn ROC Curve')<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function plot_roc_curve is deprecated; Function `plot_roc_curve` is deprecated in 1.0 and will be removed in 1.2. Use one of the class methods: RocCurveDisplay.from_predictions or RocCurveDisplay.from_estimator.
  warnings.warn(msg, category=FutureWarning)<jupyter_text>* Build the confusion matrix and plot it using seaborn.<jupyter_code>conf_matrix = confusion_matrix(y_test, preds)
ax = sns.heatmap(conf_matrix, annot=True,fmt='g')
ax.invert_xaxis()
ax.invert_yaxis()
plt.ylabel('Actual')
plt.xlabel('Predicted')<jupyter_output><empty_output><jupyter_text>**Model Validation** * Define a list of weights to iterate over.<jupyter_code>anomaly_weights = [1, 5, 10, 15]<jupyter_output><empty_output><jupyter_text>* Define the number of folds and initialize data fold generator.<jupyter_code>num_folds = 5
kfold = KFold(n_splits=num_folds, shuffle=True, random_state=2020)<jupyter_output><empty_output><jupyter_text>* Define the validation script.<jupyter_code>logs = []
for f in range(len(anomaly_weights)):
  fold = 1
  accuracies = []
  auc_scores= []
  for train, test in kfold.split(x_validate, y_validate):
    weight = anomaly_weights[f]
    class_weights= {
        0:1,
        1: weight
    }
    sk_model = LogisticRegression(random_state=None,
                                  max_iter=400,
                                  solver='newton-cg',
                                  class_weight=class_weights).fit(x_validate[train],
                                                                  y_validate[train])
    for h in range(40): print('-', end="")
    print(f"\nfold {fold}\nAnomaly Weight: {weight}")
    eval_acc = sk_model.score(x_validate[test], y_validate[test])
    preds = sk_model.predict(x_validate[test])
    try:
      auc_score = roc_auc_score(y_validate[test], preds)
    except:
      auc_score = -1
    print("AUC: {}\neval_acc: {}".format(auc_score, eval_acc))
    accuracies.append(eval_acc)
    auc_scores.append(auc_score)
    log = [sk_model, x_validate[test], y_validate[test], preds]
    logs.append(log)
    fold = fold + 1
print("\nAverages: ")
print("Accuracy: ", np.mean(accuracies))
print("AUC: ", np.mean(auc_scores))
print("Best: ")
print("Accuracy: ", np.max(accuracies))
print("AUC: ", np.max(auc_scores))<jupyter_output>----------------------------------------
fold 1
Anomaly Weight: 1
AUC: 0.8
eval_acc: 0.9985982127212195
----------------------------------------
fold 2
Anomaly Weight: 1
AUC: 0.8568790049001132
eval_acc: 0.998422712933754
----------------------------------------
fold 3
Anomaly Weight: 1
AUC: 0.8179179195189047
eval_acc: 0.9980722046968104
----------------------------------------
fold 4
Anomaly Weight: 1
AUC: 0.8682452129973255
eval_acc: 0.9987732211706976
----------------------------------------
fold 5
Anomaly Weight: 1
AUC: 0.8528532875621686
eval_acc: 0.9989484752891693
----------------------------------------
fold 1
Anomaly Weight: 5
AUC: 0.8748241603657465
eval_acc: 0.9987734361310671
----------------------------------------
fold 2
Anomaly Weight: 5
AUC: 0.9515893956527202
eval_acc: 0.9980722046968104
----------------------------------------
fold 3
Anomaly Weight: 5
AUC: 0.9314663169342972
eval_acc: 0.9987732211706976
----------------------------------------
fold 4
Anomaly Weight: [...]<jupyter_text>* Begin the first fold with weight ten, so the second fold is at index 11.<jupyter_code>sk_model, x_val, y_val, preds = logs[11]<jupyter_output><empty_output><jupyter_text>* Look at the ROC curve.<jupyter_code>roc_plot = plot_roc_curve(sk_model, x_val, y_val, name='Scikitlearn ROC Curve')<jupyter_output>/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function plot_roc_curve is deprecated; Function `plot_roc_curve` is deprecated in 1.0 and will be removed in 1.2. Use one of the class methods: RocCurveDisplay.from_predictions or RocCurveDisplay.from_estimator.
  warnings.warn(msg, category=FutureWarning)<jupyter_text>* Look at the confusion matrix to get a better idea of how the model performed on this fold.<jupyter_code>conf_matrix = confusion_matrix(y_val, preds)
ax = sns.heatmap(conf_matrix, annot=True,fmt='g')
ax.invert_xaxis()
ax.invert_yaxis()
plt.ylabel('Actual')
plt.xlabel('Predicted')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15502574434743677, 'fraction_numerical': 0.04689948511305127, 'mean_word_length': 4.775694893341952, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '645393', 'license': 'MIT', 'n_tokens_mistral': 3400, 'n_tokens_neox': 3030, 'n_words': 647, 'path': 'Simple logistic regression model with scikit-learn.ipynb', 'repo_name': 'thirasit/Begin-MLOps', 'size': '8934'}