python - assigning different weights to every numpy column -


i have following numpy array:

from sklearn.decomposition import pca sklearn.preprocessing import normalize import numpy np  # numpy array comprising associate metrics # i.e. open ta's, open sr's, open se's associatemetrics = np.array([[11,  28,  21],    [27,  17,  20],    [19,  31,  3],    [17,  24,  17]]).astype(np.float64) print("raw metrics=", associatemetrics) 

now, want assign different weights every column in above array & later normalize this. eg. lets want assign higher weight 1st column multiplying 5, multiple column 2 3 , last column 2.

how do in python? sorry bit new python , numpy.

i have tried 1 column wont work:

# assign weights metrics weightedmetrics = associatemetrics np.multiply(2, weightedmetrics[:,0]) print("weighted metrics=", weightedmetrics) 

you should make use of numpy's array broadcasting. means lower-dimensional arrays can automatically expanded perform vectorized operation array of higher (but compatible) dimensions. in specific case, can multiply (4,3)-shaped array 1d weight array of shape (3,) , obtain want:

weightedmetrics = associatemetrics * np.array([5,3,2]) 

the trick can imagine numpy ndarrays have leading singleton dimensions, along broadcasting automatic. mean 1d numpy weight array of shape (3,) can thought have leading singleton dimension (but point of view of broadcasting!). , it's easy see how array of shape (4,3) , (1,3) should multiplied: each element of latter has used full columns of former.

in general case, can use arithmetic operations on, say, array of shape (3,1,3,1,4) , 1 of shape (2,3,4,4). what's important dimensions meet should either agree, or 1 of arrays should have singleton dimension @ place, , 1 of arrays allowed longer (in front).


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -