【问题标题】:Getting the indices of misclassified rows of data using scikit使用 scikit 获取错误分类数据行的索引
【发布时间】:2018-11-18 23:59:44
【问题描述】:

当我使用代码时

import numpy as np

y_test = np.asarray(y_test)
misclassified = np.where(y_test != clf.predict(X_test))

对于二进制和 3 路分类,我们得到一个 2 元组,它是 X_test 的一长串索引(我假设)。两个元组中的每一个中的这些索引/数字也有重复。有人能解释一下错误分类应该是什么样子吗?

当我使用代码打印它时

clf=RandomForestClassifier(min_samples_leaf=20);
    model=clf.fit(X_train,y_train);
    #rf=RandomForestRegressor();
    accu=clf.score(x_test,y_test);
    pred=clf.predict(x_test);
    cnf_matrix=confusion_matrix(y_test,pred);
    print("Accuracy:",accu," Confusion matrix:",cnf_matrix);

    ##Test for false negatives
    np_y_test=np.asarray(y_test);
    print("test ",np_y_test.shape, " ", x_test.shape, " ",pred.shape);
    miss_arr= np.where(np_y_test!=pred);


    print(type(miss_arr)," mispredict ", miss_arr);

,我的看起来像这样:

Accuracy: 0.7131782945736435  Confusion matrix: [[32  0 15]
 [ 5  0 17]
 [ 0  0 60]]
test shape of np.array(y_test) (129, 1)  shape of x_test (129, 16) shape of clf.predict(x_test)  (129,)
<class 'tuple'>  mispredict  (array([  0,   0,   0, ..., 128, 128, 128]), array([19, 20, 34, ..., 49, 50, 51]))

如您所愿,这是用于 3 路分类。对于 2 路分类,我也得到了类似的输出。

【问题讨论】:

    标签: python-3.x scikit-learn


    【解决方案1】:

    再次使用逻辑与或 * (y==2) 会产生难以理解的输出。我得到一个长度为 2 的元组,打印第一个元组会给我一个包含许多 0 的列表,尽管第一个元组的长度与混淆矩阵中的不匹配数匹配。

    【讨论】:

    • 您应该删除此评论,因为它是作为问题的“答案”给出的。相反,如果更新的方法仍然给您带来问题,您应该编辑您的原始问题以添加所有最新代码以及打印输出的形状和输出数组,这些都会给您带来令人困惑的结果。
    【解决方案2】:

    这似乎是一个广播错误,因为y_test 的维度是(129, 1),使其看起来像一个二维数组。因此,clf.predict(...) 中的条目对于 y_test 中的每个条目都会重复,并且您最终会得到来自 where 操作的太多输出索引。

    我用随机数据做了一个玩具例子来演示:

    In [12]: x
    Out[12]: 
    array([[ 0.37842257],
           [ 0.8635568 ],
           [ 0.03439733],
           [ 0.57522837],
           [ 0.79302198],
           [ 0.37139872],
           [ 0.55252899],
           [ 0.76331397],
           [ 0.27676114],
           [ 0.20412743]])
    
    In [13]: y
    Out[13]: 
    array([ 0.18595721,  0.61417275,  0.6511883 ,  0.57157444,  0.84596623,
            0.15789458,  0.62759   ,  0.91146098,  0.02160262,  0.34704034])
    
    In [14]: x.shape
    Out[14]: (10, 1)
    
    In [15]: y.shape
    Out[15]: (10,)
    
    In [16]: np.where(x != y)
    Out[16]: 
    (array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
            2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
            4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6,
            6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
            9, 9, 9, 9, 9, 9, 9, 9]),
     array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
            3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
            6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
            9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
            2, 3, 4, 5, 6, 7, 8, 9]))
    
    In [17]: len(np.where(x != y)[0])
    Out[17]: 100
    

    但现在请注意,如果我首先重塑 x 以使其具有与 y 相同的一维维度格式会发生什么:

    In [18]: np.where(x.reshape(10) != y)
    Out[18]: (array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),)
    

    这是正确的:这只是一维索引集,其中两个单独的一维数组不匹配。

    对于您的情况,它建议您只需解决问题

    y_test.reshape(len(y_test))
    

    将其传递给np.where时。

    【讨论】:

    • 谢谢。那很有帮助。还有一个项目..所以在3种方式分类的情况下,真标签= 0,1,2,我应该能够获得y == 2并且被np.where(x.reshape( 10)!=y 和 y==2) ?
    • 你可以用乘法来表达:np.where((x.reshape(10) != y) * (y == 2))。这是有效的,因为y == 2 返回一个布尔数组,当转换为像乘法这样的算术运算时,它使True 像 1 一样,False 像 0 一样。所以元素必须在两者中计算为 True where 中的布尔条件最终在结果中提供索引。
    • 如果你不想使用乘法,因为它在阅读代码时会被欺骗,你也可以使用np.logical_and,例如np.where(np.logical_and(x.reshape(len(x)) != y, y == 2))。语法稍长,但应用的操作更清晰。
    • 再次使用逻辑与或 * (y==2) 会产生难以理解的输出。我得到一个长度为 2 的元组,打印第一个元组会给我一个 0 的列表。
    • @FahadSayeed 此操作的where 的输出应该是一个长度为1 的元组,因此关于np_y_testpred 的尺寸仍然不正确。您应该更新您的问题,显示您正在使用的这个新代码,并打印出给您带来问题的新输出。
    猜你喜欢
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2019-07-19
    • 2017-12-24
    • 2015-10-12
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多