【问题标题】:Expected 2D array, got 1D array instead: after fitting the model预期的二维数组,得到一维数组:拟合模型后
【发布时间】:2021-04-16 04:22:48
【问题描述】:

我不明白我哪里错了。两个阵列形状相同,编号相同。的元素。我得到了 - “如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1)。”

我将在下面提供我的代码 -

y_pred:
    array([1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1,
           1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1,
           0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1,
           1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
           1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
           1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
           1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 1], dtype=int64)
   

 y_pred.shape - (200,)
    
   
 y_test :array([1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1,
           1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
           0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1,
           0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0,
           0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1,
           1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
           0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
           1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
           1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 1], dtype=int64)
    

y_test.shape -  (200,)

现在当我尝试评估这个 (dtc.score(y_test,y_pred_dtr)) 时,我收到了这个错误 -

ValueError: Expected 2D array, got 1D array instead:
    array=[1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 1. 1. 0. 0. 1. 0. 1. 0. 0. 1. 1. 1. 1. 1.
     0. 1. 0. 0. 0. 1. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 1. 0. 0. 0. 1.
     1. 0. 1. 1. 1. 0. 1. 0. 1. 0. 0. 0. 1. 1. 1. 0. 1. 1. 0. 1. 0. 1. 1. 0.
     0. 0. 0. 1. 1. 1. 1. 1. 0. 0. 1. 1. 1. 0. 1. 0. 0. 0. 1. 0. 0. 1. 1. 0.
     0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 1. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 1.
     1. 1. 0. 0. 0. 1. 0. 1. 1. 1. 0. 0. 0. 1. 1. 0. 1. 1. 1. 0. 0. 0. 1. 1.
     0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 1. 0. 0. 0. 0. 1. 1. 0. 1. 0. 1. 0. 1. 1.
     0. 0. 1. 0. 1. 1. 1. 0. 1. 1. 1. 1. 0. 1. 0. 1. 1. 1. 1. 0. 1. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 1.].
    

Reshape your data either using array.reshape(-1, 1) if your data has a
single feature or array.reshape(1, -1) if it contains a single sample. 

【问题讨论】:

  • 当你在做 dtc.score(y_test, y_pred_dtr) 时。 y_pred_dtr 是什么时候获得的?我只看到 y_pred。另外,我可以看到您没有获得普通数组,值没有用逗号分隔。不知道是不是复制输出的错误
  • @AlexSerraMarrugat 我做了:y_pred = dtc.predict(x_test)。这就是我获得 y_pred 值的方式。
  • 是的,但是在你的乐谱中你写:y_pred_dtr。这是您尚未定义的变量。可以查一下吗?
  • @AlexSerraMarrugat 我在评论中错误地写道,我计算了 y_pred_dtr
  • 你有多少功能?

标签: machine-learning scikit-learn classification


【解决方案1】:
here is the thing,
a=np.array([1,2,3]) is 1D array
now in y_pred and y_test it should be 
array=np.array([sample1array,sample2array,....])
so it should be,
a=np.array([[1],[2],[3]])
so try reshape(-1,1) so
np.array([1,2,3]) will be converted 'n' rows '1' column array (2D) = np.array([[1],[2],[3]]).
try this.

编辑: 因为你的输出长度可能大于1呢? ,如 y=[[1,0],[0,0],[1,0]]。这是做到这一点的唯一方法。谢谢。

【讨论】:

  • 我明白,但我必须重塑两个阵列?还有为什么我们需要二维数组来评估模型的分数?
  • 对不起,但不是,两个数组的形状相同,我在问题中提到过。这两个数组都是一维数组,其中包含 200 个值。
猜你喜欢
  • 2019-08-17
  • 2018-06-06
  • 2021-03-01
  • 2019-10-07
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
相关资源
最近更新 更多