Python中实现使用逻辑回归训练一个鸢尾花分类器,只训练两个类别:山鸢尾和维吉尼亚鸢尾。需要使用二分类交叉熵损失函数 ,可视化损失和准确率变化过程,并保存
时间: 2024-12-02 19:17:13 浏览: 79
在Python中,我们可以使用scikit-learn库来实现基于逻辑回归的鸢尾花分类器,仅针对山鸢尾(Setosa)和维吉尼亚鸢尾(Virginica)这两个类别。我们将使用`sklearn.model_selection.train_test_split`来划分数据集,`LogisticRegression`作为模型,`BinaryCrossentropy`作为损失函数,以及`metrics`模块来计算准确率。
以下是一个简单的步骤示例:
```python
# 导入必要的库
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, classification_report
from tensorflow.keras.losses import BinaryCrossentropy
import matplotlib.pyplot as plt
# 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data[:, :2] # 取前两个特征,简化问题到二维
y = iris.target
# 将标签编码为0(山鸢尾)和1(维吉尼亚鸢尾)
le = LabelEncoder()
y_encoded = le.fit_transform(y)
y_binary = (y_encoded == 2).astype(int) # 山鸢尾对应0,维吉尼亚鸢尾对应1
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=42)
# 创建逻辑回归模型
model = LogisticRegression(multi_class='ovr', solver='lbfgs') # 使用one-vs-rest策略
# 训练模型并记录历史
history = model.fit(X_train, y_train,
callbacks=[EarlyStopping(monitor='val_loss', patience=5)]) # 添加早停避免过拟合
# 预测和评估
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# 计算并绘制损失和准确率的变化
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Loss Curves')
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.title('Accuracy Curves')
plt.legend()
# 保存图像
plt.savefig('loss_and_accuracy.png')
# 结果展示
print(classification_report(y_test, y_pred))
#
阅读全文
相关推荐



















