【问题标题】:difference between np.linalg.lstsq and linear regression in scikit learnscikit learn中np.linalg.lstsq和线性回归的区别
【发布时间】:2015-02-14 17:21:32
【问题描述】:

comb 1 是一个 pandas 数据框,具有以下值。

yearID    teamID    薪水        W
408        ANA       51464167    82
409        ARI      81027833     85

当我使用 np.linalg.lstsq 时,我可以打印 dfg 数据框。

dfg = pd.DataFrame()

comb1 = combined[combined['yearID'] == 2000]
x1 = comb1['salary'].values /1000000 
y1 =comb1['W'].values
A1 = np.array([x1, np.ones(len(x1))])
w1 = np.linalg.lstsq(A1.T,y1)[0]
yq = (w1[0]*x1+w1[1])
dfg['New val'] = y1 - yq

当我使用 scikit learn libary 进行线性回归并执行相同的操作时,我得到一个值错误

from sklearn.linear_model import LinearRegression
fg = pd.DataFrame()

x2 = comb1['salary'].values /1000000 
y2 =comb1['W'].values

x2_reshape = x2.reshape(-1,1)
y2_reshape = y2.reshape(-1,1)

clf1 = LinearRegression()
clf1.fit(x2_reshape, y2_reshape)
predicted_train = clf1.predict(x2_reshape)

 x_pre = y2 - predicted_train 
fg['New val'] = x_pre

这两者有什么区别??请帮助我!

【问题讨论】:

  • 请发布您的代码结果和错误。

标签: pandas scikit-learn


【解决方案1】:

他们应该是same:

Notes

From the implementation point of view, this is just plain Ordinary Least Squares (scipy.linalg.lstsq) wrapped as a predictor object.

如果您遇到错误,可能是因为您设置数据的方式。

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2019-05-14
    • 2017-12-25
    • 2016-10-23
    • 2018-07-31
    • 2014-06-19
    • 2016-05-10
    • 2017-03-26
    • 2021-04-01
    相关资源
    最近更新 更多