【问题标题】:ROC curve plottingROC曲线绘制
【发布时间】:2020-01-24 07:44:02
【问题描述】:

尝试使用 SVM 绘制具有 1200 列(作为特征)的大小为 1200 的数据集的 ROC 曲线会出现以下错误:

'数组索引过多'

代码:

 from sklearn.svm import SVC
 svclassifier = SVC(kernel='linear')

 svm = svclassifier.fit(X_train, Y_train).decision_function(X_test)

 Y_pred = svclassifier.predict(X_test)

 ns_predt = [0 for _ in range(len(Y_test))]

 Y_predt = Y_pred[:,1]

Traceback (most recent call last) IndexError
<ipython-input-92-62de12967d46> in <module>
----> 1 Y_predt = Y_pred[:,1]

IndexError: too many indices for array

【问题讨论】:

  • 显示工作代码,以便能够重现错误
  • Y_pred 的形状与您预期的不同,这就是您的 Y_pred[:,1] 不起作用的原因。你能打印“Y_pred.shape”吗?
  • Y_test 是 480 个实例的 0,1 个标签。 Y_test.shape 和 Y_pred.shape 给出的输出为 (480,0), (480,0).Y_pred 根据上面的代码给出的输出如下: [0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 1 0 1 0 0 ........ 1 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0]。 @ZarakiKenpachi
  • Y_pred.shape 是 (480,0) @Rens

标签: python size one-hot-encoding indices


【解决方案1】:

您遇到的错误与“Y_pred[:,1]”中请求的和可用的索引有关。 您正在请求第 1 列的所有行(冒号 ':')(使用 Python 的零索引,这实际上是第 2 列)。但是,Y_pred 是一个 numpy 一维数组(即没有列)。

我不确定您在 ns_predt = [0 for _ in range(len(Y_test))] 和 Y_predt = Y_pred[:,1] 中要做什么,所以我无法为您提供替代方案。但问题很明确:您请求的列不存在。

可以使用以下代码轻松复制该问题:

import pandas as pd
import numpy as np
import pdb
from sklearn.svm import SVC

print('Creating fake data..')
X_train = pd.DataFrame(np.random.randint(0,1000,size=(100, 4)), columns=list('ABCD'))
Y_train = pd.DataFrame(np.random.randint(0,10,size=(100, 1)), columns=list('E'))

X_test = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
Y_test = pd.DataFrame(np.random.randint(0,10,size=(100, 1)), columns=list('E'))

print('Initializing classifier')
svclassifier = SVC(kernel='linear')

print('Training the model')
svm = svclassifier.fit(X_train, Y_train).decision_function(X_test)

print('Predicting outcome')
Y_pred = svclassifier.predict(X_test)

print('... ? ...')
ns_predt = [0 for _ in range(len(Y_test))]
try:
    Y_predt = Y_pred[:,1]
except:
    print('I failed...')
    pdb.set_trace()

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 2019-02-27
    • 2021-03-03
    • 2013-08-10
    • 2012-01-01
    • 2017-09-11
    • 2020-08-15
    • 2019-02-05
    • 2016-12-26
    相关资源
    最近更新 更多