sklearn中报错ValueError: Expected 2D array, got 1D array instead:

 1 from sklearn.linear_model import LinearRegression
 2 lr = LinearRegression()
 3 print(tr_x.shape,tr_y.shape)
 4 lr.fit(tr_x,tr_y)
 5
 6
 7 # 报错
 8 (64,) (64,)
 9 Traceback (most recent call last):
10   File "F:/Python_Project/sklearn2_2/zong_fu_xi/A_02.py", line 51, in <module>
11     lr.fit(tr_x,tr_y)
12   File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\linear_model\base.py", line 458, in fit
13     y_numeric=True, multi_output=True)
14   File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 756, in check_X_y
15     estimator=estimator)
16   File "F:\Python_Project\machine_learning_project_01\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array
17     "if it contains a single sample.".format(array))
18 ValueError: Expected 2D array, got 1D array instead:
19 array=[ 9.1802  5.8707  7.4239 13.176   7.0708  5.6397 18.959   5.0269  8.5186
20  21.279   5.7737 11.708   8.3829  6.3654  6.4296  6.8825  6.3534  7.4764
21   5.5204  8.8254  5.5277  7.9334 22.203   5.3077  5.734   8.0959  5.5649
22   7.6031 14.164   9.2482  5.7077  9.3102  5.0365  5.8918  9.7687  5.3794
23   6.5479  6.1891  5.2524  7.5402  8.2934 13.394  10.136  20.27    7.6366
24   7.2259 10.274  12.828  12.836   5.8014  5.4069  8.2951  9.4536  8.4084
25   7.3345  5.6063  5.4901  6.5159  5.7107  5.3054  5.4994  7.2182 11.7
26   7.0931].
27 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)

此时这个错误就解决了

原文地址:https://www.cnblogs.com/--wwwww/p/10476238.html

时间: 2024-09-29 14:03:03

sklearn中报错ValueError: Expected 2D array, got 1D array instead:的相关文章

python3.6使用chardet模块总是报错ValueError: Expected a bytes object, not a unicode object

py3里,字符串,str类型,是unicode编码格式.其他类型都是byte,编码格式是gbk,utf-8等 而chardet是检查byte类型的编码格式的,不是检查str类型的编码格式的. a="abc啊",是字符串,就不能用chardet了. 当获取的数据(byte)类型,需要当成字符串来处理,需要先将其编码成unicode(python处理的都是此编码). 而要将bytes转换成unicode,需要先知道bytes具体是什么编码格式如gbk,然后使用aa=decode("

tcpSrv在vs2008中报错

报错内容: 1>LINK : warning LNK4076: 无效的增量状态文件“F:\VC_proj\Cproj\Debug\Cproj.ilk”:正在非增量链接1>lase.obj : error LNK2028: 无法解析的标记(0A000013) "extern "C" int __stdcall closesocket(unsigned int)" ([email protected]@[email protected]),该标记在函数 &q

selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string

在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.csdn.net/detail/ztzy520/9725887 或https://github.com/mozilla/geckodriver/release 如:geckodriver-v0.14.0-win32 Chrome:chromedriver 下载网址:http://chromedriver

MySql中报错:java.sql.SQLException: Incorrect string value: &#39;\xF0\x9F\x90\xBB&#39; for column

问题描述: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xBB' for column 'nickName' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226) at com.mysql.jdbc.M

matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfullpathname: embedded null character in path 原因以及Solution: http://stackoverflow.com/questions/34004063/error-on-import-matplotlib-pyplot-on-anaconda3-f

Swift在for循环中报错&#39;++&#39; is not a binary operator

最近刚开始学swift,遇到了一个非常诡异的问题.是在写for循环的时候出现语法错误.代码如下: for var i = 0; i < 10; i++{ println("hello world") } 按理说这是Swift里最简单的for循环的使用了.但是编译器还是报了两个错: '++ is not a binary operator' Operator is not a known binary operator 虽然苹果在官方文档里面说,建议通过++i这种方式使用自增运算符

Android中报错

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*****Activity}: java.lang.ClassNotFoundException: Didn't find class "*****Activity" on path: /data/app/*******.apk..... 最近总是会遇到这样的错误.哦!不,是自从用了Android4.0中的Eclise后,才发现自己经常会面对

keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: &#39;Tensor(&quot;embedding_1/random_uniform:0&quot;, shape=(5001, 128), dtype=float32)&#39;

在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)' 原因:服务器和本地的运行环境配置不同 查看本地keras版本: In [1]: import kera

ThinkPHP5.0中报错could not find driver的解决方式

这个报错是我的tp5项目转移到另外的服务器中发生的错误, 其中报错信息中还包含这pdo等字眼 解决方法:在php.ini中开启php_pdp_mysql.dll