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 ...
随机推荐
- UOJ#347. 【WC2018】通道 边分治 虚树
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ347.html 题意 有三棵树,边有边权. 对于所有点对 (x,y) 求在三棵树上 x 到 y 的距离之和 ...
- Telsa显卡比较
1. T4 2. P4/ P40 3. P100 4. V100
- LeetCode 709.To Lower Case
Description Implement function ToLowerCase() that has a string parameter str, and returns the same s ...
- DAY01-Python入门学习-计算机硬件
一.Python是编程语言 语言: 一种事物与另外一种事物沟通的介质所以说编程语言是程序员与计算机沟通的介质 什么是编程: 就是程序员用计算机所能理解的表达方式(编程语言)把自己的思维逻辑写下来,编程 ...
- day13_H5_CSS_1
一.标签.HTML是可以被浏览器解析的一套规则 1.a标签:他是主动闭合标签,表现形式<a href="http://www.baidu.com">baidu<a ...
- __x__(8)0906第三天__乱码问题
需要知道: 计算机只认 0 1 任何内容,计算机都会以 0 1 去存储 所以 0 1 与内容的编码方式/解码方式需要依照一定的规则,实现 0 1 与内容之间的转换. 字符集:一定的规则,由编码/解码采 ...
- python 生成器generator
关于生成器,主要有以下几个 关键点的内容 一.什么是generator ,为什么要有generator? 二.两种创建生成器方式 三.yield关键字 四.generator 两个调用方法 next( ...
- mac charles抓安卓(小米)http包
网上有很多的教程说明如何操作,都写的很好,比如 https://blog.csdn.net/luochoudan/article/details/72801573,我在这里补充一点,非常重要的一点:手 ...
- DevExpress控件GridControl中的布局详解 【转】
DevExpress控件GridControl中的布局详解 [转] 2012-10-24 13:27:28| 分类: devexpress | 标签:devexpress |举报|字号 订阅 ...
- jmeter使用指南:jmeter无脑式指南
一:启动jmeter 二:添加线程组 三:添加 HTTP 请求 四:添加监听器 五:填写访问的域名,并保存 六:运行,查看结果 七:配置多线程.循环机制,进行压力测试