Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

numpy array divide each row by its sum

>>> e
array([[ 0.,  1.],
       [ 2.,  4.],
       [ 1.,  5.]])
# Method #1
>>> e/e.sum(axis=1)[:,None]
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

# Method #2
>>> (e.T/e.sum(axis=1)).T
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

# Method #3:
>>> e/e.sum(axis=1, keepdims=True)
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])
Comment

PREVIOUS NEXT
Code Example
Python :: sklearn train test split 
Python :: python get number of arguments of a function 
Python :: drop 0 in np array 
Python :: pandas groupby multiple columns 
Python :: hex to string python 
Python :: sort a dict by values 
Python :: Display head of the DataFrame 
Python :: how to use drf permission class with class method actions 
Python :: python replace one character into string 
Python :: django choicefield empty label 
Python :: collections counter sort by value 
Python :: compare multiple columns in pandas 
Python :: concatenation of array in python 
Python :: python list last element 
Python :: python dictionary pop key 
Python :: how to join tables in python 
Python :: Word2Vec 4.0 Gensim model python dataframe 
Python :: django override delete 
Python :: series to dataframe 
Python :: python integer to octal 
Python :: python telegram bot login 
Python :: axios django post 
Python :: a int and float. python 
Python :: flask run 
Python :: conv2 python 
Python :: csv read python 
Python :: matrix diagonal sum leetcode 
Python :: matplotlib histogram frequency labels 
Python :: torch print full tensor 
Python :: python argparser flags 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =