Summing up columns of arrays of different shapes in array of arrays- Python 3.x











up vote
1
down vote

favorite
1












I have an array that contains 2D arrays.
For each 2D array i want to sum up the columns and the result must be in column form.

I have a piece of code to do this, but I feel like I am not utilising numpy optimally. What is the fastest to do this?

My current code:



temp = [np.sum(l_i,axis=1).reshape(-1,1) for l_i in self.layer_inputs]


Sample Array:



array([
array([[ 0.48517904, -11.10809746],
[ 13.64104864, 5.77576326]]),
array([[16.74109924, -3.28535518],
[-4.00977275, -3.39593759],
[ 5.9048581 , -1.65258805],
[13.40762143, -1.61158724],
[ 9.8634849 , 8.02993728]]),
array([[-7.61920427, -3.2314264 ],
[-3.79142779, -2.44719713],
[32.42085005, 4.79376209],
[13.97676962, -1.19746096],
[45.60100807, -3.01680368]])
], dtype=object)


Sample Expected Result:



[array([[-10.62291842],
[ 19.41681191]]),
array([[13.45574406],
[-7.40571034],
[ 4.25227005],
[11.7960342 ],
[17.89342218]]),
array([[-10.85063067],
[ -6.23862492],
[ 37.21461214],
[ 12.77930867],
[ 42.58420439]]) ]









