繁体   English   中英

使用python从数字列表中保存有序对

[英]saving an ordered pair from a list of numbers using python

我有一个数字数组:

q1a = [1,2,2,2,4,3,1,3,3,4,0,0]

我想将它们保存在一个数组中,使用PYTHON将其存储为(数字,数字的比例)。

例如:[[0 0.1667],[1 0.1667],[2 0.25],[3 0.25],[4 0.167]。

这对于计算数字分布至关重要。 我怎样才能做到这一点?

尽管我编写了将数字保存为:(数字,它在列表中出现的次数)的代码,但是我无法弄清楚如何找到每个数字的比例。 谢谢。

sorted_sample_values_of_x = unique, counts = np.unique(q1a, return_counts=True)
np.asarray((unique, counts)).T
np.put(q1a, [0], [0])

sorted_x = np.matrix(sorted_sample_values_of_x)
sorted_x = np.transpose(sorted_x)
print('\n' 'Values of x (sorted):' '\n')
print(sorted_x)
>>> q1a = [1,2,2,2,4,3,1,3,3,4,0,0]
>>> from collections import Counter
>>> sorted([[x, float(y)/len(q1a)] for (x, y) in Counter(q1a).items()],
...        key=lambda x: x[0])
[[0, 0.16666666666666666],
 [1, 0.16666666666666666],
 [2, 0.25],
 [3, 0.25],
 [4, 0.16666666666666666]]

您将需要做两件事。

  1. sorted_x数组转换为float数组。

  2. 然后将其除以counts总和数组。

范例-

In [34]: sorted_x = np.matrix(sorted_sample_values_of_x)

In [35]: sorted_x = np.transpose(sorted_x).astype(float)

In [36]: sorted_x
Out[36]:
matrix([[ 0.,  2.],
        [ 1.,  2.],
        [ 2.,  3.],
        [ 3.,  3.],
        [ 4.,  2.]])

In [37]: sorted_x[:,1] = sorted_x[:,1]/counts.sum()

In [38]: sorted_x
Out[38]:
matrix([[ 0.        ,  0.16666667],
        [ 1.        ,  0.16666667],
        [ 2.        ,  0.25      ],
        [ 3.        ,  0.25      ],
        [ 4.        ,  0.16666667]])

要将具有规定的数字存储在新数组中,请执行-

In [41]: sorted_x = np.matrix(sorted_sample_values_of_x)

In [42]: sorted_x = np.transpose(sorted_x).astype(float)

In [43]: ns = sorted_x/np.array([1,counts.sum()])

In [44]: ns
Out[44]:
matrix([[ 0.        ,  0.16666667],
        [ 1.        ,  0.16666667],
        [ 2.        ,  0.25      ],
        [ 3.        ,  0.25      ],
        [ 4.        ,  0.16666667]])
In [12]: from collections import Counter

In [13]: a = [1,2,2,2,4,3,1,3,3,4,0,0]

In [14]: counter = Counter(a)

In [15]: sorted( [ [key, float(counter[key])/len(a)]  for key in counter ] )
Out[15]:
[[0, 0.16666666666666666],
 [1, 0.16666666666666666],
 [2, 0.25],
 [3, 0.25],
 [4, 0.16666666666666666]]
#!/usr/bin/env python
import numpy as np
q1a = [1,2,2,2,4,3,1,3,3,4,0,0]

unique, counts = np.unique(q1a, return_counts=True)
counts = counts.astype(float) # convert to float
counts /= counts.sum()        # counts -> proportion
print(np.c_[unique, counts])

输出量

[[ 0.          0.16666667]
 [ 1.          0.16666667]
 [ 2.          0.25      ]
 [ 3.          0.25      ]
 [ 4.          0.16666667]]

作为collections.Counter的替代方法,请尝试collections.defaultdict 这样,您就可以在输入过程中累计总频率(即应该更有效),并且更具可读性(IMO)。

from collections import defaultdict

q1a = [1,2,2,2,4,3,1,3,3,4,0,0]
n = float(len(q1a))
frequencies = defaultdict(int)
for i in q1a:
    frequencies[i] += 1/n

print frequencies.items()
[(0, 0.16666666666666666), (1, 0.16666666666666666), (2, 0.25), (3, 0.25), (4, 0.16666666666666666)]

使用numpy的有趣替代方法

print [(val, 1.*np.sum(q1a==val)/len(q1a) ) for val in np.unique(q1a) ]
#[(0, 0.16666666666666666),
#(1, 0.16666666666666666),
#(2, 0.25),
#(3, 0.25),
#(4, 0.16666666666666666)]

1.是强制浮法分割

暂无
暂无

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

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