用澳大利亚信贷批准数据集中的690个样本作为数据集,编写python代码分别用逻辑回归模型和随机森林模型,向量机模型构建识别模型,特征选择并用准确度召回率F1和混淆矩阵可视化等指标分别对不同模型进行评估
时间: 2024-03-09 08:45:56 浏览: 129
好的,我可以帮你完成这个任务。下面是完整的Python代码,包括数据预处理、特征选择、模型构建和评估:
```python
# 导入必要的库
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.metrics import accuracy_score, recall_score, f1_score, confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据集
data = pd.read_csv('australian.csv', header=None)
# 数据预处理
# 将分类变量转换为数值变量
data[8] = data[8].map({0: -1, 1: 1})
# 将数据集分为特征和标签
X = data.iloc[:, :-1]
y = data.iloc[:, -1]
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 数据标准化
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# 特征选择
selector = SelectKBest(chi2, k=5)
X_train = selector.fit_transform(X_train, y_train)
X_test = selector.transform(X_test)
# 逻辑回归模型
lr_model = LogisticRegression()
lr_model.fit(X_train, y_train)
lr_y_pred = lr_model.predict(X_test)
lr_accuracy = accuracy_score(y_test, lr_y_pred)
lr_recall = recall_score(y_test, lr_y_pred)
lr_f1 = f1_score(y_test, lr_y_pred)
lr_cm = confusion_matrix(y_test, lr_y_pred)
# 随机森林模型
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
rf_y_pred = rf_model.predict(X_test)
rf_accuracy = accuracy_score(y_test, rf_y_pred)
rf_recall = recall_score(y_test, rf_y_pred)
rf_f1 = f1_score(y_test, rf_y_pred)
rf_cm = confusion_matrix(y_test, rf_y_pred)
# 向量机模型
svm_model = SVC(random_state=42)
svm_model.fit(X_train, y_train)
svm_y_pred = svm_model.predict(X_test)
svm_accuracy = accuracy_score(y_test, svm_y_pred)
svm_recall = recall_score(y_test, svm_y_pred)
svm_f1 = f1_score(y_test, svm_y_pred)
svm_cm = confusion_matrix(y_test, svm_y_pred)
# 输出评估结果
print('逻辑回归模型评估结果:')
print(f'准确度:{lr_accuracy}')
print(f'召回率:{lr_recall}')
print(f'F1值:{lr_f1}')
print(f'混淆矩阵:\n{lr_cm}')
print('随机森林模型评估结果:')
print(f'准确度:{rf_accuracy}')
print(f'召回率:{rf_recall}')
print(f'F1值:{rf_f1}')
print(f'混淆矩阵:\n{rf_cm}')
print('向量机模型评估结果:')
print(f'准确度:{svm_accuracy}')
print(f'召回率:{svm_recall}')
print(f'F1值:{svm_f1}')
print(f'混淆矩阵:\n{svm_cm}')
# 可视化混淆矩阵
sns.heatmap(lr_cm, annot=True, cmap='Blues')
plt.title('Confusion Matrix - Logistic Regression')
plt.show()
sns.heatmap(rf_cm, annot=True, cmap='Blues')
plt.title('Confusion Matrix - Random Forest')
plt.show()
sns.heatmap(svm_cm, annot=True, cmap='Blues')
plt.title('Confusion Matrix - SVM')
plt.show()
```
这段代码做了以下几件事情:
1. 加载数据集并进行数据预处理,包括将分类变量转换为数值变量、将数据集分为特征和标签、将数据集分为训练集和测试集、数据标准化等。
2. 进行特征选择,使用卡方检验选择5个最重要的特征。
3. 使用逻辑回归、随机森林和支持向量机模型构建识别模型。
4. 对不同模型进行评估,包括计算准确度、召回率、F1值和混淆矩阵,并将混淆矩阵可视化。
最后,运行这段代码,你将得到逻辑回归、随机森林和支持向量机模型的评估结果和混淆矩阵可视化。
阅读全文