简体   繁体   English

将 numpy 行与列表中的所有元素相乘

[英]multiply numpy row with all elements in list

how to multiply all rows in numpy array with list elements one by one like first row in array with first tuple in list, second with second and so on.如何将 numpy 数组中的所有行与列表元素一个接一个地相乘,例如数组中的第一行与列表中的第一个元组,第二个与第二个等等。

i am doing this我在做这个

utl  = np.array([[  3,         12.      ],
                 [  3.    ,  17.        ]])

all_ltp = ([(0, 134.30000305175778), (1, 133.80000305175778)])

a=np.array(list(itertools.product(utl, all_ltp)))
a = np.reshape(a, (-1,4))
print(a)

output is  - 
[[  3.          12.           0.         134.30000305]
 [  3.          12.           1.         133.80000305]
 [  3.          17.           0.         134.30000305]
 [  3.          17.           1.         133.80000305]]



it only works but if i increase the values of array then它只有效,但如果我增加数组的值,那么

utl  = np.array([[  3,         12.  , 99   ],
                 [  3.    ,  17.   , 99    ]])

all_ltp = ([(0, 134.30000305175778), (1, 133.80000305175778)])

a=np.array(list(itertools.product(utl, all_ltp)))
a = np.reshape(a, (-1,2))
print(a)

output is - 

[[array([ 3., 12., 99.]) (0, 134.30000305175778)]
 [array([ 3., 12., 99.]) (1, 133.80000305175778)]
 [array([ 3., 17., 99.]) (0, 134.30000305175778)]
 [array([ 3., 17., 99.]) (1, 133.80000305175778)]]

it is also working but not combining elements它也可以工作,但不能组合元素

output must be -

[[  3.          12.     99      0.         134.30000305]
 [  3.          12.     99      1.         133.80000305]
 [  3.          17.     99      0.         134.30000305]
 [  3.          17.     99      1.         133.80000305]]


One option is to use np.hstack inside a list comprehension to stack each pair of tuples and then cast the resulting list to np.array :一种选择是在列表推导中使用np.hstack来堆叠每对元组,然后将结果列表转换为np.array

a = np.array([np.hstack(tpl) for tpl in itertools.product(utl, all_ltp)])

Another option is to separate the np.arrays and tuples (from itertools.product) and vertically stack each to create two np.ndarray objects and then horizontally stack them.另一种选择是将 np.arrays 和元组(来自 itertools.product)分开并垂直堆叠每个以创建两个 np.ndarray 对象,然后水平堆叠它们。

a = np.array(list(itertools.product(utl, all_ltp)), dtype=object).T
a = np.hstack((np.vstack(a[0]), np.vstack(a[1])))

Output: Output:

[[  3.          12.          99.           0.         134.30000305]
 [  3.          12.          99.           1.         133.80000305]
 [  3.          17.          99.           0.         134.30000305]
 [  3.          17.          99.           1.         133.80000305]]

First convert all_ltp to a Numpy array:首先将all_ltp转换为Numpy数组:

b = np.array(all_ltp)

Then generate 2 intermediate arrays, by repeating utl and tiling b :然后通过重复utl和平铺b生成 2 个中间 arrays:

wrk1 = np.repeat(utl, repeats=b.shape[0], axis=0)
wrk2 = np.tile(b, reps=(utl.shape[0], 1))

(print both of them to see the result). (打印它们以查看结果)。

And to get the final result, horizontally stack both these tables:为了得到最终结果,水平堆叠这两个表:

result = np.hstack((wrk1, wrk2))

The result, for your source data, is:对于您的源数据,结果是:

[[  3.          12.          99.           0.         134.30000305]
 [  3.          12.          99.           1.         133.80000305]
 [  3.          17.          99.           0.         134.30000305]
 [  3.          17.          99.           1.         133.80000305]]

Or, to have more concise code, run:或者,要获得更简洁的代码,请运行:

result = np.hstack((np.repeat(utl, repeats=b.shape[0], axis=0),
    np.tile(b, reps=(utl.shape[0], 1))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM