简体   繁体   English

使用Scikitlearn进行线性回归(线性回归)

[英]Linear regression suing Scikitlearn(linear regression)

Here is my scenarion. 这是我的场景。

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]]

About data is my data model. 关于数据是我的数据模型。

x-cordinates refers "Salary" y-cordinates refers "Expenses" x坐标表示“工资” y坐标表示“费用”

I want to predict the expense when I give "Salary" ie, X-coordinate. 我想预测当我给出“薪水”即X坐标时的费用。

Here is my sample code. 这是我的示例代码。 Please help me out. 请帮帮我。

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)

Error which I got: 我得到的错误:

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

.

As suggested by the comments, something like this would be a better way to work with data you want to feed into a scikit learn model. 正如评论所建议的那样,类似这样的方法将是处理要输入到scikit学习模型中的数据的更好方法。 Another example can be seen here . 这里可以看到另一个例子。

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)

quick fix, replace this line 快速修复,替换此行

model.fit(np.array([salary]), np.array([expenses]))

X is expected to be an array of arrays, array([arr1,arr2,array3,...]) same of arr1 and arr2 being arrays of at least one feature, same for y,it should be an array of containing a list of values array[label1,label2,label3,...] X应该是一个数组数组, array([arr1,arr2,array3,...])与arr1相同,而arr2是至少一个特征的数组,与y相同,它应该是包含列表的数组的值array[label1,label2,label3,...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM