首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线性回归起诉山基学习(线性回归)

线性回归起诉山基学习(线性回归)
EN

Stack Overflow用户
提问于 2018-02-20 17:21:18
回答 2查看 92关注 0票数 0

这是我的场景。

代码语言:javascript
复制
data = [[25593.14, 39426.66],
        [98411.00, 81869.75],
        [71498.80, 62495.80],
        [38068.00, 54774.00],
        [58188.00, 43453.65],
        [10220.00, 18465.25]]

关于数据是我的数据模型。

x-cordinates指“工资”y-cordinates指“费用”

当我给出“工资”时,我想预测费用,也就是X坐标。

这是我的代码样本。请帮帮我。

代码语言:javascript
复制
from sklearn.linear_model import LinearRegression

data = [[25593.14, 39426.66],
        [98411.00, 81869.75],
        [71498.80, 62495.80],
        [38068.00, 54774.00],
        [58188.00, 43453.65],
        [10220.00, 18465.25]]

salary=[]
expenses=[]

for dataset in data:
    # import pdb; pdb.set_trace()
    salary.append(dataset[0])
    expenses.append(dataset[1])

model = LinearRegression()
model.fit(salary, expenses)
prediction = model.predict([10200.00])
print(prediction)

我得到的错误:

代码语言:javascript
复制
ValueError: Expected 2D array, got 1D array instead:
array=[ 25593.14  98411.    71498.8   38068.    58188.    10220.  ].
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-20 17:39:27

正如评论中所建议的那样,这样的东西将是处理您想要输入到scikit学习模型中的数据的更好方法。另一个例子是这里

代码语言:javascript
复制
from sklearn.linear_model import LinearRegression
import numpy as np

data = np.array(
        [[25593.14, 39426.66],
        [98411.00, 81869.75],
        [71498.80, 62495.80],
        [38068.00, 54774.00],
        [58188.00, 43453.65],
        [10220.00, 18465.25]]
).T

salary = data[0].reshape(-1, 1)
expenses = data[1]

model = LinearRegression()
model.fit(salary, expenses)
prediction = model.predict(np.array([10200.00]).reshape(-1, 1))
print(prediction)
票数 4
EN

Stack Overflow用户

发布于 2018-02-20 17:38:13

快速修复,替换这一行

代码语言:javascript
复制
model.fit(np.array([salary]), np.array([expenses]))

X应该是一个数组,array([arr1,arr2,array3,...])相同的arr1和arr2是至少一个特性的数组,对于y,它应该是一个包含值array[label1,label2,label3,...]列表的数组。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48890866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档