Matrix multiplication with multiple numpy arrays
What is the quickest way to multiply a matrix against a numpy array of vectors? I need to multiply a matrix A by every single vector in a list of 1000 vectors. Using a for loop is taking too long, so I was wondering if there's a way to multiply them all at once?
Example:
arr = [[1,1,1], [1,1,1],[1,1,1]]
A=
[2 2 2]
[2 2 2]
So I need to multiply Av for each v in arr. The result:
arr = [[6,6], [6,6], [6,6]]
Is there a faster way than:
new_arr =
for v in arr:
sol = np.matmul(A, v)
new_arr.append(sol)
python python-3.x numpy scipy
add a comment |
What is the quickest way to multiply a matrix against a numpy array of vectors? I need to multiply a matrix A by every single vector in a list of 1000 vectors. Using a for loop is taking too long, so I was wondering if there's a way to multiply them all at once?
Example:
arr = [[1,1,1], [1,1,1],[1,1,1]]
A=
[2 2 2]
[2 2 2]
So I need to multiply Av for each v in arr. The result:
arr = [[6,6], [6,6], [6,6]]
Is there a faster way than:
new_arr =
for v in arr:
sol = np.matmul(A, v)
new_arr.append(sol)
python python-3.x numpy scipy
1
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
1
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Your terminology is a little vague. What's the shape ofA. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?
– hpaulj
Nov 15 '18 at 22:54
The basic rule formatmul(anddot) is last dimension ofApairs with the 2nd to the last ofB(or the only one ofv). Have you triedmatmul(A, arr.T)?
– hpaulj
Nov 15 '18 at 23:19
IfAis (2,3) andarris (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of eitherA` orarris required.
– hpaulj
Nov 15 '18 at 23:38
add a comment |
What is the quickest way to multiply a matrix against a numpy array of vectors? I need to multiply a matrix A by every single vector in a list of 1000 vectors. Using a for loop is taking too long, so I was wondering if there's a way to multiply them all at once?
Example:
arr = [[1,1,1], [1,1,1],[1,1,1]]
A=
[2 2 2]
[2 2 2]
So I need to multiply Av for each v in arr. The result:
arr = [[6,6], [6,6], [6,6]]
Is there a faster way than:
new_arr =
for v in arr:
sol = np.matmul(A, v)
new_arr.append(sol)
python python-3.x numpy scipy
What is the quickest way to multiply a matrix against a numpy array of vectors? I need to multiply a matrix A by every single vector in a list of 1000 vectors. Using a for loop is taking too long, so I was wondering if there's a way to multiply them all at once?
Example:
arr = [[1,1,1], [1,1,1],[1,1,1]]
A=
[2 2 2]
[2 2 2]
So I need to multiply Av for each v in arr. The result:
arr = [[6,6], [6,6], [6,6]]
Is there a faster way than:
new_arr =
for v in arr:
sol = np.matmul(A, v)
new_arr.append(sol)
python python-3.x numpy scipy
python python-3.x numpy scipy
edited Nov 15 '18 at 23:03
Matthieu Brucher
16.8k32244
16.8k32244
asked Nov 15 '18 at 22:28
AP730AP730
183
183
1
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
1
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Your terminology is a little vague. What's the shape ofA. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?
– hpaulj
Nov 15 '18 at 22:54
The basic rule formatmul(anddot) is last dimension ofApairs with the 2nd to the last ofB(or the only one ofv). Have you triedmatmul(A, arr.T)?
– hpaulj
Nov 15 '18 at 23:19
IfAis (2,3) andarris (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of eitherA` orarris required.
– hpaulj
Nov 15 '18 at 23:38
add a comment |
1
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
1
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Your terminology is a little vague. What's the shape ofA. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?
– hpaulj
Nov 15 '18 at 22:54
The basic rule formatmul(anddot) is last dimension ofApairs with the 2nd to the last ofB(or the only one ofv). Have you triedmatmul(A, arr.T)?
– hpaulj
Nov 15 '18 at 23:19
IfAis (2,3) andarris (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of eitherA` orarris required.
– hpaulj
Nov 15 '18 at 23:38
1
1
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
1
1
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Your terminology is a little vague. What's the shape of
A. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?– hpaulj
Nov 15 '18 at 22:54
Your terminology is a little vague. What's the shape of
A. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?– hpaulj
Nov 15 '18 at 22:54
The basic rule for
matmul (and dot) is last dimension of A pairs with the 2nd to the last of B (or the only one of v). Have you tried matmul(A, arr.T)?– hpaulj
Nov 15 '18 at 23:19
The basic rule for
matmul (and dot) is last dimension of A pairs with the 2nd to the last of B (or the only one of v). Have you tried matmul(A, arr.T)?– hpaulj
Nov 15 '18 at 23:19
If
A is (2,3) and arr is (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of either A` or arr is required.– hpaulj
Nov 15 '18 at 23:38
If
A is (2,3) and arr is (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of either A` or arr is required.– hpaulj
Nov 15 '18 at 23:38
add a comment |
1 Answer
1
active
oldest
votes
Seems like you want a dot product:
new_arr = np.dot(arr, A.T)
where arr and A are numpy arrays:
arr = np.array([[1,1,1], [1,1,1],[1,1,1]])
A = np.array([[2,2, 2],[2,2,2]])
Result:
array([[6, 6],
[6, 6],
[6, 6]])
According to your edit, the dot product you want may be:
new_arr = np.dot(A, arr).T
Both return the same, but it's not the same computation.
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose ofAusing numpy.
– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also benp.dot(A, arr).T. Might be this you want according to your edit.
– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53328768%2fmatrix-multiplication-with-multiple-numpy-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Seems like you want a dot product:
new_arr = np.dot(arr, A.T)
where arr and A are numpy arrays:
arr = np.array([[1,1,1], [1,1,1],[1,1,1]])
A = np.array([[2,2, 2],[2,2,2]])
Result:
array([[6, 6],
[6, 6],
[6, 6]])
According to your edit, the dot product you want may be:
new_arr = np.dot(A, arr).T
Both return the same, but it's not the same computation.
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose ofAusing numpy.
– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also benp.dot(A, arr).T. Might be this you want according to your edit.
– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
|
show 7 more comments
Seems like you want a dot product:
new_arr = np.dot(arr, A.T)
where arr and A are numpy arrays:
arr = np.array([[1,1,1], [1,1,1],[1,1,1]])
A = np.array([[2,2, 2],[2,2,2]])
Result:
array([[6, 6],
[6, 6],
[6, 6]])
According to your edit, the dot product you want may be:
new_arr = np.dot(A, arr).T
Both return the same, but it's not the same computation.
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose ofAusing numpy.
– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also benp.dot(A, arr).T. Might be this you want according to your edit.
– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
|
show 7 more comments
Seems like you want a dot product:
new_arr = np.dot(arr, A.T)
where arr and A are numpy arrays:
arr = np.array([[1,1,1], [1,1,1],[1,1,1]])
A = np.array([[2,2, 2],[2,2,2]])
Result:
array([[6, 6],
[6, 6],
[6, 6]])
According to your edit, the dot product you want may be:
new_arr = np.dot(A, arr).T
Both return the same, but it's not the same computation.
Seems like you want a dot product:
new_arr = np.dot(arr, A.T)
where arr and A are numpy arrays:
arr = np.array([[1,1,1], [1,1,1],[1,1,1]])
A = np.array([[2,2, 2],[2,2,2]])
Result:
array([[6, 6],
[6, 6],
[6, 6]])
According to your edit, the dot product you want may be:
new_arr = np.dot(A, arr).T
Both return the same, but it's not the same computation.
edited Nov 15 '18 at 23:11
answered Nov 15 '18 at 23:02
Matthieu BrucherMatthieu Brucher
16.8k32244
16.8k32244
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose ofAusing numpy.
– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also benp.dot(A, arr).T. Might be this you want according to your edit.
– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
|
show 7 more comments
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose ofAusing numpy.
– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also benp.dot(A, arr).T. Might be this you want according to your edit.
– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
What is A.T in this case?
– AP730
Nov 15 '18 at 23:06
The transpose of
A using numpy.– Matthieu Brucher
Nov 15 '18 at 23:07
The transpose of
A using numpy.– Matthieu Brucher
Nov 15 '18 at 23:07
Depending on the content of the two matrices, what you want may also be
np.dot(A, arr).T. Might be this you want according to your edit.– Matthieu Brucher
Nov 15 '18 at 23:10
Depending on the content of the two matrices, what you want may also be
np.dot(A, arr).T. Might be this you want according to your edit.– Matthieu Brucher
Nov 15 '18 at 23:10
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
Oh okay. That makes sense. Basically I have a matrix of dimension 50 x 30000. And I need to multiply this matrix by every vector in a numpy array. There are 500 vectors of dim=30000. Would this still be correct?
– AP730
Nov 15 '18 at 23:13
1
1
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
Oh shoot. Thank you so much! I completely got my two matrices backwards. It works now :)
– AP730
Nov 15 '18 at 23:29
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53328768%2fmatrix-multiplication-with-multiple-numpy-arrays%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
1
What kind of multiplication? Elementwise across rows?
– CJR
Nov 15 '18 at 22:30
1
Please provide sample data with expected output.
– Alexander
Nov 15 '18 at 22:32
Your terminology is a little vague. What's the shape of
A. Is the other thing a list or array? If array what's the dtype? What's the shape of the 'vectors'?– hpaulj
Nov 15 '18 at 22:54
The basic rule for
matmul(anddot) is last dimension ofApairs with the 2nd to the last ofB(or the only one ofv). Have you triedmatmul(A, arr.T)?– hpaulj
Nov 15 '18 at 23:19
If
Ais (2,3) andarris (4,3), it should be clearer that you want to pair the 3's, and get a (2,4) or (4,2) result. To get that a transpose of eitherA` orarris required.– hpaulj
Nov 15 '18 at 23:38