numpy的np.dot()在多维数组 [英] Numpy np.dot() on multidimensional arrays
问题描述
这是一个简单的问题,但我发现所涉及的大小相混淆。
使用numpy的,我有一个3维数组,形状=(10,100,100)。
(我认为它的方式是10矩阵的np.ndarray,每100型100,即
ARR1 = M1 M2 M3 .... M10]
其中, M1.shape =(100,100),M2.shape =(100,100),...
我也有叫ARRB数据的第二阵列,这是 arrB.shaped(100)
。我的目标是做矩阵乘法与这些numpy的阵列,即(arrB.T)* * ARR1(ARRB)
,结果是一个整数。使用numpy的数组,这个操作应该以 np.dot()完成
OP1 = np.dot(ARR1,ARRB)
OP2 = np.dot((arrB.T),OP1)
或
终产物= np.dot((arrB.T),np.dot(ARR1,ARRB))
但是,这是行不通的。我得到一个错误:
ValueError错误:形状(100)和(10,100)未对齐:100(点心0)= 10(0变暗)
如果我做一个操作黑客帝国M#的时间,我可以执行此操作,即
=中element1 ARR1 [0]
结束= np.dot((arrB.T),np.dot(中element1,ARRB))
未经剪接我原来的阵列,在做业务,再次追加,我怎么能我原来阵列上执行这些操作 ARR1
导致
的结果= [(arrB.T)* ARR1 [0] *(ARRB)(arrB.T)* ARR1 [1] *(ARRB)(arrB.T)* ARR1 [2] *(ARRB)...
....(arrB.T)* ARR1 [9] *(ARRB)]
您可以使用列表COM prehension这是 -
ARR3 = np.array([np.dot(arr2.T,np.dot(ARR1 [在范围I],ARR2))因为我(arr1.shape [0] )])
This is an easy question, but I'm getting confused by the size involved.
Using NumPy, I have a 3-dimensional array, shape = (10, 100, 100).
(The way I think of it is as an np.ndarray of 10 "matrices", each shaped 100 by 100, i.e.
arr1 = [M1 M2 M3....M10]
where M1.shape = (100,100), M2.shape = (100,100),...
I also have a second array of data called "arrB", which is arrB.shaped (100,)
. My goal is to do matrix multiplication with these numpy arrays, i.e. (arrB.T)*arr1*(arrB)
, resulting in a single integer. Using numpy arrays, this operation should be completed with np.dot()
op1 = np.dot(arr1, arrB)
op2 = np.dot((arrB.T), op1)
or
endproduct = np.dot((arrB.T), np.dot(arr1, arrB) )
However, this does not work. I get an error:
ValueError: shapes (100,) and (10,100) not aligned: 100 (dim 0) != 10 (dim 0)
If I do the operation on one "matrix" M# at a time, I can perform this operation, i.e.
element1 = arr1[0]
end = np.dot((arrB.T), np.dot(element1, arrB) )
Without splicing my original array, doing the operations, and appending again, how can I perform these operations on my original array arr1
to result in
result = [(arrB.T)*arr1[0]*(arrB) (arrB.T)*arr1[1]*(arrB) (arrB.T)*arr1[2]*(arrB) ...
....(arrB.T)*arr1[9]*(arrB) ]
You can use list comprehension for this as -
arr3 = np.array([np.dot(arr2.T , np.dot(arr1[i] , arr2)) for i in range(arr1.shape[0])])
这篇关于numpy的np.dot()在多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!