如何利用代码iris数据集验证逻辑回归算法并进行可视化显示
时间: 2024-05-08 16:17:59 浏览: 186
Iris数据集 Iris数据集 Iris数据集Iris数据集
你好,对于您的问题,我可以回答。可以使用Python编程语言中的scikit-learn库来处理iris数据集并验证逻辑回归算法。以下是代码示例:
```
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
# 获取iris数据集并转换为pandas DataFrame类型
iris_data = load_iris()
iris_df = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)
iris_df['target'] = iris_data.target
# 划分训练集和测试集
msk = np.random.rand(len(iris_df)) < 0.8
train_df = iris_df[msk]
test_df = iris_df[~msk]
# 训练逻辑回归模型
model = LogisticRegression()
model.fit(train_df[iris_data.feature_names], train_df['target'])
# 预测测试集
test_df['predict'] = model.predict(test_df[iris_data.feature_names])
# 计算准确率
accuracy = np.sum(test_df['target'] == test_df['predict']) / len(test_df)
print('Accuracy:', accuracy)
# 可视化分类散点图
fig, ax = plt.subplots()
colors = ['blue', 'green', 'red']
for i in range(0, 3):
x = test_df[test_df['target'] == i]['sepal length (cm)']
y = test_df[test_df['target'] == i]['sepal width (cm)']
ax.scatter(x, y, c=colors[i], alpha=0.5, label=iris_data.target_names[i])
# 绘制决策边界
x_min, x_max = test_df['sepal length (cm)'].min() - 0.5, test_df['sepal length (cm)'].max() + 0.5
y_min, y_max = test_df['sepal width (cm)'].min() - 0.5, test_df['sepal width (cm)'].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, alpha=0.3)
# 显示图例和标题
ax.legend()
plt.title('Iris Classification with Logistic Regression')
plt.show()
```
希望上述回答能够帮到您,如有其他问题可继续提出。
阅读全文