简体   繁体   English

在 Sklearn 中规范化 function 需要二维数组

[英]Normalize function in Sklearn requires 2D array

In linear algebra, vectors are normalized when they are divided by their norm, that is, the squared sum of their components.在线性代数中,向量在除以范数时被归一化,即它们的分量的平方和。

Yet, sklearn.preprocessing.normalize method does not accept vectors, only matrices of at least two columns:然而, sklearn.preprocessing.normalize 方法不接受向量,只接受至少两列的矩阵:

"ValueError: Expected 2D array, got 1D array instead" “ValueError:预期的 2D 数组,得到 1D 数组”

Why?为什么?

normalize works on a data set, not a vector. normalize适用于数据集,而不是向量。 You have the wrong definition of "normalize" for this function .对此 function的“规范化”定义错误。 It works on individual vectors.它适用于单个向量。 If you give it a 2D array of a single column (shape of [N, 1]), you can get your vector normalized in the "normal" fashion.如果你给它一个单列的二维数组([N,1] 的形状),你可以让你的向量以“正常”的方式归一化。

According to the documentation for sklearn.preprocessing.normalize , the parameter x is the data to normalize, element by element, and has the shape [n_samples, n_features].根据sklearn.preprocessing.normalize的文档,参数x是要归一化的数据,逐个元素,形状为 [n_samples, n_features]。 The function normalize perform this operation on a single array-like dataset, either using the L1 or L2 norms. function normalize使用 L1 或 L2 规范对单个类似数组的数据集执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM