which numpy command could I use to subtract vectors with different dimensions many times?
up vote
-1
down vote
favorite
i have to write this function:
in which x is a vector with dimensions [150,2] and c is [N,2] (lets suppose N=20). From each component xi (i=1,2) I have to subtract the components of c in this way ([x11-c11,x12-c12])...([x11-cN1, x12-cN2])for all the 150 sample.
I've trasformed them in a way I have the same dimensions and I can subtract them, but the result of the function should be a vector. Maybe How can I write this in numpy?
Thank you
Ok, lets suppose x=(5,2) and c=(3,2)
this is what I have obtained transforming dimensions of the two arrays. the problem is that, I have to do this but with a iteration "for loop" because the exp function should give me as a result a vector. so I have to obtain a sort of matrix divided in N blocks.
python arrays numpy for-loop
|
show 5 more comments
up vote
-1
down vote
favorite
i have to write this function:
in which x is a vector with dimensions [150,2] and c is [N,2] (lets suppose N=20). From each component xi (i=1,2) I have to subtract the components of c in this way ([x11-c11,x12-c12])...([x11-cN1, x12-cN2])for all the 150 sample.
I've trasformed them in a way I have the same dimensions and I can subtract them, but the result of the function should be a vector. Maybe How can I write this in numpy?
Thank you
Ok, lets suppose x=(5,2) and c=(3,2)
this is what I have obtained transforming dimensions of the two arrays. the problem is that, I have to do this but with a iteration "for loop" because the exp function should give me as a result a vector. so I have to obtain a sort of matrix divided in N blocks.
python arrays numpy for-loop
1
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
I think you need another axis in x to account for the 20 values of the vectorc
: i.e.x[:, None] - c
to give an array of shape(150,20,2)
– xnx
Nov 11 at 14:45
iGian i have added an example :)
– ggg
Nov 11 at 14:51
For the element 1,1 of the matrix you mean(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...
– iGian
Nov 11 at 14:55
You probably need something like(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of(N,2)
arrays, likeres[0,...]
.
– Andras Deak
Nov 11 at 15:00
|
show 5 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
i have to write this function:
in which x is a vector with dimensions [150,2] and c is [N,2] (lets suppose N=20). From each component xi (i=1,2) I have to subtract the components of c in this way ([x11-c11,x12-c12])...([x11-cN1, x12-cN2])for all the 150 sample.
I've trasformed them in a way I have the same dimensions and I can subtract them, but the result of the function should be a vector. Maybe How can I write this in numpy?
Thank you
Ok, lets suppose x=(5,2) and c=(3,2)
this is what I have obtained transforming dimensions of the two arrays. the problem is that, I have to do this but with a iteration "for loop" because the exp function should give me as a result a vector. so I have to obtain a sort of matrix divided in N blocks.
python arrays numpy for-loop
i have to write this function:
in which x is a vector with dimensions [150,2] and c is [N,2] (lets suppose N=20). From each component xi (i=1,2) I have to subtract the components of c in this way ([x11-c11,x12-c12])...([x11-cN1, x12-cN2])for all the 150 sample.
I've trasformed them in a way I have the same dimensions and I can subtract them, but the result of the function should be a vector. Maybe How can I write this in numpy?
Thank you
Ok, lets suppose x=(5,2) and c=(3,2)
this is what I have obtained transforming dimensions of the two arrays. the problem is that, I have to do this but with a iteration "for loop" because the exp function should give me as a result a vector. so I have to obtain a sort of matrix divided in N blocks.
python arrays numpy for-loop
python arrays numpy for-loop
edited Nov 11 at 15:16
asked Nov 11 at 14:13
ggg
134
134
1
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
I think you need another axis in x to account for the 20 values of the vectorc
: i.e.x[:, None] - c
to give an array of shape(150,20,2)
– xnx
Nov 11 at 14:45
iGian i have added an example :)
– ggg
Nov 11 at 14:51
For the element 1,1 of the matrix you mean(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...
– iGian
Nov 11 at 14:55
You probably need something like(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of(N,2)
arrays, likeres[0,...]
.
– Andras Deak
Nov 11 at 15:00
|
show 5 more comments
1
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
I think you need another axis in x to account for the 20 values of the vectorc
: i.e.x[:, None] - c
to give an array of shape(150,20,2)
– xnx
Nov 11 at 14:45
iGian i have added an example :)
– ggg
Nov 11 at 14:51
For the element 1,1 of the matrix you mean(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...
– iGian
Nov 11 at 14:55
You probably need something like(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of(N,2)
arrays, likeres[0,...]
.
– Andras Deak
Nov 11 at 15:00
1
1
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
I think you need another axis in x to account for the 20 values of the vector
c
: i.e. x[:, None] - c
to give an array of shape (150,20,2)
– xnx
Nov 11 at 14:45
I think you need another axis in x to account for the 20 values of the vector
c
: i.e. x[:, None] - c
to give an array of shape (150,20,2)
– xnx
Nov 11 at 14:45
iGian i have added an example :)
– ggg
Nov 11 at 14:51
iGian i have added an example :)
– ggg
Nov 11 at 14:51
For the element 1,1 of the matrix you mean
(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...– iGian
Nov 11 at 14:55
For the element 1,1 of the matrix you mean
(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...– iGian
Nov 11 at 14:55
You probably need something like
(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of (N,2)
arrays, like res[0,...]
.– Andras Deak
Nov 11 at 15:00
You probably need something like
(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of (N,2)
arrays, like res[0,...]
.– Andras Deak
Nov 11 at 15:00
|
show 5 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||)
, try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm
will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
add a comment |
up vote
0
down vote
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x
:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x
is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c
is:
[[0 1]
[2 3]
[4 5]]
After the function x
becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||)
, try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm
will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
add a comment |
up vote
0
down vote
accepted
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||)
, try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm
will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||)
, try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm
will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
From what I understand of the issue, the problem seems to be in the way you are calculating the vector norm, not in the subtraction. Using your example, but calculating exp(-||x-c||)
, try:
x = np.linspace(8,17,10).reshape((5,2))
c = np.linspace(1,6,6).reshape((3,2))
sub = np.linalg.norm(x[:,None] - c, axis=-1)
np.exp(-sub)
array([[ 5.02000299e-05, 8.49325705e-04, 1.43695961e-02],
[ 2.96711024e-06, 5.02000299e-05, 8.49325705e-04],
[ 1.75373266e-07, 2.96711024e-06, 5.02000299e-05],
[ 1.03655678e-08, 1.75373266e-07, 2.96711024e-06],
[ 6.12664624e-10, 1.03655678e-08, 1.75373266e-07]])
np.exp(-sub).shape
(5, 3)
numpy.linalg.norm
will try to return some kind of matrix norm across all the dimensions of its input unless you tell it explicitly which axis represents the vector components.
edited Nov 11 at 15:53
answered Nov 11 at 15:33
xnx
15.1k43671
15.1k43671
add a comment |
add a comment |
up vote
0
down vote
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x
:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x
is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c
is:
[[0 1]
[2 3]
[4 5]]
After the function x
becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]
add a comment |
up vote
0
down vote
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x
:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x
is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c
is:
[[0 1]
[2 3]
[4 5]]
After the function x
becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]
add a comment |
up vote
0
down vote
up vote
0
down vote
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x
:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x
is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c
is:
[[0 1]
[2 3]
[4 5]]
After the function x
becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]
I I understand, try if this give the expected result, but there is still the problem that the result has the same shape of x
:
import numpy as np
x = np.arange(10).reshape(5,2)
c = np.arange(6).reshape(3,2)
c_col_sum = np.sum(c, axis=0)
for (h,k), value in np.ndenumerate(x):
x[h,k] = c.shape[0] * x[h,k] - c_col_sum[k]
Initially x
is:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
And c
is:
[[0 1]
[2 3]
[4 5]]
After the function x
becomes:
[[-6 -6]
[ 0 0]
[ 6 6]
[12 12]
[18 18]]
answered Nov 11 at 15:10
iGian
2,6642622
2,6642622
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53249580%2fwhich-numpy-command-could-i-use-to-subtract-vectors-with-different-dimensions-ma%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
So you are subtracting just from the first row of x? Can you add sample data for example for x shape (5,2) and c shape (3,2)?
– iGian
Nov 11 at 14:31
I think you need another axis in x to account for the 20 values of the vector
c
: i.e.x[:, None] - c
to give an array of shape(150,20,2)
– xnx
Nov 11 at 14:45
iGian i have added an example :)
– ggg
Nov 11 at 14:51
For the element 1,1 of the matrix you mean
(8-1)+(8-5)+(8-5)
? Anyway, it seems that the result is still a matrix having the same shape of x...– iGian
Nov 11 at 14:55
You probably need something like
(x[:,None,:] - c).reshape(-1,2)
. Without the reshape you have 150 blocks of(N,2)
arrays, likeres[0,...]
.– Andras Deak
Nov 11 at 15:00