简体   繁体   English

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

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

I have an array of numbers: 我有一个数字数组:

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

I want to save these in an array where it will be stored in as (number, proportion of the number) using PYTHON. 我想将它们保存在一个数组中,使用PYTHON将其存储为(数字,数字的比例)。

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

This is essential to calculate the distribution of the numbers. 这对于计算数字分布至关重要。 How can I do this? 我怎样才能做到这一点?

Although I wrote the code to save the numbers as : (number, number of times it occurred in the list) but I cant figure it out how I can find the proportion of each number. 尽管我编写了将数字保存为:(数字,它在列表中出现的次数)的代码,但是我无法弄清楚如何找到每个数字的比例。 Thanks. 谢谢。

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]]

You will need to do two things. 您将需要做两件事。

  1. Convert sorted_x array as a float array. sorted_x数组转换为float数组。

  2. And then divide it by sum of counts array. 然后将其除以counts总和数组。

Example - 范例-

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]])

To store the numbers with the propertions in a new array, do - 要将具有规定的数字存储在新数组中,请执行-

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])

Output 输出量

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

As an alternative to collections.Counter , try collections.defaultdict . 作为collections.Counter的替代方法,请尝试collections.defaultdict This allows you to accumulate the total frequency as you proceed through the input (ie should be more efficient) and it's more readable (IMO). 这样,您就可以在输入过程中累计总频率(即应该更有效),并且更具可读性(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)]

An fun alternative using numpy 使用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)]

The 1. is to force float division 1.是强制浮法分割

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

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