share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I have an array that contains 2D arrays.
    For each 2D array i want to sum up the columns and the result must be in column form.

    I have a piece of code to do this, but I feel like I am not utilising numpy optimally. What is the fastest to do this?

    My current code:



    temp = [np.sum(l_i,axis=1).reshape(-1,1) for l_i in self.layer_inputs]


    Sample Array:



    array([
    array([[ 0.48517904, -11.10809746],
    [ 13.64104864, 5.77576326]]),
    array([[16.74109924, -3.28535518],
    [-4.00977275, -3.39593759],
    [ 5.9048581 , -1.65258805],
    [13.40762143, -1.61158724],
    [ 9.8634849 , 8.02993728]]),
    array([[-7.61920427, -3.2314264 ],
    [-3.79142779, -2.44719713],
    [32.42085005, 4.79376209],
    [13.97676962, -1.19746096],
    [45.60100807, -3.01680368]])
    ], dtype=object)


    Sample Expected Result:



    [array([[-10.62291842],
    [ 19.41681191]]),
    array([[13.45574406],
    [-7.40571034],
    [ 4.25227005],
    [11.7960342 ],
    [17.89342218]]),
    array([[-10.85063067],
    [ -6.23862492],
    [ 37.21461214],
    [ 12.77930867],
    [ 42.58420439]]) ]









    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I have an array that contains 2D arrays.
      For each 2D array i want to sum up the columns and the result must be in column form.

      I have a piece of code to do this, but I feel like I am not utilising numpy optimally. What is the fastest to do this?

      My current code:



      temp = [np.sum(l_i,axis=1).reshape(-1,1) for l_i in self.layer_inputs]


      Sample Array:



      array([
      array([[ 0.48517904, -11.10809746],
      [ 13.64104864, 5.77576326]]),
      array([[16.74109924, -3.28535518],
      [-4.00977275, -3.39593759],
      [ 5.9048581 , -1.65258805],
      [13.40762143, -1.61158724],
      [ 9.8634849 , 8.02993728]]),
      array([[-7.61920427, -3.2314264 ],
      [-3.79142779, -2.44719713],
      [32.42085005, 4.79376209],
      [13.97676962, -1.19746096],
      [45.60100807, -3.01680368]])
      ], dtype=object)


      Sample Expected Result:



      [array([[-10.62291842],
      [ 19.41681191]]),
      array([[13.45574406],
      [-7.40571034],
      [ 4.25227005],
      [11.7960342 ],
      [17.89342218]]),
      array([[-10.85063067],
      [ -6.23862492],
      [ 37.21461214],
      [ 12.77930867],
      [ 42.58420439]]) ]









      share|improve this question















      I have an array that contains 2D arrays.
      For each 2D array i want to sum up the columns and the result must be in column form.

      I have a piece of code to do this, but I feel like I am not utilising numpy optimally. What is the fastest to do this?

      My current code:



      temp = [np.sum(l_i,axis=1).reshape(-1,1) for l_i in self.layer_inputs]


      Sample Array:



      array([
      array([[ 0.48517904, -11.10809746],
      [ 13.64104864, 5.77576326]]),
      array([[16.74109924, -3.28535518],
      [-4.00977275, -3.39593759],
      [ 5.9048581 , -1.65258805],
      [13.40762143, -1.61158724],
      [ 9.8634849 , 8.02993728]]),
      array([[-7.61920427, -3.2314264 ],
      [-3.79142779, -2.44719713],
      [32.42085005, 4.79376209],
      [13.97676962, -1.19746096],
      [45.60100807, -3.01680368]])
      ], dtype=object)


      Sample Expected Result:



      [array([[-10.62291842],
      [ 19.41681191]]),
      array([[13.45574406],
      [-7.40571034],
      [ 4.25227005],
      [11.7960342 ],
      [17.89342218]]),
      array([[-10.85063067],
      [ -6.23862492],
      [ 37.21461214],
      [ 12.77930867],
      [ 42.58420439]]) ]






      python arrays list performance numpy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 12:57









      jpp

      86.7k194998




      86.7k194998










      asked Nov 11 at 16:36









      Vikhyat Agarwal

      420214




      420214
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          New answer



          Given your stringent requirement for a list of arrays, there is no more computationally efficient solution.



          Original answer



          To leverage NumPy, don't work with a list of arrays: dtype=object is the hint you won't be able to use vectorised operations.



          Instead, combine into one array, e.g. via np.vstack, and store split indices. If you need a list of arrays, use np.split as a final step. But this constant flipping between lists and a single array is expensive. Really, you should attempt to just store the splits and a single array, i.e. idx and data below.



          idx = np.array(list(map(len, A))).cumsum()[:-1]  # [2, 7]
          data = np.vstack(A).sum(1)





          share|improve this answer























          • Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
            – Vikhyat Agarwal
            Nov 11 at 16:55










          • @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
            – jpp
            Nov 11 at 17:00










          • Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
            – Vikhyat Agarwal
            Nov 12 at 12:53










          • @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
            – jpp
            Nov 12 at 12:55












          • What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
            – Vikhyat Agarwal
            Nov 12 at 12:57











          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%2f53250846%2fsumming-up-columns-of-arrays-of-different-shapes-in-array-of-arrays-python-3-x%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








          up vote
          0
          down vote













          New answer



          Given your stringent requirement for a list of arrays, there is no more computationally efficient solution.



          Original answer



          To leverage NumPy, don't work with a list of arrays: dtype=object is the hint you won't be able to use vectorised operations.



          Instead, combine into one array, e.g. via np.vstack, and store split indices. If you need a list of arrays, use np.split as a final step. But this constant flipping between lists and a single array is expensive. Really, you should attempt to just store the splits and a single array, i.e. idx and data below.



          idx = np.array(list(map(len, A))).cumsum()[:-1]  # [2, 7]
          data = np.vstack(A).sum(1)





          share|improve this answer























          • Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
            – Vikhyat Agarwal
            Nov 11 at 16:55










          • @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
            – jpp
            Nov 11 at 17:00










          • Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
            – Vikhyat Agarwal
            Nov 12 at 12:53










          • @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
            – jpp
            Nov 12 at 12:55












          • What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
            – Vikhyat Agarwal
            Nov 12 at 12:57















          up vote
          0
          down vote













          New answer



          Given your stringent requirement for a list of arrays, there is no more computationally efficient solution.



          Original answer



          To leverage NumPy, don't work with a list of arrays: dtype=object is the hint you won't be able to use vectorised operations.



          Instead, combine into one array, e.g. via np.vstack, and store split indices. If you need a list of arrays, use np.split as a final step. But this constant flipping between lists and a single array is expensive. Really, you should attempt to just store the splits and a single array, i.e. idx and data below.



          idx = np.array(list(map(len, A))).cumsum()[:-1]  # [2, 7]
          data = np.vstack(A).sum(1)





          share|improve this answer























          • Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
            – Vikhyat Agarwal
            Nov 11 at 16:55










          • @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
            – jpp
            Nov 11 at 17:00










          • Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
            – Vikhyat Agarwal
            Nov 12 at 12:53










          • @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
            – jpp
            Nov 12 at 12:55












          • What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
            – Vikhyat Agarwal
            Nov 12 at 12:57













          up vote
          0
          down vote










          up vote
          0
          down vote









          New answer



          Given your stringent requirement for a list of arrays, there is no more computationally efficient solution.



          Original answer



          To leverage NumPy, don't work with a list of arrays: dtype=object is the hint you won't be able to use vectorised operations.



          Instead, combine into one array, e.g. via np.vstack, and store split indices. If you need a list of arrays, use np.split as a final step. But this constant flipping between lists and a single array is expensive. Really, you should attempt to just store the splits and a single array, i.e. idx and data below.



          idx = np.array(list(map(len, A))).cumsum()[:-1]  # [2, 7]
          data = np.vstack(A).sum(1)





          share|improve this answer














          New answer



          Given your stringent requirement for a list of arrays, there is no more computationally efficient solution.



          Original answer



          To leverage NumPy, don't work with a list of arrays: dtype=object is the hint you won't be able to use vectorised operations.



          Instead, combine into one array, e.g. via np.vstack, and store split indices. If you need a list of arrays, use np.split as a final step. But this constant flipping between lists and a single array is expensive. Really, you should attempt to just store the splits and a single array, i.e. idx and data below.



          idx = np.array(list(map(len, A))).cumsum()[:-1]  # [2, 7]
          data = np.vstack(A).sum(1)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 12:59

























          answered Nov 11 at 16:49









          jpp

          86.7k194998




          86.7k194998












          • Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
            – Vikhyat Agarwal
            Nov 11 at 16:55










          • @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
            – jpp
            Nov 11 at 17:00










          • Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
            – Vikhyat Agarwal
            Nov 12 at 12:53










          • @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
            – jpp
            Nov 12 at 12:55












          • What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
            – Vikhyat Agarwal
            Nov 12 at 12:57


















          • Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
            – Vikhyat Agarwal
            Nov 11 at 16:55










          • @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
            – jpp
            Nov 11 at 17:00










          • Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
            – Vikhyat Agarwal
            Nov 12 at 12:53










          • @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
            – jpp
            Nov 12 at 12:55












          • What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
            – Vikhyat Agarwal
            Nov 12 at 12:57
















          Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
          – Vikhyat Agarwal
          Nov 11 at 16:55




          Oh actually i need the shape of (n,1) for some operations ill be performing on the array. About the code, it uses a lot of numpy functions and looks pretty time consuming given expescially since you;re converting the map object to list and then to a numpy array. Could you run a time test of my loop based code vs your current code? (Also it would be better if you could also arrange the result into shape (n,1) arrays)
          – Vikhyat Agarwal
          Nov 11 at 16:55












          @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
          – jpp
          Nov 11 at 17:00




          @VikhyatAgarwal, Nope, you can time it yourself :). The map bit is as efficient as you can get as your input is a list. I've updated my answer for your unusual shape.
          – jpp
          Nov 11 at 17:00












          Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
          – Vikhyat Agarwal
          Nov 12 at 12:53




          Actually I tried the time test, but it takes -on an average- 3 to 4 times more time than my solution (even without the last line in your code which reshapes the array, it takes twice as much). I am looking for a solution which is be computationally efficient.
          – Vikhyat Agarwal
          Nov 12 at 12:53












          @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
          – jpp
          Nov 12 at 12:55






          @VikhyatAgarwal, Sure, see my update. If you choose to ignore the advice to avoid a list of arrays to begin with, there's little more I (or anyone else) can help.
          – jpp
          Nov 12 at 12:55














          What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
          – Vikhyat Agarwal
          Nov 12 at 12:57




          What's the problem with list of arrays? Do you prefer an array of arrays instead? That's fine by me.
          – Vikhyat Agarwal
          Nov 12 at 12:57


















          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%2f53250846%2fsumming-up-columns-of-arrays-of-different-shapes-in-array-of-arrays-python-3-x%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python