gpt4 book ai didi

python - sklearn : how to get coefficients of polynomial features

转载 作者:太空狗 更新时间:2023-10-29 17:54:26 28 4
gpt4 key购买 nike

我知道可以使用以下方法获取多项式特征作为数字:polynomial_features.transform(X)。根据manual ,对于二度的特征是:[1, a, b, a^2, ab, b^2]。但是我如何获得高阶特征的描述呢? .get_params() 不显示任何功能列表。

最佳答案

顺便说一句,现在有更合适的功能: PolynomialFeatures.get_feature_names .

from sklearn.preprocessing import PolynomialFeatures
import pandas as pd
import numpy as np

data = pd.DataFrame.from_dict({
'x': np.random.randint(low=1, high=10, size=5),
'y': np.random.randint(low=-1, high=1, size=5),
})

p = PolynomialFeatures(degree=2).fit(data)
print p.get_feature_names(data.columns)

这将输出如下:

['1', 'x', 'y', 'x^2', 'x y', 'y^2']

注意出于某种原因,您必须先拟合 PolynomialFeatures 对象,然后才能使用 get_feature_names()。

如果您是 Pandas 爱好者(就像我一样),您可以轻松地构建具有所有新功能的 DataFrame,如下所示:

features = DataFrame(p.transform(data), columns=p.get_feature_names(data.columns))
print features

结果如下所示:

     1    x    y   x^2  x y  y^2
0 1.0 8.0 -1.0 64.0 -8.0 1.0
1 1.0 9.0 -1.0 81.0 -9.0 1.0
2 1.0 1.0 0.0 1.0 0.0 0.0
3 1.0 6.0 0.0 36.0 0.0 0.0
4 1.0 5.0 -1.0 25.0 -5.0 1.0

关于python - sklearn : how to get coefficients of polynomial features,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31290976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com