帮我设计一个关于校园卡号 消费金额(元)存储金额(元)余额(元)消费次数等因子的调查贫困python逻辑回归模型的代码
时间: 2023-08-13 22:07:31 浏览: 47
好的,下面是一个简单的关于校园卡号、消费金额、存储金额、余额、消费次数等因子的调查贫困Python逻辑回归模型的代码:
```
# 导入需要的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, roc_curve, roc_auc_score
# 读取数据
data = pd.read_csv("data.csv")
# 数据预处理
data = data.dropna() # 删除缺失值
data = data.drop(columns=["card_number"]) # 删除校园卡号
data["is_poor"] = np.where(data["balance"] < 100, 1, 0) # 添加贫困标签
# 数据可视化
plt.scatter(data["consumption"], data["balance"])
plt.xlabel("consumption")
plt.ylabel("balance")
plt.show()
# 模型训练与评估
X = data.drop(columns=["is_poor"])
y = data["is_poor"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
confusion_matrix = confusion_matrix(y_test, y_pred)
roc_auc = roc_auc_score(y_test, y_pred)
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
# 输出结果
print("Confusion Matrix:\n", confusion_matrix)
print("ROC AUC Score:", roc_auc)
# 绘制ROC曲线
plt.plot(fpr, tpr)
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.show()
```
在这个代码中,我们首先读取了一个名为"data.csv"的数据文件,然后进行了简单的数据预处理,包括删除缺失值、删除校园卡号、添加贫困标签等操作。接着,我们对数据进行可视化分析,绘制了消费金额和余额之间的散点图。然后,我们使用逻辑回归算法对数据进行拟合,并使用测试集对模型进行评估,包括输出混淆矩阵和ROC AUC得分,并绘制了ROC曲线。最后,我们输出了模型的评估结果和ROC曲线。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)