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:的更多相关文章

  1. [已解决]报错: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 ...

  2. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  3. python 使用sk_learn 遇到 问题ValueError: Expected 2D array, got 1D array instead:

    这里我找到我的问题是: 使用的是一个新的scikit学习版本,它抛出了一个错误,因为在新版本中,所有东西都必须是一个二维矩阵,甚至是一个列或行. 它甚至说:用数组来重塑你的数据.如果您的数据有一个单独 ...

  4. 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 ...

  5. odoo开发笔记--ValueError Expected singleton

    异常处理参考:https://stackoverflow.com/questions/31070640/valueerror-expected-singleton-odoo8 报错: ValueErr ...

  6. moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法

    ☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...

  7. 解决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 ...

  8. js 在myeclipse中报错

    转myeclipse中的js文件报错   整理一下,希望帮到 遇到此问题的哥们.姐们. 方法一:myeclipse9 很特殊 和 myeclipse10 不一样,所以myeclipse9 不能使用该方 ...

  9. 转帖:解决jquery.js在myeclipse中报错的问题

    转载地址:http://tieba.baidu.com/p/1993753087 从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在my ...

随机推荐

  1. 20165220 mybash

    使用fork,exec,wait实现mybash - 写出伪代码,产品代码和测试代码 - 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 1.fork 功能:创建一个新的进程 一个现存进程 ...

  2. webpack打包后访问不到json文件

    一.问题描述 在vue中,前端写ajax假数据,用axios将json数据渲染到组件中,开发期间一切正常,webpack打包压缩后,json文件的路径错误,页面访问不到数据,导致渲染失败. 二.预期结 ...

  3. Class.forName()+Class.forName().newlnstance()和new语句和初始化块+static初始化块+构造方法之间的关系

    先上代码 class A{     int a;     static {System.out.println("载入类时执行");}     public A() {       ...

  4. vue中,对象数组多层嵌套时,更新数据更新页面

    vue中的对象和数组的元素直接赋值修改时,是不能响应到view中去的 1.对象更新 this.a={title:'列表1’}; this.a.title='列表2’; <h1>{{a.ti ...

  5. 小甲鱼Python第二十一讲课后习题

    测试题: 0.  递归在编程上的形式是如何表现的呢? 在编程上,递归表现为函数调用本身这么一个行为. 1.  递归必须满足哪两个基本条件? 一.        函数调用自身二.        设置了正 ...

  6. FloatingActionButton的使用

  7. Python 在cmd中import模块成功,但是在jupyter notebook中No module xxx found

    由于需要用到python中的某个库,因此打开命令行窗口cmd,然后使用pip安装.安装成功后,在cmd中输入python调出python环境,import该模块并使用,可以正常使用.但是打开juypt ...

  8. Windows系统Git安装配置

    Git的安装 Git是一个开源的分布式的版本控制软件,是Linus Torvalds 为了方便开源贡献者协同开发和管理 Linux 内核开发替代BitKe而开发的. 打开git官网的下载地址:http ...

  9. EF Oracle TNS 连接

    <oracle.manageddataaccess.client> <version number="*"> <settings> <se ...

  10. logback使用注意点1

    logback中配置了springProfile(策略),因此在properties中只需要配置如下即可logging.config=./config/logback.xml //logback配置文 ...