簡體   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