sklearn中报错ValueError: Expected 2D array, got 1D array instead:
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
print(tr_x.shape,tr_y.shape)
lr.fit(tr_x,tr_y) # 报错
(64,) (64,)
Traceback (most recent call last):
File "F:/Python_Project/sklearn2_2/zong_fu_xi/A_02.py", line 51, in <module>
lr.fit(tr_x,tr_y)
File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\linear_model\base.py", line 458, in fit
y_numeric=True, multi_output=True)
File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 756, in check_X_y
estimator=estimator)
File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array
"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[ 9.1802 5.8707 7.4239 13.176 7.0708 5.6397 18.959 5.0269 8.5186
21.279 5.7737 11.708 8.3829 6.3654 6.4296 6.8825 6.3534 7.4764
5.5204 8.8254 5.5277 7.9334 22.203 5.3077 5.734 8.0959 5.5649
7.6031 14.164 9.2482 5.7077 9.3102 5.0365 5.8918 9.7687 5.3794
6.5479 6.1891 5.2524 7.5402 8.2934 13.394 10.136 20.27 7.6366
7.2259 10.274 12.828 12.836 5.8014 5.4069 8.2951 9.4536 8.4084
7.3345 5.6063 5.4901 6.5159 5.7107 5.3054 5.4994 7.2182 11.7
7.0931].
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.
# 造成错误原因:
这是由于在新版的sklearn中,所有的数据都应该是二维矩阵,哪怕它只是单独一行或一列(比如前面做预测时,仅仅只用了一个样本数据),前面程序第3行输出的维度是(64,)一维的,所以需要使用.reshape(1,-1)进行转换,具体操作如下。
需改为
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
tr_x = np.array(tr_x).reshape(1,-1)
te_x = np.array(te_x).reshape(1,-1)
tr_y = np.array(tr_y).reshape(1,-1)
te_y = np.array(te_y).reshape(1,-1)
print(tr_x.shape,tr_y.shape)
lr.fit(tr_x,tr_y)
此时这个错误就解决了
sklearn中报错ValueError: Expected 2D array, got 1D array instead:的更多相关文章
- [已解决]报错:ValueError: Expected 2D array, got scalar array instead
报错代码: new_x = 84610 pre_y = model.predict(new_x) print(pre_y) 报错结果: ValueError: Expected 2D array, g ...
- 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:
决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...
- python 使用sk_learn 遇到 问题ValueError: Expected 2D array, got 1D array instead:
这里我找到我的问题是: 使用的是一个新的scikit学习版本,它抛出了一个错误,因为在新版本中,所有东西都必须是一个二维矩阵,甚至是一个列或行. 它甚至说:用数组来重塑你的数据.如果您的数据有一个单独 ...
- pytorch报错:ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1,512,1,1])
1.pytorch报错:ValueError: Expected more than 1 value per channel when training, got input size torch.S ...
- odoo开发笔记--ValueError Expected singleton
异常处理参考:https://stackoverflow.com/questions/31070640/valueerror-expected-singleton-odoo8 报错: ValueErr ...
- moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...
- 解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)
问题描述 在将一个数组送入tensorflow训练时,报错如下: ValueError: Failed to convert a NumPy array to a Tensor (Unsupporte ...
- js 在myeclipse中报错
转myeclipse中的js文件报错 整理一下,希望帮到 遇到此问题的哥们.姐们. 方法一:myeclipse9 很特殊 和 myeclipse10 不一样,所以myeclipse9 不能使用该方 ...
- 转帖:解决jquery.js在myeclipse中报错的问题
转载地址:http://tieba.baidu.com/p/1993753087 从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在my ...
随机推荐
- IntelliJ IDEA 中的Java程序目录结构
--src 应用程序源代码与测试代码的根目录 --main 应用程序代码的源目录 --java 源代码 --resources 项目用到的资源文件 --test 测试程序代码的源目录 --java 测 ...
- GA:GA优化BP神经网络的初始权值、阈值,从而增强BP神经网络的鲁棒性—Jason niu
global p global t global R % 输入神经元个数,此处是6个 global S1 % 隐层神经元个数,此处是10个 global S2 % 输出神经元个数,此处是4个 glob ...
- ISP PIPLINE (五) Denoise
what is the Denoise? Denoise就是图像去噪,平滑图像,并保留图像细节. why does Denoise? 图像在采集并转换为数字信号的过程会引入一些噪声,这些噪声会让图片看 ...
- hwy题目选讲
\[ f(n) = n/5 + f(n/5)\\ g(n) = (n\mod10)! * g(n/5)\mod 5\\ ans = f(n)*inv(2^g(n))\\ \]
- Lesnoe Ozero 2016. BSUIR Open 2016 Finals
A. Street magic 数位DP,设$f[i][j][k]$表示从低到高考虑$x$的后$i$位,$x$和$m$大小关系为$j$,和$n$大小关系为$k$的方案数. #include<cs ...
- BZOJ 4763
有毒 第一开始一直RE,我就把dfs改成了bfs 结果一直TLE,自己造的数据要跑8s 因为 lxl 等人讲随机 $\sqrt{n}$ 个点作为关键点就可以了 但是我把随机改成深度有关就AC了,而且那 ...
- dnsmasq配置
介绍 dnsmasq是一个dns和dhcp服务 参数说明 dhcp-sequential-ip 默认情况下Dnsmasq分配ip是根据目标机的mac地址按一定的hash规则来的.这样每个目标机在租约期 ...
- JS对象2
1.Date对象 创建对象 //方法1:不指定参数 var nowd1=new Date(); alert(nowd1.toLocaleString( )); //方法2:参数为日期字符串 var n ...
- [LeetCode] Friends Of Appropriate Ages 适合年龄段的朋友
Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...
- Hibernate-day04
HIbernate中的HQL查询 Hibernate中的查询方式:1,使用HQL:使用hibernate提供的面向对象的查询语句;2,使用SQL:在hibernate中允许使用原生的SQL直接查询;3 ...