代码之家  ›  专栏  ›  技术社区  ›  Raksha

混淆“矩阵错误”dataframe“对象不可调用”

  •  1
  • Raksha  · 技术社区  · 7 年前

    我做错什么了?我将两个变量都设置为列表。也试过了 np.array .

    y = list(y_test.values)
    yhat = list(predictions)
    
    print(y)
    print(yhat)
    
    confusion_matrix = pd.DataFrame(confusion_matrix(y, yhat), columns=["Predicted False", "Predicted True"], index=["Actual False", "Actual True"])
    display(confusion_matrix)
    

    出:

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ..., 0]
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ..., 0]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-159-e1640f0e3b13> in <module>()
         45 print(yhat)
         46 
    ---> 47 confusion_matrix = pd.DataFrame(confusion_matrix(y, yhat), columns=["Predicted False", "Predicted True"], index=["Actual False", "Actual True"])
         48 display(confusion_matrix)
         49 
    
    TypeError: 'DataFrame' object is not callable
    

    不知道这里发生了什么…

    2 回复  |  直到 7 年前
        1
  •  3
  •   Bruno L    7 年前

    你是在笔记本里做的吗?如果是这样,也许 confusion_matrix 方法在您第一次调用它时已被数据帧隐藏。尝试更改变量名并重新启动内核。

        2
  •  2
  •   Ivanovitch    7 年前
    from sklearn.metrics import confusion_matrix
    y_true = [2, 0, 2, 2, 0, 1]
    y_pred = [0, 0, 2, 2, 0, 2]
    

    当我跑步时 confusion_matrix(y_true, y_pred) 结果如下:

    array([[2, 0, 0],
           [0, 0, 1],
           [1, 0, 2]], dtype=int64)
    

    请注意,对于这个特定的输入,结果是一个3x3矩阵,因此在这种情况下,您需要一个列表,其中列和索引有三个名称。 您可以将结果直接放入数据帧,如下所示:

    pd.DataFrame(confusion_matrix(y_true, y_pred),columns=['column 1','column 2','column 3'], index=['index 1', 'index 2','index 3'])
    

    结果如下:

         column 1  column 2  column 3
    index 1         2         0         0
    index 2         0         0         1
    index 3         1         0         2