0

Is there a method in numpy that calculates the ratio of entries in an array over the sum of the numbers in the array? For example:

>>>import numpy as np
>>>datas=[1,2,3,3,100]
>>>np.frequencies(datas)
[1/109,2/109,3/109,3/109,100/109]
martineau
  • 119,623
  • 25
  • 170
  • 301
user42493
  • 813
  • 4
  • 14
  • 34
  • 1
    Are you asking given an array, find the ratio of entries which exceed the sum of the array? E.g given `[1,2,3]` find the ratio of values that exceed 6 (the sum)? – IanQ Oct 19 '19 at 18:54
  • No I mean, it should return [1/6,2/6,3/6] – user42493 Oct 19 '19 at 18:57

1 Answers1

3

Assuming I understand what you're saying you can just do the following

import numpy as np
datas=np.asarray([1,2,3,3,100])  # Convert to a numpy array 
answer_you_want = datas / sum(datas)
IanQ
  • 1,831
  • 5
  • 20
  • 29
  • it is an obvious duplicate : https://stackoverflow.com/questions/45364832/how-can-i-calculate-the-ratio-of-a-value-in-array-to-the-sum-of-array-in-python – Mohsen_Fatemi Oct 19 '19 at 19:00