分类数据:特征组合
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
特征组合是通过对数据集中两个或更多分类特征或分桶特征进行组合(取笛卡尔积)而创建的。与多项式转换一样,特征组合可让线性模型处理非线性问题。特征组合还会编码特征之间的互动。
例如,假设有一个包含以下分类特征的叶子数据集:
edges
,包含值 smooth
、toothed
和 lobed
arrangement
,包含值 opposite
和 alternate
假设上述顺序是 one-hot 表示法中特征列的顺序,因此具有 smooth
个边缘和 opposite
个排列方式的叶子表示为 {(1, 0, 0), (1, 0)}
。
这两个地图项的交叉项(即笛卡尔积)为:
{Smooth_Opposite, Smooth_Alternate, Toothed_Opposite, Toothed_Alternate,
Lobed_Opposite, Lobed_Alternate}
其中每个项的值为基准地图项值的乘积,即:
Smooth_Opposite = edges[0] * arrangement[0]
Smooth_Alternate = edges[0] * arrangement[1]
Toothed_Opposite = edges[1] * arrangement[0]
Toothed_Alternate = edges[1] * arrangement[1]
Lobed_Opposite = edges[2] * arrangement[0]
Lobed_Alternate = edges[2] * arrangement[1]
例如,如果叶子具有 lobed
边和 alternate
排列方式,则特征交叉矢量将为 Lobed_Alternate
的值为 1,所有其他项的值为 0:
{0, 0, 0, 0, 0, 1}
由于这些特征在同一树种中不会发生变化,因此此数据集可用于按树种对叶子进行分类。
点击此处比较多项式转换与特征交叉
特征交叉有点类似于
多项式转换。这两种方法都会将多个特征组合成一个新的合成特征,以便模型进行训练以学习非线性特征。多项式转换通常会组合数字数据,而特征交叉会组合分类数据。
何时使用特征交叉
领域知识可以提供有用的特征组合建议。如果没有这些领域知识,很难手动确定有效的特征交叉或多项式转换。如果计算成本较高,通常可以使用神经网络在训练期间自动发现并应用有用的特征组合。
请注意,对两个稀疏特征进行交叉会产生比这两个原始特征更稀疏的新特征。例如,如果特征 A 是 100 个元素的稀疏特征,特征 B 是 200 个元素的稀疏特征,则 A 和 B 的特征交叉会产生 2 万个元素的稀疏特征。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-05-23。
[null,null,["最后更新时间 (UTC):2025-05-23。"],[[["\u003cp\u003eFeature crosses are created by combining two or more categorical or bucketed features to capture interactions and nonlinearities within a dataset.\u003c/p\u003e\n"],["\u003cp\u003eThey enable linear models to handle nonlinearities similar to polynomial transforms, but feature crosses work with categorical data while polynomial transforms are applied to numerical data.\u003c/p\u003e\n"],["\u003cp\u003eFeature crosses can be particularly effective when guided by domain expertise, but using neural networks can automate the process of discovering valuable combinations.\u003c/p\u003e\n"],["\u003cp\u003eOveruse of feature crosses with sparse features should be avoided, as it can lead to excessive sparsity in the resulting feature set.\u003c/p\u003e\n"]]],[],null,["# Categorical data: Feature crosses\n\n[**Feature crosses**](/machine-learning/glossary#feature-cross) are created by\ncrossing (taking the Cartesian product of) two or more categorical or bucketed\nfeatures of the dataset. Like [polynomial\ntransforms](/machine-learning/crash-course/numerical-data/polynomial-transforms),\nfeature crosses allow linear models to handle nonlinearities. Feature crosses\nalso encode interactions between features.\n\nFor example, consider a leaf dataset with the categorical features:\n\n- `edges`, containing values `smooth`, `toothed`, and `lobed`\n- `arrangement`, containing values `opposite` and `alternate`\n\nAssume the order above is the order of the feature columns in a one-hot\nrepresentation, so that a leaf with `smooth` edges and `opposite` arrangement\nis represented as `{(1, 0, 0), (1, 0)}`.\n\nThe feature cross, or Cartesian product, of these two features would be:\n\n`{Smooth_Opposite, Smooth_Alternate, Toothed_Opposite, Toothed_Alternate,\nLobed_Opposite, Lobed_Alternate}`\n\nwhere the value of each term is the product of the base feature values, such\nthat:\n\n- `Smooth_Opposite = edges[0] * arrangement[0]`\n- `Smooth_Alternate = edges[0] * arrangement[1]`\n- `Toothed_Opposite = edges[1] * arrangement[0]`\n- `Toothed_Alternate = edges[1] * arrangement[1]`\n- `Lobed_Opposite = edges[2] * arrangement[0]`\n- `Lobed_Alternate = edges[2] * arrangement[1]`\n\nFor example, if a leaf has a `lobed` edge and an `alternate` arrangement, the\nfeature-cross vector will have a value of 1 for `Lobed_Alternate`, and a value\nof 0 for all other terms:\n\n`{0, 0, 0, 0, 0, 1}`\n\nThis dataset could be used to classify leaves by tree species, since these\ncharacteristics do not vary within a species.\n**Click here to compare polynomial transforms\nwith feature crosses** \n\nFeature crosses are somewhat analogous to\n[Polynomial transforms](/machine-learning/crash-course/numerical-data/polynomial-transforms).\nBoth combine multiple features into a new synthetic feature that the model can\ntrain on to learn nonlinearities. Polynomial transforms typically combine\nnumerical data, while feature crosses combine categorical data.\n\nWhen to use feature crosses\n---------------------------\n\nDomain knowledge can suggest a useful combination of features\nto cross. Without that domain knowledge, it can be difficult to determine\neffective feature crosses or polynomial transforms by hand. It's often possible,\nif computationally expensive, to use\n[neural networks](/machine-learning/crash-course/neural-networks) to\n*automatically* find and apply useful feature combinations during training.\n\nBe careful---crossing two sparse features produces an even sparser new\nfeature than the two original features. For example, if feature A is a\n100-element sparse feature and feature B is a 200-element sparse feature,\na feature cross of A and B yields a 20,000-element sparse feature.\n| **Key terms:**\n|\n| - [Feature cross](/machine-learning/glossary#feature-cross)\n- [Neural network](/machine-learning/glossary#neural_network) \n[Help Center](https://support.google.com/machinelearningeducation)"]]