gpt4 book ai didi

python - 将 ROC AUC 分数与 Logistic 回归和 Iris 数据集结合使用

转载 作者:太空狗 更新时间:2023-10-30 00:00:53 24 4
gpt4 key购买 nike

我需要的是:

  • 应用逻辑回归分类器
  • 使用 AUC 报告每个类别的 ROC。
  • 使用逻辑回归的估计概率来指导 ROC 的构建。
  • 为训练您的模型进行 5 折交叉验证。

为此,我的方法是使用 this非常好的教程:

根据他的想法和方法,我只是改变了我获取原始数据的方式,我得到的是这样的:

df = pd.read_csv(
filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
header=None,
sep=',')

df.columns=['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
df.dropna(how="all", inplace=True) # drops the empty line at file-end

df.tail()

# split data table into data X and class labels y
X = df.iloc[:,0:4].values
Y = df.iloc[:,4].values

他们我只是运行代码。如果我尝试运行 accuracy
等指标balanced_accuracy 一切正常(即使有许多其他指标)。我的问题是,当我尝试使用指标 roc_auc 运行时,出现错误:

"ValueError: Only one class present in y_true. ROC AUC score is not defined in that case."

这个错误已经讨论过here1 , here2 , here3 , 和 here4 .但是,我无法使用他们提供的任何“解决方案”/解决方法来解决我的问题。

我的全部代码是:

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode
from sklearn.preprocessing import StandardScaler
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'qt')
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split


df = pd.read_csv(
filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
header=None,
sep=',')

df.columns=['sepal_len', 'sepal_wid', 'petal_len', 'petal_wid', 'class']
df.dropna(how="all", inplace=True) # drops the empty line at file-end

df.tail()

# split data table into data X and class labels y
X = df.iloc[:,0:4].values
Y = df.iloc[:,4].values

#print(X)
#print(Y)


seed = 7

# prepare models
models = []
models.append(('LR', LogisticRegression()))

# evaluate each model in turn
results = []
names = []
scoring = 'roc_auc'
for name, model in models:
kfold = model_selection.KFold(n_splits=5, random_state=seed)
cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)



# boxplot algorithm comparison
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.boxplot(results)
ax.set_xticklabels(names)
plt.show()

最佳答案

鸢尾花数据集通常根据类别排序。因此,当您在不进行洗牌的情况下进行拆分时,测试数据集可能只会得到一个类。

一个简单的解决方案是使用 shuffle 参数。

kfold = model_selection.KFold(n_splits=10, shuffle=True, random_state=seed)

即便如此,roc_auc 也不直接支持多类格式(iris - 数据集具有三个类)。

通过this链接以了解有关如何将 roc_auc 用于多类情况的更多信息。

关于python - 将 ROC AUC 分数与 Logistic 回归和 Iris 数据集结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com