python 过采样-机器学习项目实战——信用卡欺诈检测(过采样代码)

import pandas as pd

from imblearn.over_sampling import SMOTE # pip install imblearn

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import confusion_matrix, recall_score

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

from sklearn.linear_model import LogisticRegression

import numpy as np

def printing_Kfold_scores(x_train_data, y_train_data):

"""交叉验证求最佳参数"""

from sklearn.model_selection import KFold

fold = KFold(5, shuffle=False) # 分成5份进行交叉验证

# 惩罚力度

c_param_range = [0.01, 0.1, 1, 10, 100]

result_table = pd.DataFrame(index=range(len(c_param_range), 2), columns=["C_parameter", "Mean recall score"])

result_table["C_parameter"] = c_param_range

j=0 # 惩罚力度index

# 循环找到最好的惩罚力度

for c_param in c_param_range:

print("-------------------------------------------")

print("C parameter:", c_param)

print("------------------------------------------- ")

recall_accs = []

for iteration, indices in enumerate(fold.split(x_train_data)):

# fold.split(x_train_data) --> [train_indices, test_indices]

# 用特定的c参数调用逻辑回归模型

lr = LogisticRegression(C = c_param, penalty="l1", solver="liblinear",max_iter=10000)

# 警告 ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.

# 解决 增加solver="liblinear" max_iter=10000(默认1000)

# 将x的训练值, y的训练值.ravel() 填充进lr

lr.fit(x_train_data.iloc[indices[0], :], y_train_data[indices[0]].values.ravel())

# 预测值 = lr.predict(x的验证值)

y_pred = lr.predict(x_train_data.iloc[indices[1], :].values)

# 用 y的验证值, y的预测值 计算recall,反映当前的c参数

recall_acc = recall_score(y_train_data[indices[1]].values, y_pred)

recall_accs.append(recall_acc)

print("Iteration: {}, recall score = {}".format(iteration, recall_acc))

# 多次交叉验证的评分均值

result_table.loc[j, "Mean recall score"] = np.mean(recall_accs)

j += 1

print("")

print("Mean recall score ", np.mean(recall_accs))

print("")

# 注意此处报错 源代码没有astype("float64")

best_c = result_table.loc[result_table["Mean recall score"].astype("float64").idxmax()]["C_parameter"]

# Finally, we can check which C parameter is the best amongst the chosen.

print("*********************************************************************************")

print("Best model to choose from cross validation is with C parameter", best_c)

print("*********************************************************************************")

return best_c

def plot_confusion_matrix(cm, classes, title="Confusion matrix", cmap=plt.cm.Blues):

"""

绘制混淆矩阵

cm: confusion_matrix 混淆矩阵对象

classes: 类别,例如[0, 1]

cmap: 样式

"""

plt.imshow(cm, interpolation="nearest", cmap=cmap)

plt.title(title)

plt.colorbar()

tick_marks = np.arange(len(classes))

plt.xticks(tick_marks, classes, rotation=0)

plt.yticks(tick_marks, classes)

# 混淆矩阵的文字颜色

# 上半部分蓝色,因此文字呈白色

# 下半部分白色,因此文字呈黑色

import itertools

for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):

color="white" if cm[i, j] > cm.max() / 2 else "black"

plt.text(j, i, cm[i, j], horizontalalignment="center")

plt.tight_layout()

plt.ylabel("True label")

plt.xlabel("Predicted label")

# 读取数据

credit_cards = pd.read_csv("creditcard.csv")

# 划分特征、标签数据

features = credit_cards.iloc[:, :-1]

labels = credit_cards["Class"]

# 划分训练集测试集

features_train, features_test, labels_train, labels_test = train_test_split(

features, labels, test_size=0.2, random_state=0)

# 根据SMOTE算法得到过采样数据集

oversampler = SMOTE(random_state=0)

os_features, os_labels = oversampler.fit_sample(features_train, labels_train)

# 交叉验证

best_c = printing_Kfold_scores(os_features, os_labels)

# 创建lr对象

lr = LogisticRegression(C=best_c, penalty="l1", solver="liblinear", max_iter=10000)

lr.fit(os_features, os_labels)

y_pred = lr.predict(features_test.values)

# 计算混淆矩阵

cnf_matrix = confusion_matrix(labels_test, y_pred)

# 打印评估分数recall = TP / (FN + TP)

np.set_printoptions(precision=2) # 打印两位小数

recall_acc = cnf_matrix[1, 1] / (cnf_matrix[1, 0] + cnf_matrix[1, 1])

print("Recall metric in the testing dataset:", recall_acc)

# 绘制混淆矩阵

class_names = [0, 1]

plt.figure()

plot_confusion_matrix(cnf_matrix, classes=class_names)

plt.show()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python项目案例开发从入门到实战的主题为爬虫、游戏和机器学习源码。这本书旨在让读者通过实际案例的开发,逐步掌握Python的应用技巧和项目开发经验。 首先,书中介绍了爬虫的基础知识和原理,然后通过实战案例,教读者如何使用Python编写自己的爬虫程序。案例涵盖了从简单的网页爬取到高级的数据挖掘和分析。读者通过学习这些案例,可以了解爬虫的工作原理和实际应用。 其次,书中还介绍了Python游戏开发的基础知识和技巧。读者可以通过案例学习如何使用Python库和框架,编写自己的游戏程序。案例涵盖了不同类型的游戏,包括文字冒险游戏、迷宫游戏和简单的图形游戏等。读者通过实践,可以逐步提升自己的游戏开发技能。 最后,书中还介绍了机器学习的基本原理和常用算法。通过案例,读者可以学习如何使用Python编写机器学习模型,进行数据预处理和特征工程,并进行模型评估和优化。案例涵盖了分类、回归和聚类等不同类型的机器学习任务,读者可以通过这些案例加深对机器学习的理解,提升自己的数据科学能力。 总体来说,这本书通过实际案例的开发,引导读者逐步掌握Python的应用技巧和项目开发经验。爬虫、游戏和机器学习源码的学习将帮助读者在实践中提升自己的编程能力,并为未来的项目开发打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值