如何解决预期的二维数组,改为一维数组:拟合模型后
我不明白我哪里出错了。两个阵列的形状相同,编号相同。的元素。我得到 - “如果您的数据具有单个特征,则使用 array.reshape(-1,1) 或使用 array.reshape(1,-1) 如果它包含单个样本来重塑您的数据。”
我将在下面提供我的代码 -
y_pred:
array([1,1,1],dtype=int64)
y_pred.shape - (200,)
y_test :array([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.
解决方法
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,3]) will be converted 'n' rows '1' column array (2D) = np.array([[1],[3]]).
try this.
编辑: 因为您的输出长度有可能大于 1 吗? ,如 y=[[1,0],[0,[1,0]]。这是做到这一点的唯一方法。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。