which numpy command could I use to subtract vectors with different dimensions many times?











up vote
-1
down vote

favorite
1












i have to write this function:
enter image description here
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)
enter image description here
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.










share|improve this question




















  • 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, like res[0,...].
    – Andras Deak
    Nov 11 at 15:00

















up vote
-1
down vote

favorite
1












i have to write this function:
enter image description here
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)
enter image description here
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.










share|improve this question




















  • 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, like res[0,...].
    – Andras Deak
    Nov 11 at 15:00















up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





i have to write this function:
enter image description here
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)
enter image description here
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.










share|improve this question















i have to write this function:
enter image description here
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)
enter image description here
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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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, like res[0,...].
    – Andras Deak
    Nov 11 at 15:00
















  • 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, like res[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














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.






share|improve this answer






























    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]]





    share|improve this answer





















      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',
      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
      });


      }
      });














      draft saved

      draft discarded


















      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

























      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.






      share|improve this answer



























        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.






        share|improve this answer

























          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 15:53

























          answered Nov 11 at 15:33









          xnx

          15.1k43671




          15.1k43671
























              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]]





              share|improve this answer

























                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]]





                share|improve this answer























                  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]]





                  share|improve this answer












                  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]]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 15:10









                  iGian

                  2,6542621




                  2,6542621






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation