i have array of numbers:
q1a = [1,2,2,2,4,3,1,3,3,4,0,0] i want save these in array stored in (number, proportion of number) using python.
such : [[0 0.1667], [1 0.1667], [2 0.25], [3 0.25], [4 0.167]].
this essential calculate distribution of numbers. how can this?
although wrote code save numbers : (number, number of times occurred in list) cant figure out how can find 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)
you need 2 things.
convert
sorted_xarray float array.and divide sum of
countsarray.
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 numbers propertions in new array, -
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]])
Comments
Post a Comment