使用sklearn和线性回归的错误:形状(1,16)和(1,1)不对齐:16(dim 1)!= 1(dim 0) [英] Error using sklearn and linear regression: shapes (1,16) and (1,1) not aligned: 16 (dim 1) != 1 (dim 0)

查看:282
本文介绍了使用sklearn和线性回归的错误:形状(1,16)和(1,1)不对齐:16(dim 1)!= 1(dim 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解机器学习的知识,偶然发现了youtube siraj和他的Udacity视频,想尝试一些事情.

I watned to learn about machine learning and I stumbled upon youtube siraj and his Udacity videos and wanted to try and pick up a few things.

他的参考视频: https://www .youtube.com/watch?v = vOppzHpvTiQ& index = 1& list = PL2-dafEMk2A7YdKv4XfKpfbTH5z6rEEj3

在他的视频中,他有一个导入并读取的txt文件,但是当我尝试重新创建该txt文件时,无法正确读取该文件.相反,我尝试用相同的数据创建一个熊猫数据框,并对它执行线性回归/预测,但是随后出现以下错误.

In his video, he had a txt file he imported and read, but when I tried to recreate the the txt file it couldnt be read in correctly. Instead, I tried to create a pandas dataframe with the same data and perform the linear regression/predict on it, but then I got the below error.

发现输入变量的样本数量不一致:[1,16],以及关于传递1d数组的一些事情,我需要重塑它们.

Found input variables with inconsistent numbers of samples: [1, 16] and something about passing 1d arrays and I need to reshape them.

然后,当我尝试在这篇文章之后重塑它们时:

Then when I tried to reshape them following this post: Sklearn : ValueError: Found input variables with inconsistent numbers of samples: [1, 6]

我收到此错误....

(1,16)和(1,1)形状未对齐:16(dim 1)!= 1(dim 0)

这是我下面的代码.我知道这可能是语法错误,我只是对这个scklearn还不熟悉,并且需要一些帮助.

This is my code down below. I know it's probably a syntax error, I'm just not familiar with this scklearn yet and would like some help.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model

#DF = pd.read_fwf('BrainBodyWeight.txt')
DF = pd.DataFrame()
DF['Brain'] = [3.385, .480, 1.350, 465.00,36.330, 27.660, 14.830, 1.040, 4.190, 0.425, 0.101, 0.920, 1.000, 0.005, 0.060, 3.500 ]

DF['Body'] = [44.500, 15.5, 8.1, 423, 119.5, 115, 98.2, 5.5,58, 6.40, 4, 5.7,6.6, .140,1, 10.8]

try:
    x = DF['Brain']
    y = DF['Body']

    x = x.tolist()
    y = y.tolist()

    x = np.asarray(x)
    y = np.asarray(y)


    body_reg = linear_model.LinearRegression()
    body_reg.fit(x.reshape(-1,1),y.reshape(-1,1))
    plt.scatter(x,y)
    plt.plot(x,body_reg.predict(x))
    plt.show()
except Exception as e:
    print(e)

任何人都可以解释为什么sklearn不喜欢我的输入吗????

Can anyone explain why sklearn doesn't like my input????

推荐答案

来自

From documentation LinearRegression.fit() requires an x array with [n_samples,n_features] shape. So that's why you are reshaping your x array before calling fit. Since if you don't you'll have an array with (16,) shape, which does not meet the required [n_samples,n_features] shape, there are no n_features given.

x = DF['Brain']
x = x.tolist()
x = np.asarray(x)

# 16 samples, None feature
x.shape
(16,)

# 16 samples, 1 feature
x.reshape(-1,1).shape
(16,1)

The same requirement goes for the LinearRegression.predict function (and also for consistency), you just simply need to do the same reshaping when calling the predict function.

plt.plot(x,body_reg.predict(x.reshape(-1,1)))

或者,您也可以在调用任何函数之前重塑x数组.

Or alternatively you can just reshape the x array before calling any functions.

作为功能参考,您只需调用DF['Brain'].values即可轻松获取内部numpy值数组.您无需将其强制转换为list-> numpy array.因此,您可以只使用此方法,而不必使用所有转换方法:

And for feature reference, you can easily get the inner numpy array of values by just calling DF['Brain'].values. You don't need to cast it to list -> numpy array. So you can just use this instead of all the conversion:

x = DF['Brain'].values.reshape(1,-1)
y = DF['Body'].values.reshape(1,-1)

body_reg = linear_model.LinearRegression()
body_reg.fit(x, y)

这篇关于使用sklearn和线性回归的错误:形状(1,16)和(1,1)不对齐:16(dim 1)!= 1(dim 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