Creating an array by adding to shape of existing array












0















I have a numpy array:



X = np.array([[1,0,1],
[1,1,1],
[0,1,0],
[1,0,1]])


which has a shape of (4,3)



I would like to change this shape into (4,4) by adding 1 to the second dimension of the array, via:



X_b = np.ones((X.shape+(0,1)))


but what I get is:



ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)


What is the right way to do it?



Basically I want X_b to have a shape of (4,4) if X.shape = (4,3)










share|improve this question





























    0















    I have a numpy array:



    X = np.array([[1,0,1],
    [1,1,1],
    [0,1,0],
    [1,0,1]])


    which has a shape of (4,3)



    I would like to change this shape into (4,4) by adding 1 to the second dimension of the array, via:



    X_b = np.ones((X.shape+(0,1)))


    but what I get is:



    ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)


    What is the right way to do it?



    Basically I want X_b to have a shape of (4,4) if X.shape = (4,3)










    share|improve this question



























      0












      0








      0








      I have a numpy array:



      X = np.array([[1,0,1],
      [1,1,1],
      [0,1,0],
      [1,0,1]])


      which has a shape of (4,3)



      I would like to change this shape into (4,4) by adding 1 to the second dimension of the array, via:



      X_b = np.ones((X.shape+(0,1)))


      but what I get is:



      ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)


      What is the right way to do it?



      Basically I want X_b to have a shape of (4,4) if X.shape = (4,3)










      share|improve this question
















      I have a numpy array:



      X = np.array([[1,0,1],
      [1,1,1],
      [0,1,0],
      [1,0,1]])


      which has a shape of (4,3)



      I would like to change this shape into (4,4) by adding 1 to the second dimension of the array, via:



      X_b = np.ones((X.shape+(0,1)))


      but what I get is:



      ValueError: could not broadcast input array from shape (4,3) into shape (4,2,0,1)


      What is the right way to do it?



      Basically I want X_b to have a shape of (4,4) if X.shape = (4,3)







      python arrays numpy shapes






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 20:35









      tel

      7,34621431




      7,34621431










      asked Nov 21 '18 at 20:11









      oakcaoakca

      334112




      334112
























          1 Answer
          1






          active

          oldest

          votes


















          1














          To fix your code, do this instead:



          X_b = np.ones(X.shape + np.array((0,1)))


          The catch here is that X.shape returns a plain Python tuple. By adding (0,1) you were actually performing tuple concatenation, instead of pairwise addition like you intended.



          Of course, you could also just stick an extra column on to your existing array with append:



          X_b = np.append(X, [[1]]*X.shape[0], axis=1)





          share|improve this answer


























          • X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

            – oakca
            Nov 21 '18 at 20:17













          • @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

            – tel
            Nov 21 '18 at 20:41











          • Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

            – xnx
            Nov 21 '18 at 20:42











          • @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

            – tel
            Nov 21 '18 at 20:48













          • Here you can find more about the thema stackoverflow.com/questions/8486294/…

            – oakca
            Nov 21 '18 at 21:03











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419788%2fcreating-an-array-by-adding-to-shape-of-existing-array%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









          1














          To fix your code, do this instead:



          X_b = np.ones(X.shape + np.array((0,1)))


          The catch here is that X.shape returns a plain Python tuple. By adding (0,1) you were actually performing tuple concatenation, instead of pairwise addition like you intended.



          Of course, you could also just stick an extra column on to your existing array with append:



          X_b = np.append(X, [[1]]*X.shape[0], axis=1)





          share|improve this answer


























          • X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

            – oakca
            Nov 21 '18 at 20:17













          • @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

            – tel
            Nov 21 '18 at 20:41











          • Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

            – xnx
            Nov 21 '18 at 20:42











          • @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

            – tel
            Nov 21 '18 at 20:48













          • Here you can find more about the thema stackoverflow.com/questions/8486294/…

            – oakca
            Nov 21 '18 at 21:03
















          1














          To fix your code, do this instead:



          X_b = np.ones(X.shape + np.array((0,1)))


          The catch here is that X.shape returns a plain Python tuple. By adding (0,1) you were actually performing tuple concatenation, instead of pairwise addition like you intended.



          Of course, you could also just stick an extra column on to your existing array with append:



          X_b = np.append(X, [[1]]*X.shape[0], axis=1)





          share|improve this answer


























          • X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

            – oakca
            Nov 21 '18 at 20:17













          • @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

            – tel
            Nov 21 '18 at 20:41











          • Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

            – xnx
            Nov 21 '18 at 20:42











          • @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

            – tel
            Nov 21 '18 at 20:48













          • Here you can find more about the thema stackoverflow.com/questions/8486294/…

            – oakca
            Nov 21 '18 at 21:03














          1












          1








          1







          To fix your code, do this instead:



          X_b = np.ones(X.shape + np.array((0,1)))


          The catch here is that X.shape returns a plain Python tuple. By adding (0,1) you were actually performing tuple concatenation, instead of pairwise addition like you intended.



          Of course, you could also just stick an extra column on to your existing array with append:



          X_b = np.append(X, [[1]]*X.shape[0], axis=1)





          share|improve this answer















          To fix your code, do this instead:



          X_b = np.ones(X.shape + np.array((0,1)))


          The catch here is that X.shape returns a plain Python tuple. By adding (0,1) you were actually performing tuple concatenation, instead of pairwise addition like you intended.



          Of course, you could also just stick an extra column on to your existing array with append:



          X_b = np.append(X, [[1]]*X.shape[0], axis=1)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 21:55

























          answered Nov 21 '18 at 20:13









          teltel

          7,34621431




          7,34621431













          • X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

            – oakca
            Nov 21 '18 at 20:17













          • @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

            – tel
            Nov 21 '18 at 20:41











          • Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

            – xnx
            Nov 21 '18 at 20:42











          • @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

            – tel
            Nov 21 '18 at 20:48













          • Here you can find more about the thema stackoverflow.com/questions/8486294/…

            – oakca
            Nov 21 '18 at 21:03



















          • X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

            – oakca
            Nov 21 '18 at 20:17













          • @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

            – tel
            Nov 21 '18 at 20:41











          • Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

            – xnx
            Nov 21 '18 at 20:42











          • @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

            – tel
            Nov 21 '18 at 20:48













          • Here you can find more about the thema stackoverflow.com/questions/8486294/…

            – oakca
            Nov 21 '18 at 21:03

















          X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

          – oakca
          Nov 21 '18 at 20:17







          X_b[:,:-1] = X and then use X_b as input... probably doing the same thing. your answer looks good. Just waiting the minutes

          – oakca
          Nov 21 '18 at 20:17















          @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

          – tel
          Nov 21 '18 at 20:41





          @oakca assuming X_b was created with np.ones, then yep. The one "gotcha" is that X_b will be of dtype float64, which is the default for np.ones. Thus the dtypes of X and X_b may not match (eg if X.dtype==int)

          – tel
          Nov 21 '18 at 20:41













          Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

          – xnx
          Nov 21 '18 at 20:42





          Or np.hstack((X, np.ones((4,1), dtype=np.int))), which I find easier to visualize.

          – xnx
          Nov 21 '18 at 20:42













          @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

          – tel
          Nov 21 '18 at 20:48







          @xnx There's a whole bunch of these array-growing functions, and they're all roughly equivalent. Ideally you use none of them and instead just preallocate all your arrays (whose size you magically know in advance). Internally, both append and hstack are thin (5-10 lines) wrappers around concatenate.

          – tel
          Nov 21 '18 at 20:48















          Here you can find more about the thema stackoverflow.com/questions/8486294/…

          – oakca
          Nov 21 '18 at 21:03





          Here you can find more about the thema stackoverflow.com/questions/8486294/…

          – oakca
          Nov 21 '18 at 21:03




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419788%2fcreating-an-array-by-adding-to-shape-of-existing-array%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?