首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有numpy的多元多项式回归

具有numpy的多元多项式回归
EN

Stack Overflow用户
提问于 2012-06-12 05:55:37
回答 2查看 49.8K关注 0票数 26

我有很多样本,其中假设ya,b,c中作为多项式变化到一定程度。例如,对于给定的数据集和次数2,我可能会生成模型

y = a^2 + 2ab - 3cb + c^2 +.5ac

这可以使用最小二乘来完成,并且是numpy的polyfit例程的一个轻微扩展。在Python生态系统中有没有标准实现?

EN

回答 2

Stack Overflow用户

发布于 2015-07-17 10:23:38

sklearn提供了一种简单的方法来实现这一点。

根据发布的here构建示例

#X is the independent variable (bivariate in this case)
X = array([[0.44, 0.68], [0.99, 0.23]])

#vector is the dependent data
vector = [109.85, 155.72]

#predict is an independent variable for which we'd like to predict the value
predict= [0.49, 0.18]

#generate a model of polynomial features
poly = PolynomialFeatures(degree=2)

#transform the x data for proper fitting (for single variable type it returns,[1,x,x**2])
X_ = poly.fit_transform(X)

#transform the prediction to fit the model type
predict_ = poly.fit_transform(predict)

#here we can remove polynomial orders we don't want
#for instance I'm removing the `x` component
X_ = np.delete(X_,(1),axis=1)
predict_ = np.delete(predict_,(1),axis=1)

#generate the regression object
clf = linear_model.LinearRegression()
#preform the actual regression
clf.fit(X_, vector)

print("X_ = ",X_)
print("predict_ = ",predict_)
print("Prediction = ",clf.predict(predict_))

下面是输出:

>>> X_ =  [[ 0.44    0.68    0.1936  0.2992  0.4624]
>>>  [ 0.99    0.23    0.9801  0.2277  0.0529]]
>>> predict_ =  [[ 0.49    0.18    0.2401  0.0882  0.0324]]
>>> Prediction =  [ 126.84247142]
票数 19
EN

Stack Overflow用户

发布于 2016-09-30 23:09:23

sklearn有一个使用他们的管道here的很好的例子。下面是他们示例的核心:

polynomial_features = PolynomialFeatures(degree=degrees[i],
                                         include_bias=False)
linear_regression = LinearRegression()
pipeline = Pipeline([("polynomial_features", polynomial_features),
                     ("linear_regression", linear_regression)])
pipeline.fit(X[:, np.newaxis], y)

您不需要自己转换数据--只需将其传递到管道中即可。

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

https://stackoverflow.com/questions/10988082

复制
相关文章

相似问题

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