import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
import tensorflow as tf
import datetime
import os
import tempfile
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
raw_df = pd.read_csv('shouhuan5000xiaciFL.csv')
cleaned_df = raw_df.copy()
####pos是危险的####
neg, pos = np.bincount(raw_df['Class'])
####4000个训练,1000个测试####
train_df, test_df = train_test_split(cleaned_df, test_size=0.2)
####弹出训练标签,和测试标签####
# Python列表pop()方法用于从列表中删除并返回最后一个对象
train_labels = np.array(train_df.pop('Class'))
test_labels = np.array(test_df.pop('Class'))
train_features = np.array(train_df)
test_features = np.array(test_df)
####归一化####
scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)
test_features = scaler.transform(test_features)
####限制在-5,5之间####
train_features = np.clip(train_features, -5, 5)
test_features = np.clip(test_features, -5, 5)
####创建模型####
METRICS = [
keras.metrics.TruePositives(name='tp'),
keras.metrics.FalsePositives(name='fp'),
keras.metrics.TrueNegatives(name='tn'),
keras.metrics.FalseNegatives(name='fn'),
keras.metrics.BinaryAccuracy(name='accuracy'),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall'),
keras.metrics.AUC(name='auc'),
]
def make_model(metrics=METRICS, output_bias=None):
if output_bias is not None:
output_bias = tf.keras.initializers.Constant(output_bias)
model = keras.Sequential([
keras.layers.Dense(
16, activation='relu',
kernel_regularizer='l2',
input_shape=(train_features.shape[-1],)),
keras.layers.Dropout(0.5),
keras.layers.Dense(1, activation='sigmoid',
bias_initializer=output_bias),
])
model.compile(
optimizer=keras.optimizers.Adam(lr=1e-3),
loss=keras.losses.BinaryCrossentropy(),
metrics=metrics)
return model
start = datetime.datetime.now()
EPOCHS = 200
BATCH_SIZE = 64
initial_bias = np.log([pos / neg])
model = make_model(output_bias=initial_bias)
careful_bias_history = model.fit(
train_features,
train_labels,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
verbose=0)
results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0)
print("Loss: {:0.4f}".format(results[0]))
end = datetime.datetime.now()
print(end - start)
###########画图###############
def plot_loss(history, label, n):
# Use a log scale on y-axis to show the wide range of values.
plt.plot(history.epoch, history.history['loss'],
color='blue', label='Train ' + label, linewidth=1)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plot_loss(careful_bias_history, "Careful Bias", 1)
test_predictions_baseline = model.predict(test_features, batch_size=BATCH_SIZE)
def plot_cm(labels, predictions, p=0.5):
cm = confusion_matrix(labels, predictions > p)
plt.figure(figsize=(5, 5))
sns.heatmap(cm, annot=True, fmt="d")
plt.title('Confusion matrix @{:.2f}'.format(p))
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
a = cm[0][0]
b = cm[0][1]
c = cm[1][0]
d = cm[1][1]
print('f1:', 2 * ((d * d) / (2 * d * d + d * c + d * b)))
print('Legitimate Transactions Detected (True Negatives): ', cm[0][0])
print('Legitimate Transactions Incorrectly Detected (False Positives): ', cm[0][1])
print('Fraudulent Transactions Missed (False Negatives): ', cm[1][0])
print('Fraudulent Transactions Detected (True Positives): ', cm[1][1])
print('Total Fraudulent Transactions: ', np.sum(cm[1]))
baseline_results = model.evaluate(test_features, test_labels,
batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(model.metrics_names, baseline_results):
print(name, ': ', "{:0.4f}".format(value))
plot_cm(test_labels, test_predictions_baseline)
def plot_metrics(history):
metrics = ['loss', 'auc', 'precision', 'recall']
for n, metric in enumerate(metrics):
name = metric.replace("_", " ").capitalize()
plt.subplot(2, 2, n + 1)
plt.plot(history.epoch, history.history[metric], color='blue', label='Train', linewidth=0.5)
plt.xlabel('Epoch')
plt.ylabel(name)
if metric == 'loss':
plt.ylim([0, plt.ylim()[1]])
elif metric == 'auc':
plt.ylim([0.8, 1])
else:
plt.ylim([0, 1])
plt.legend()
plot_metrics(careful_bias_history)
plt.show()
Adam和焦点损失在bp神经网络中的应用
最新推荐文章于 2023-12-30 02:22:52 发布