PolynomialFeatures#
- class sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C')[源代码]#
生成多项和交互功能。
生成一个新的特征矩阵,由次数小于或等于指定次数的特征的所有多项组合组成。例如,如果输入样本是二维的并且是 [a, b] ,2次多项特征为 [1, a, b, a^2, ab, b^2] .
阅读更多的 User Guide .
- 参数:
- degreeint或tuple(min_degree,max_degree),默认=2
如果给出单个int,则它指定多项特征的最大次数。如果一个二元组
(min_degree, max_degree)
通过了,min_degree
是最低值,max_degree
是生成特征的最大多次。注意min_degree=0
和min_degree=1
等效,因为输出零度项由以下公式确定include_bias
.- interaction_only布尔,默认=假
If
True
, only interaction features are produced: features that are products of at mostdegree
distinct input features, i.e. terms with power of 2 or higher of the same input feature are excluded:包括:
x[0]
,x[1]
,x[0] * x[1]
等。排除:
x[0] ** 2
,x[0] ** 2 * x[1]
等。
- include_bias布尔,默认=True
如果
True
(默认),然后包括一个偏差列,该特征中的所有函数均为零(即一列1-充当线性模型中的拦截项)。- order' C ',',默认=' C '
密集情况下输出数组的顺序。
'F'
顺序的计算速度更快,但可能会减慢后续估计的速度。Added in version 0.21.
- 属性:
powers_
:nd形状数组 (n_output_features_
,n_features_in_
)nd数组形状(输出中每个输入的指数。
- n_features_in_int
期间看到的功能数量 fit .
Added in version 0.24.
- feature_names_in_ :nd形状数组 (
n_features_in_
,)nd数组形状( Names of features seen during fit. Defined only when
X
has feature names that are all strings.Added in version 1.0.
- n_output_features_int
多项输出特征的总数。输出特征的数量是通过迭代所有适当大小的输入特征组合来计算的。
参见
SplineTransformer
为特征生成一元B样条基的Transformer。
注意到
请注意,输出数组中的特征数量与输入数组的特征数量成正比,与程度成正比。过高的度数可能会导致过度贴合。
看到 examples/linear_model/plot_polynomial_interpolation.py
示例
>>> import numpy as np >>> from sklearn.preprocessing import PolynomialFeatures >>> X = np.arange(6).reshape(3, 2) >>> X array([[0, 1], [2, 3], [4, 5]]) >>> poly = PolynomialFeatures(2) >>> poly.fit_transform(X) array([[ 1., 0., 1., 0., 0., 1.], [ 1., 2., 3., 4., 6., 9.], [ 1., 4., 5., 16., 20., 25.]]) >>> poly = PolynomialFeatures(interaction_only=True) >>> poly.fit_transform(X) array([[ 1., 0., 1., 0.], [ 1., 2., 3., 6.], [ 1., 4., 5., 20.]])
- fit(X, y=None)[源代码]#
计算输出特征的数量。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
数据。
- y忽视
未使用,此处列出是为了按照惯例实现API一致性。
- 返回:
- self对象
已安装Transformer。
- fit_transform(X, y=None, **fit_params)[源代码]#
适应数据,然后对其进行转换。
适合变压器
X
和y
具有可选参数fit_params
并返回的转换版本X
.- 参数:
- X形状类似阵列(n_samples,n_features)
输入样本。
- y形状为(n_samples,)或(n_samples,n_outputs)的阵列状, 默认值=无
目标值(无监督转换)。
- **fit_paramsdict
其他适合参数。
- 返回:
- X_newndray形状数组(n_samples,n_features_new)
变形的数组。
- get_feature_names_out(input_features=None)[源代码]#
获取用于转换的输出要素名称。
- 参数:
- input_features字符串或无的类数组,默认=无
输入功能。
如果
input_features is None
那么feature_names_in_
在中用作功能名称。如果feature_names_in_
未定义,则生成以下输入要素名称:["x0", "x1", ..., "x(n_features_in_ - 1)"]
.如果
input_features
是一个类似阵列的,那么input_features
必须匹配feature_names_in_
如果feature_names_in_
是定义的。
- 返回:
- feature_names_out字符串对象的nd数组
转换的功能名称。
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_params(deep=True)[源代码]#
获取此估计器的参数。
- 参数:
- deep布尔,默认=True
如果为True,将返回此估计量和包含的作为估计量的子对象的参数。
- 返回:
- paramsdict
参数名称映射到其值。
- set_output(*, transform=None)[源代码]#
设置输出容器。
看到 介绍 set_output API 了解如何使用API的示例。
- 参数:
- transform{“默认”,“pandas”,“polars”},默认=无
配置输出
transform
和fit_transform
."default"
:Transformer的默认输出格式"pandas"
:DataFrame输出"polars"
:两极输出None
:转换配置不变
Added in version 1.4:
"polars"
添加了选项。
- 返回:
- self估计器实例
估计实例。
- set_params(**params)[源代码]#
设置此估计器的参数。
该方法适用于简单估计器以及嵌套对象(例如
Pipeline
).后者具有以下形式的参数<component>__<parameter>
以便可以更新嵌套对象的每个组件。- 参数:
- **paramsdict
估计参数。
- 返回:
- self估计器实例
估计实例。
- transform(X)[源代码]#
将数据转换为多项特征。
- 参数:
- X形状(n_samples,n_features)的{类数组,稀疏矩阵}
要逐行转换的数据。
对于稀疏输入(为了速度),更喜欢CSR而不是CSC,但如果学位为4或更高,则需要CSC。如果度数小于4并且输入格式为CSC,则将转换为CSR,生成其多项特征,然后转换回CSC。
If the degree is 2 or 3, the method described in "Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers" by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.
- 返回:
- XP{ndarray,sparse matrix}的形状(n_samples,NP)
特征矩阵,其中
NP
是从输入组合生成的多项特征的数量。如果提供了稀疏矩阵,则会将其转换为稀疏矩阵csr_matrix
.