鸢尾花roc曲线Python代码
时间: 2023-11-19 16:50:17 浏览: 96
以下是鸢尾花roc曲线的Python代码:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
# 加载数据
iris = load_iris()
X = iris.data
y = iris.target
# 将类别0设为正样本,其他为负样本
y = (y == 0)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# 训练逻辑回归模型
logreg = LogisticRegression().fit(X_train, y_train)
# 计算决策函数的值以及fpr和tpr
_score = logreg.decision_function(X_test)
fpr, tpr, _ = roc_curve(y_test, y_score)
# 绘制ROC曲线
plt.plot(fpr, tpr, label='ROC Curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate (Recall)')
plt.title('ROC Curve')
plt.legend(loc='best')
plt.show()
```
阅读全文