简体   繁体   English

Python:将2d数组的每一行缩放为总和为1的值。每一行包含一些负值

[英]Python: Scale each row of 2d array to values that sum to 1. Each row contains some negative values

Let's say I have an array: 假设我有一个数组:

myArray = [[-1.58, -1.09, -0.41,  0.22, -0.95],
          [-1.16, -1.27, -1.89, -1.01,  1.11],
          [-0.73, -0.81, -0.47, -0.46, -0.04],
          [-1.46, -0.82,  0.40, -0.22, -1.82],
          [-1.12 , -0.97, -0.89, -0.18,  0.06]]

I wish to convert each line of this array into values that sum to 1 for each row. 我希望将此数组的每一行转换为每行总和为1的值。

Is this easily possible? 这容易吗?

My actual array is several thousand lines long, so I would like a solution that optimizes well if possible. 我的实际数组长数千行,所以我希望有一个可能优化的解决方案。 Thank you very much! 非常感谢你!

I realize that I'm not being clear. 我意识到我不清楚。 I want the resulting values to be positive and sum to 1 . 我希望结果值是正数总和为1 Sorry. 抱歉。

I can give you a sample using positive values (That's total at the end): 我可以给您一个使用正值的样本(最后是总计):


Row1 1.10 2.20 3.30 4.40 5.50 (Total = 16.50) 第1行1.10 2.20 3.30 4.40 5.50(总计= 16.50)
Row2 2.20 3.30 4.40 5.50 6.60 (Total = 22.00) 第2行2.20 3.30 4.40 5.50 6.60(总计= 22.00)
Row3 4.20 5.01 2.50 3.30 1.10 (Total = 16.11) 第3行4.20 5.01 2.50 3.30 1.10(总计= 16.11)

to (again total at the end):: 到(最后总计)::


Row1 0.07 0.13 0.20 0.27 0.33 (Total = 1.00) 第1行0.07 0.13 0.20 0.27 0.33(总计= 1.00)
Row2 0.10 0.15 0.20 0.25 0.30 (Total = 1.00) 第2行0.10 0.15 0.20 0.25 0.30(总计= 1.00)
Row3 0.26 0.31 0.16 0.20 0.07 (Total = 1.00) 第3行0.26 0.31 0.16 0.20 0.07(总计= 1.00)

And i achieve this by simply adding a row, then diving each cell in each row by the total of that row. 我可以通过简单地添加一行,然后将每一行中的每个单元格除以该行的总数来实现。 I don't know how to achieve this in python with an array, with negative values. 我不知道如何在带有负值的数组的python中实现这一点。

First using min-max normalization to transform original data, this could be one approach: 首先使用最小-最大规范化来转换原始数据,这可能是一种方法:

myArray = [[-1.58, -1.09, -0.41, 0.22, -0.95], 
[-1.16, -1.27, -1.89, -1.01, 1.11], 
[-0.73, -0.81, -0.47, -0.46, -0.04], 
[-1.46, -0.82, 0.40, -0.22, -1.82], 
[-1.12 , -0.97, -0.89, -0.18, 0.06]]

#Transform data
normalizedArray = []

for row in range(0, len(myArray)):
    list = []
    Min =  min(myArray[row])
    Max = max(myArray[row])

    for element in myArray[row]:
        list.append(  float(element-Min)/float(Max- Min) )

    normalizedArray.append(list)

#Normalize to 1
newArray = []

for row in range(0, len(normalizedArray)):
    list = [x / sum(normalizedArray[row]) for x in normalizedArray[row]]
    newArray.append(list)

As I say, I don't think you can achieve exactly what you need (because if you have a mix of positive and negative values, you'll always have a mix of positive and negative values in the ratio of the value to the sum of the row). 正如我所说,我认为您无法完全实现所需的功能(因为如果混合使用正值和负值,则总会在值与总和之比中混合使用正值和负值行)。 But this gets close, I think. 我想这已经接近了。

import numpy as np

myArray = [[-1.58, -1.09, -0.41, 0.22, -0.95], 
[-1.16, -1.27, -1.89, -1.01, 1.11], 
[-0.73, -0.81, -0.47, -0.46, -0.04], 
[-1.46, -0.82, 0.40, -0.22, -1.82], 
[-1.12 , -0.97, -0.89, -0.18, 0.06]]

new_array = abs(np.asarray(new_array))

ratio_array = np.divide(new_array, new_array.sum(axis=1))

EDIT: I've used %timeit , and a numpy method is 10x faster than the looping method above. 编辑:我已经使用%timeit ,并且numpy方法比上面的循环方法快10倍。

new_array = np.asarray(myArray)

transformed_array = new_array + (np.min(new_array, axis=1) * -1)[:, None]

ratio_matrix = transformed_array / np.sum(transformed_array, axis=1)[:, None]

Is it what you're asking for: 这是您要的吗?

myArray = [[-1.58, -1.09, -0.41, 0.22, -0.95], 
[-1.16, -1.27, -1.89, -1.01, 1.11], 
[-0.73, -0.81, -0.47, -0.46, -0.04], 
[-1.46, -0.82, 0.40, -0.22, -1.82], 
[-1.12 , -0.97, -0.89, -0.18, 0.06]] 


print [sum(_list) for _list in myArray]

?

[-3.8099999999999996, -4.219999999999999, -2.51, -3.92, -3.1]

Here's a working example: 这是一个工作示例:

data = [[-1.58, -1.09, -0.41, 0.22, -0.95],
        [-1.16, -1.27, -1.89, -1.01, 1.11],
        [-0.73, -0.81, -0.47, -0.46, -0.04],
        [-1.46, -0.82, 0.40, -0.22, -1.82],
        [-1.12, -0.97, -0.89, -0.18, 0.06]]


print[[x / sum(data[r]) for x in data[r]] for r in range(0, len(data))]

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

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