创建多项式特征矩阵
我正在尝试构建类似于R中的Python的Sklearn PolyenmialFeatures的多项式特征矩阵。不幸的是,我找不到任何具有相似函数的现有软件包。我不了解此类功能矩阵的基本统计数据 - 任何帮助或指针都非常感谢!
Sklearn文档将其解释为:生成一个新的特征矩阵,该矩阵由该特征的所有多项式组合组成,该特征的程度小于或等于指定度。例如,如果输入样本为二维和形式[a,b],则第二个多项式特征为[1,a,b,a^2,ab,b^2]。
我尝试复制的Python代码如下:
x1 = 298
x2 = 35
x3 = 0.05
x4 = 0.01
X = np.vstack([x1, np.log(x1), x2, x3, x4]).T
poly = PolynomialFeatures(degree=3)
X_ = poly.fit_transform(X)
X_
[24]:
array([[1.00000000e+00, 2.98000000e+02, 5.69709349e+00, 3.50000000e+01,
5.00000000e-02, 1.00000000e-02, 8.88040000e+04, 1.69773386e+03,
1.04300000e+04, 1.49000000e+01, 2.98000000e+00, 3.24568742e+01,
1.99398272e+02, 2.84854674e-01, 5.69709349e-02, 1.22500000e+03,
1.75000000e+00, 3.50000000e-01, 2.50000000e-03, 5.00000000e-04,
1.00000000e-04, 2.64635920e+07, 5.05924690e+05, 3.10814000e+06,
4.44020000e+03, 8.88040000e+02, 9.67214851e+03, 5.94206851e+04,
8.48866929e+01, 1.69773386e+01, 3.65050000e+05, 5.21500000e+02,
1.04300000e+02, 7.45000000e-01, 1.49000000e-01, 2.98000000e-02,
1.84909847e+02, 1.13599060e+03, 1.62284371e+00, 3.24568742e-01,
6.97893952e+03, 9.96991360e+00, 1.99398272e+00, 1.42427337e-02,
2.84854674e-03, 5.69709349e-04, 4.28750000e+04, 6.12500000e+01,
1.22500000e+01, 8.75000000e-02, 1.75000000e-02, 3.50000000e-03,
1.25000000e-04, 2.50000000e-05, 5.00000000e-06, 1.00000000e-06]])
I am trying to build a polynomial feature matrix similar to python's sklearn PolynomialFeatures in R. Unfortunately I could not find any existing packages with a similar function. I don't understand the underlying statistics of such a feature matrix - any help or pointers are very much appreciated!
The sklearn docs explain it as: Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].
The python code I try to replicate is the following:
x1 = 298
x2 = 35
x3 = 0.05
x4 = 0.01
X = np.vstack([x1, np.log(x1), x2, x3, x4]).T
poly = PolynomialFeatures(degree=3)
X_ = poly.fit_transform(X)
X_
[24]:
array([[1.00000000e+00, 2.98000000e+02, 5.69709349e+00, 3.50000000e+01,
5.00000000e-02, 1.00000000e-02, 8.88040000e+04, 1.69773386e+03,
1.04300000e+04, 1.49000000e+01, 2.98000000e+00, 3.24568742e+01,
1.99398272e+02, 2.84854674e-01, 5.69709349e-02, 1.22500000e+03,
1.75000000e+00, 3.50000000e-01, 2.50000000e-03, 5.00000000e-04,
1.00000000e-04, 2.64635920e+07, 5.05924690e+05, 3.10814000e+06,
4.44020000e+03, 8.88040000e+02, 9.67214851e+03, 5.94206851e+04,
8.48866929e+01, 1.69773386e+01, 3.65050000e+05, 5.21500000e+02,
1.04300000e+02, 7.45000000e-01, 1.49000000e-01, 2.98000000e-02,
1.84909847e+02, 1.13599060e+03, 1.62284371e+00, 3.24568742e-01,
6.97893952e+03, 9.96991360e+00, 1.99398272e+00, 1.42427337e-02,
2.84854674e-03, 5.69709349e-04, 4.28750000e+04, 6.12500000e+01,
1.22500000e+01, 8.75000000e-02, 1.75000000e-02, 3.50000000e-03,
1.25000000e-04, 2.50000000e-05, 5.00000000e-06, 1.00000000e-06]])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
poly
例如,请注意,订购将有所不同。
另请注意,Python代码不正确。如果x是列,则不要转置。在这种情况下,您将拥有每种语言的正确值:
in R:
Use
poly
egNote that the ordering will be different.
Also Note that the python code is incorrect. If X is a column, then do not transpose. in that case you will have the correct values from each language:
in R: