0

Sorry if this is a duplicate- it surely is but there are so many ways this could be asked I didn't stumble across it...

I want to convert an array to weights. The following code does it:

>>> a = np.array([1,2,3])
>>> a.astype(float) / a.sum()
array([ 0.16666667,  0.33333333,  0.5       ])

This gets really hairy for complex arrays. Is there a numpy method that will do the same?

rawkintrevo
  • 659
  • 5
  • 16
  • possible duplicate of [How to normalize a 2-dimensional numpy array in python less verbose?](http://stackoverflow.com/questions/8904694/how-to-normalize-a-2-dimensional-numpy-array-in-python-less-verbose) – ali_m Apr 23 '15 at 13:23
  • wow- that answer is even uglier than my question. – rawkintrevo Apr 23 '15 at 13:30
  • 4
    Well, your question doesn't really give us much to go on. What do you mean by *"complex arrays"*? Do you mean multidimensional arrays or arrays containing complex values? If you are talking about multidimensional arrays, do you always want to normalize by the sum over all elements, or do you want rows or columns to sum to 1? What *"gets really hairy"* about your approach? – ali_m Apr 23 '15 at 13:34
  • or arrays of imaginary numbers ;) a/np.absolute(a).sum() – paddyg Apr 23 '15 at 14:18
  • You can use `np.true_divide` but other than that? Maybe some external module implements this. –  Apr 23 '15 at 17:46
  • 1
    What bothers you about your current solution? This is more of a vague question than a duplicate. – hpaulj Apr 23 '15 at 19:36
  • It seems like Normalization is such a common thing that there would be an included method for arrays. I'm pretty certain after a day of looking that no such method exists. – rawkintrevo Apr 24 '15 at 12:58

0 Answers0