简体   繁体   English

如何使用 Sklearn 在 Python 中对列表列表进行 L2 规范化

[英]How to L2 Normalize a list of lists in Python using Sklearn

s2 = [[0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194], [0.2, 0.4892574205256839, 0.2, 0.2, 0.383258146374831], [0.3193817886456925, 0.16666666666666666, 0.16666666666666666, 0.16666666666666666, 0.3193817886456925, 0.3193817886456925], [0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194]]

from sklearn.preprocessing import normalize
X = normalize(s2)

this is throwing error:这是抛出错误:

ValueError: setting an array element with a sequence.

How to L2 Normalize a list of lists in Python using Sklearn.如何使用 Sklearn 在 Python 中对列表列表进行 L2 规范化。

Since I don't have enough reputation to comment;由于我没有足够的声誉发表评论; hence posting it as an answer.因此将其发布为答案。

Let's quickly look at your datapoint.让我们快速查看您的数据点。

I have converted the given datapoint into NumPy array.我已将给定的数据点转换为 NumPy 数组。 Since it doesn't have the same length, so it will look like.由于它没有相同的长度,所以它看起来像。

>>> n2 = np.array([[0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194], [0.2, 0.4892574205256839, 0.2, 0.2, 0.383258146374831], [0.3193817886456925, 0.16666666666666666, 0.16666666666666666, 0.16666666666666666, 0.3193817886456925, 0.3193817886456925], [0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194]])
>>> n2
array([list([0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194]),
       list([0.2, 0.4892574205256839, 0.2, 0.2, 0.383258146374831]),
       list([0.3193817886456925, 0.16666666666666666, 0.16666666666666666, 0.16666666666666666, 0.3193817886456925, 0.3193817886456925]),
       list([0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194])],
      dtype=object)

And you can see here that converted values are not in Sequence of Values and to achieve this you need to keep the same length for the internal list ( looks like 0.16666666666666666 is copied multiple time in your array; if not then fix the length), it will look like您可以在这里看到转换后的值不在值序列中,为此您需要为内部列表保持相同的长度(看起来像 0.166666666666666666 在您的数组中被多次复制;如果不是,则固定长度),它看起来像

>>> n3 = np.array([[0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194], [0.2, 0.4892574205256839, 0.2, 0.2, 0.383258146374831], [0.3193817886456925, 0.16666666666666666, 0.16666666666666666, 0.16666666666666666, 0.319381788645692], [0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194]])
>>> n3
array([[0.2       , 0.2       , 0.2       , 0.30216512, 0.24462871],
       [0.2       , 0.48925742, 0.2       , 0.2       , 0.38325815],
       [0.31938179, 0.16666667, 0.16666667, 0.16666667, 0.31938179],
       [0.2       , 0.2       , 0.2       , 0.30216512, 0.24462871]])

As you can see now n3 has become a sequence of values.正如你现在看到的,n3 已经变成了一个值序列。

and if you use normalize function, it simply works如果您使用 normalize 功能,它就可以正常工作

>>> X = normalize(n3)
>>> X
array([[0.38408524, 0.38408524, 0.38408524, 0.58028582, 0.46979139],
       [0.28108867, 0.6876236 , 0.28108867, 0.28108867, 0.53864762],
       [0.59581303, 0.31091996, 0.31091996, 0.31091996, 0.59581303],
       [0.38408524, 0.38408524, 0.38408524, 0.58028582, 0.46979139]])

How to use NumPy array to avoid this issue, please have a look at this SO link ValueError: setting an array element with a sequence如何使用 NumPy 数组避免此问题,请查看此 SO 链接ValueError: setting an array element with a sequence

Important: I removed one element from the 3rd list in order for all lists to have the same length.重要提示:我从第三个列表中删除了一个元素,以使所有列表的长度相同。

I did that cause I really believe that it's a copy-paste error.我这样做是因为我真的相信这是一个复制粘贴错误。 If not, comment below and I will modify my answer.如果没有,请在下面评论,我会修改我的答案。

import numpy as np

s2 = [[0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194], [0.2, 0.4892574205256839, 0.2, 0.2, 0.383258146374831], [0.3193817886456925, 0.16666666666666666, 0.16666666666666666, 0.3193817886456925, 0.3193817886456925], [0.2, 0.2, 0.2, 0.3021651247531982, 0.24462871026284194]]

X = normalize(np.array(s2))

暂无
暂无

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

相关问题 python sklearn:“ sklearn.preprocessing.normalize(X,norm ='l2')”和“ sklearn.svm.LinearSVC(penalty ='l2')”之间有什么区别 - python sklearn: what is the different between “sklearn.preprocessing.normalize(X, norm='l2')” and “sklearn.svm.LinearSVC(penalty='l2')” 在python中使用L2范数的LAD? (sklearn) - LAD with L2 norm in python? (sklearn) 在sklearn python中撤消L2规范化 - Undo L2 Normalization in sklearn python 具有 Logloss 和 L2 正则化的 SGD 分类器 使用 SGD 而不使用 sklearn python - SGD Classifier with Logloss and L2 regularization Using SGD without using sklearn python 如何使用 L2 范数(python)在描述符中查找匹配项? - How to find matches in descriptors using L2 norm (python)? sklearn.preprocessing.normalize中的norm ='l2'对于矩阵归一化有什么作用? - What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization? 使用 Python 将列表 l 中以 'S' 开头的项目保存到 l2 - Save items starting with 'S' from list l to l2 using Python 如何规范化python中的字符串列表? - how to normalize list of lists of strings in python? 在需要使用单位L2范数进行输出的回归问题中,如何标准化Keras网络输出? - How to normalize Keras network output in a regression problem that demands output with unit L2 norm? 使用 SGD 而不使用 sklearn 实现具有 Logloss 和 L2 正则化的 SGD 分类器 - Implement SGD Classifier with Logloss and L2 regularization Using SGD without using sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM