这是将表中该列中的最大值除以每列中的每个单元格的有效或正确方法吗?是否有更好的实现方式(如果正确的话)?注意:所有值> = 0
new_data = [];
for row in np.transpose(data)[1::]: #from 1 till end
for elements in row:
if sum(elements) != 0:
new_data.append(elements/max(row));
else:
new_data.append(0);
new_data = np.transpose(new_data);
现在:
id col1 col2 col3 col4
A 2 1 4 0
B 3 8 2 0
C 2 3 0 0
D 5 5 3 0
E 6 3 3 0
需要:
id col1 col2 col3 col4
A 1/3 1/8 1 0
B 1/2 1 1/2 0
C 1/3 3/8 0 0
D 5/6 5/8 3/4 0
E 1 3/8 3/4 0
你如何处理0
?像上一栏一样?应该是nan
理论上的。(sum(elements) != 0
,如果它是-2 -1 0 1 2?那应该是-1 -0.5 0 0.5 1,对吗?)
In [138]:
A*1./np.max(A, axis=0)
Out[138]:
array([[ 0.33333333, 0.125 , 1. , nan],
[ 0.5 , 1. , 0.5 , nan],
[ 0.33333333, 0.375 , 0. , nan],
[ 0.83333333, 0.625 , 0.75 , nan],
[ 1. , 0.375 , 0.75 , nan]])
我们可以保留最后一列。
In [141]:
np.where(np.max(A, axis=0)==0, A, A*1./np.max(A, axis=0))
Out[141]:
array([[ 0.33333333, 0.125 , 1. , 0. ],
[ 0.5 , 1. , 0.5 , 0. ],
[ 0.33333333, 0.375 , 0. , 0. ],
[ 0.83333333, 0.625 , 0.75 , 0. ],
[ 1. , 0.375 , 0.75 , 0. ]])
循环的正确方法是:
for row in A.T:
if max(row)>0:
new_data.append([item*1./max(row) for item in row])
else:
new_data.append(row)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句