numpy matrix multiplication each col and sum
up vote
1
down vote
favorite
I have some matrices:
A = np.array([[0.8, 0.2],
[0.4, 0.6]])
B = np.array([0.4, 0.2])
C = np.array([0.28, 0.06])
And what I want to do is:
S = A.shape[0]
what_I_need = np.zeros([S])
for s in range(S):
what_I_need[s] = np.sum(A[:, s] * C) * B[s]
print(what_I_need)
The result is
[0.0992 0.0184]
Is there an elegant or pythonic way to do this without for loop? Thanks
python numpy
add a comment |
up vote
1
down vote
favorite
I have some matrices:
A = np.array([[0.8, 0.2],
[0.4, 0.6]])
B = np.array([0.4, 0.2])
C = np.array([0.28, 0.06])
And what I want to do is:
S = A.shape[0]
what_I_need = np.zeros([S])
for s in range(S):
what_I_need[s] = np.sum(A[:, s] * C) * B[s]
print(what_I_need)
The result is
[0.0992 0.0184]
Is there an elegant or pythonic way to do this without for loop? Thanks
python numpy
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have some matrices:
A = np.array([[0.8, 0.2],
[0.4, 0.6]])
B = np.array([0.4, 0.2])
C = np.array([0.28, 0.06])
And what I want to do is:
S = A.shape[0]
what_I_need = np.zeros([S])
for s in range(S):
what_I_need[s] = np.sum(A[:, s] * C) * B[s]
print(what_I_need)
The result is
[0.0992 0.0184]
Is there an elegant or pythonic way to do this without for loop? Thanks
python numpy
I have some matrices:
A = np.array([[0.8, 0.2],
[0.4, 0.6]])
B = np.array([0.4, 0.2])
C = np.array([0.28, 0.06])
And what I want to do is:
S = A.shape[0]
what_I_need = np.zeros([S])
for s in range(S):
what_I_need[s] = np.sum(A[:, s] * C) * B[s]
print(what_I_need)
The result is
[0.0992 0.0184]
Is there an elegant or pythonic way to do this without for loop? Thanks
python numpy
python numpy
asked Nov 11 at 7:26
Sheng
295
295
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
With matrix-multiplication -
A.T.dot(C)*B # or C.dot(A)*B
With a single call to np.einsum -
np.einsum('ij,i,j->j',A,C,B) #use `optimize` flag for better perf.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
With matrix-multiplication -
A.T.dot(C)*B # or C.dot(A)*B
With a single call to np.einsum -
np.einsum('ij,i,j->j',A,C,B) #use `optimize` flag for better perf.
add a comment |
up vote
4
down vote
accepted
With matrix-multiplication -
A.T.dot(C)*B # or C.dot(A)*B
With a single call to np.einsum -
np.einsum('ij,i,j->j',A,C,B) #use `optimize` flag for better perf.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
With matrix-multiplication -
A.T.dot(C)*B # or C.dot(A)*B
With a single call to np.einsum -
np.einsum('ij,i,j->j',A,C,B) #use `optimize` flag for better perf.
With matrix-multiplication -
A.T.dot(C)*B # or C.dot(A)*B
With a single call to np.einsum -
np.einsum('ij,i,j->j',A,C,B) #use `optimize` flag for better perf.
answered Nov 11 at 7:29
Divakar
152k1478167
152k1478167
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246688%2fnumpy-matrix-multiplication-each-col-and-sum%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown