<jupyter_start><jupyter_text>Go To https://www.kaggle.com/dalpozz/creditcardfraud<jupyter_code>data_frame = pd.read_csv('data/creditcard.csv')
data_frame.head()
data_frame.describe()
X = np.array(data_frame.drop('Class', axis=1))
print(X.shape)
Y = np.array(data_frame['Class'])
frauds = np.where(Y==1)[0] 
non_frauds = np.where(Y==0)[0] 
n_frauds = len(frauds)
print("There are {} frauds among {} transations".format(n_frauds, len(Y)))
### How to deal with such an imbalance?
# balance dataset
print(X.shape)
undersampled_non_frauds = random.sample(non_frauds, n_frauds)
indexes_to_keep = list(frauds) + list(undersampled_non_frauds)
indexes_to_remove = [i for i in range(X.shape[0]) if i not in indexes_to_keep]

X_keep = np.take(X, indexes_to_keep, axis=0)
Y_keep = np.take(Y, indexes_to_keep, axis=0)
X_train, X_test, Y_train, Y_test = train_test_split(X_keep, Y_keep, test_size=1/4., random_state=0)


X_test = np.concatenate((X_test, np.take(X, indexes_to_remove, axis=0)))
Y_test = np.concatenate((Y_test, np.take(Y, indexes_to_remove, axis=0)))

model = RandomForestClassifier(max_depth=4, n_estimators=50)
model.fit(X_train, Y_train)

Y_train_pred = model.predict(X_train)
Y_test_pred = model.predict(X_test)

Y_train_score = np.max(model.predict_proba(X_train), axis=1)
Y_test_score = np.max(model.predict_proba(X_test), axis=1)

train_mse = sklearn.metrics.mean_squared_error(Y_train, Y_train_pred)
test_mse = sklearn.metrics.mean_squared_error(Y_test, Y_test_pred)
print("Train MSE {}".format(train_mse))
print("Test MSE {}".format(test_mse))

train_accuracy = sklearn.metrics.accuracy_score(Y_train, Y_train_pred)
test_accuracy = sklearn.metrics.accuracy_score(Y_test, Y_test_pred)
print("Train Accuracy {}%".format(train_accuracy*100.0))
print("Test Accuracy {}%".format(test_accuracy*100))
print("Test Precision {}".format(sklearn.metrics.precision_score(Y_test, Y_test_pred)))
print("Test Recall {}".format(sklearn.metrics.recall_score(Y_test, Y_test_pred)))
print("Test f1 Score {}".format(f1_score(Y_test, Y_test_pred)))
cf_matrix = confusion_matrix(Y_test, Y_test_pred)
cf_matrix
# plot AUC, precision, recall
print(Y_test.shape)
precision, recall, _ = precision_recall_curve(Y_test, Y_test_score)
average_precision = average_precision_score(Y_test, Y_test_score)

plt.step(recall, precision, color='b', alpha=0.2,
         where='post')
plt.fill_between(recall, precision, step='post', alpha=0.2,
                 color='b')

plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.title('Precision-Recall curve: AUC={0:0.2f}'.format(
          average_precision))
plt.show()<jupyter_output>(284069,)
{'fraction_non_alphanumeric': 0.15135542168674698, 'fraction_numerical': 0.018448795180722892, 'mean_word_length': 7.277258566978193, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '312743', 'license': 'MIT', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1045, 'n_words': 166, 'path': 'Sec2/2.6 Notebook.ipynb', 'repo_name': 'PacktPublishing/Machine-learning-with-Sci-kit-Learn-and-Tensorflow-V-', 'size': '2656'}