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. SpringMVC-2-(Controller)

    一)参数类型 @RequestMapping("hello4") @ResponseBody public ModelAndView Hello4( // Servlet的三个参数 ...

  2. Palindrome POJ - 3974 (字符串hash+二分)

    Andy the smart computer science student was attending an algorithms class when the professor asked t ...

  3. BZOJ 5104

    这个模数比较有趣 可以求出 $\sqrt{5}$ 然后就可以做了 $f_n=\dfrac{\sqrt{5}}{5}[(\dfrac{\sqrt{5}+1}{2})^n-(\dfrac{1-\sqrt{ ...

  4. bat入门--第一个bat文件

    所谓的批处理就是从记事本开始进行的. 1.新建一个记事本文件, 2, 打开的记事本上敲入一行字:@echo off 意思:隐藏以下输入的代码(off改成on是打开代码显示). 3.再输入:echo h ...

  5. CCTV5 前端

    get :  fre = 11868  symbolrate  = 27500 pls_n=0------167    CCTV 9------------168    CCTV 3--------- ...

  6. GMA Round 1 新程序

    传送门 新程序 程序框图如图所示,当输入的n=时,输出结果的ans是多少? 容易看出该程序求n以内质数个数,50以内有15个. 定位:简单题

  7. JavaScript学习day1

    JavaScript 特点: javascript 是一种脚本语言,它的解释器被称为javascript引擎,JavaScript被发明用于在HTML网页上使用,给HTML 网页增加动态功能 由于ja ...

  8. linux中安装python+selenium+chrome

  9. spring boot中jsp解析c标签方法

    pro.xml中添加jstl标签 <dependency><groupId>javax.servlet</groupId><artifactId>jst ...

  10. 写给踏入IT行业的自己

    在IT工程师和培训机构多如牛毛的时代,拜师学艺并不难.但自学编程对于毫无基础的同学来说却可能是个问题,相信有过类似经历的朋友都有一把辛酸泪和一肚不吐不快的体会.让我们从一个故事说起… 故事 某君在一个 ...