iloc method return different type of data












1















I have a dataset 30 obs and 2 columns i used code below to create independent and depend dataset for a single linear regression.



So each dataset is expected to be a 1 column array.



But the return X is a 2d arrary and returned y is a 1d array what is the reason for that?



So to put my quetion in one line:



what is the difference between



X = dataset.iloc[:, 0].values


and



X = dataset.iloc[:, :-1].values?


When I use:



X = dataset.iloc[:, 0].values
y = dataset.iloc[:, 1].values

X.shape
Out[207]: (30,)
y.shape
Out[204]: (30,)


When I use:



X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

X.shape
Out[203]: (30, 1)
y.shape
Out[204]: (30,)









share|improve this question





























    1















    I have a dataset 30 obs and 2 columns i used code below to create independent and depend dataset for a single linear regression.



    So each dataset is expected to be a 1 column array.



    But the return X is a 2d arrary and returned y is a 1d array what is the reason for that?



    So to put my quetion in one line:



    what is the difference between



    X = dataset.iloc[:, 0].values


    and



    X = dataset.iloc[:, :-1].values?


    When I use:



    X = dataset.iloc[:, 0].values
    y = dataset.iloc[:, 1].values

    X.shape
    Out[207]: (30,)
    y.shape
    Out[204]: (30,)


    When I use:



    X = dataset.iloc[:, :-1].values
    y = dataset.iloc[:, 1].values

    X.shape
    Out[203]: (30, 1)
    y.shape
    Out[204]: (30,)









    share|improve this question



























      1












      1








      1








      I have a dataset 30 obs and 2 columns i used code below to create independent and depend dataset for a single linear regression.



      So each dataset is expected to be a 1 column array.



      But the return X is a 2d arrary and returned y is a 1d array what is the reason for that?



      So to put my quetion in one line:



      what is the difference between



      X = dataset.iloc[:, 0].values


      and



      X = dataset.iloc[:, :-1].values?


      When I use:



      X = dataset.iloc[:, 0].values
      y = dataset.iloc[:, 1].values

      X.shape
      Out[207]: (30,)
      y.shape
      Out[204]: (30,)


      When I use:



      X = dataset.iloc[:, :-1].values
      y = dataset.iloc[:, 1].values

      X.shape
      Out[203]: (30, 1)
      y.shape
      Out[204]: (30,)









      share|improve this question
















      I have a dataset 30 obs and 2 columns i used code below to create independent and depend dataset for a single linear regression.



      So each dataset is expected to be a 1 column array.



      But the return X is a 2d arrary and returned y is a 1d array what is the reason for that?



      So to put my quetion in one line:



      what is the difference between



      X = dataset.iloc[:, 0].values


      and



      X = dataset.iloc[:, :-1].values?


      When I use:



      X = dataset.iloc[:, 0].values
      y = dataset.iloc[:, 1].values

      X.shape
      Out[207]: (30,)
      y.shape
      Out[204]: (30,)


      When I use:



      X = dataset.iloc[:, :-1].values
      y = dataset.iloc[:, 1].values

      X.shape
      Out[203]: (30, 1)
      y.shape
      Out[204]: (30,)






      python python-3.x pandas dataframe indexing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 14:52









      jpp

      100k2161111




      100k2161111










      asked Dec 31 '18 at 14:40









      Dongyang FuDongyang Fu

      134




      134
























          1 Answer
          1






          active

          oldest

          votes


















          0














          :-1 represents a range1, while -1 is a scalar. Ranges are 1-dimensional, while scalars are 0-dimensional. Think of a line versus a point; a range is a line while a scalar is a point. This is reflected in how Pandas translates a range vs scalar when indexing.



          Therefore, the following are equivalent for a dataframe with 2 columns:



          df = pd.DataFrame(np.random.random((5, 2)))

          df.iloc[:, :-1].shape # (5, 1)
          df.iloc[:, [0]].shape # (5, 1)


          Using a scalar will remove the extra dimension. You can do this in a couple of ways:



          df.iloc[:, 0].shape   # (5,)
          df.iloc[:, -2].shape # (5,)




          In fact, :-1 is syntactic sugar for a slice object: slice(0, -1). In practice, the simpler syntax is preferred unless you need to pass slice objects around.






          share|improve this answer


























          • thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

            – Dongyang Fu
            Dec 31 '18 at 14:54











          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%2f53988637%2filoc-method-return-different-type-of-data%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









          0














          :-1 represents a range1, while -1 is a scalar. Ranges are 1-dimensional, while scalars are 0-dimensional. Think of a line versus a point; a range is a line while a scalar is a point. This is reflected in how Pandas translates a range vs scalar when indexing.



          Therefore, the following are equivalent for a dataframe with 2 columns:



          df = pd.DataFrame(np.random.random((5, 2)))

          df.iloc[:, :-1].shape # (5, 1)
          df.iloc[:, [0]].shape # (5, 1)


          Using a scalar will remove the extra dimension. You can do this in a couple of ways:



          df.iloc[:, 0].shape   # (5,)
          df.iloc[:, -2].shape # (5,)




          In fact, :-1 is syntactic sugar for a slice object: slice(0, -1). In practice, the simpler syntax is preferred unless you need to pass slice objects around.






          share|improve this answer


























          • thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

            – Dongyang Fu
            Dec 31 '18 at 14:54
















          0














          :-1 represents a range1, while -1 is a scalar. Ranges are 1-dimensional, while scalars are 0-dimensional. Think of a line versus a point; a range is a line while a scalar is a point. This is reflected in how Pandas translates a range vs scalar when indexing.



          Therefore, the following are equivalent for a dataframe with 2 columns:



          df = pd.DataFrame(np.random.random((5, 2)))

          df.iloc[:, :-1].shape # (5, 1)
          df.iloc[:, [0]].shape # (5, 1)


          Using a scalar will remove the extra dimension. You can do this in a couple of ways:



          df.iloc[:, 0].shape   # (5,)
          df.iloc[:, -2].shape # (5,)




          In fact, :-1 is syntactic sugar for a slice object: slice(0, -1). In practice, the simpler syntax is preferred unless you need to pass slice objects around.






          share|improve this answer


























          • thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

            – Dongyang Fu
            Dec 31 '18 at 14:54














          0












          0








          0







          :-1 represents a range1, while -1 is a scalar. Ranges are 1-dimensional, while scalars are 0-dimensional. Think of a line versus a point; a range is a line while a scalar is a point. This is reflected in how Pandas translates a range vs scalar when indexing.



          Therefore, the following are equivalent for a dataframe with 2 columns:



          df = pd.DataFrame(np.random.random((5, 2)))

          df.iloc[:, :-1].shape # (5, 1)
          df.iloc[:, [0]].shape # (5, 1)


          Using a scalar will remove the extra dimension. You can do this in a couple of ways:



          df.iloc[:, 0].shape   # (5,)
          df.iloc[:, -2].shape # (5,)




          In fact, :-1 is syntactic sugar for a slice object: slice(0, -1). In practice, the simpler syntax is preferred unless you need to pass slice objects around.






          share|improve this answer















          :-1 represents a range1, while -1 is a scalar. Ranges are 1-dimensional, while scalars are 0-dimensional. Think of a line versus a point; a range is a line while a scalar is a point. This is reflected in how Pandas translates a range vs scalar when indexing.



          Therefore, the following are equivalent for a dataframe with 2 columns:



          df = pd.DataFrame(np.random.random((5, 2)))

          df.iloc[:, :-1].shape # (5, 1)
          df.iloc[:, [0]].shape # (5, 1)


          Using a scalar will remove the extra dimension. You can do this in a couple of ways:



          df.iloc[:, 0].shape   # (5,)
          df.iloc[:, -2].shape # (5,)




          In fact, :-1 is syntactic sugar for a slice object: slice(0, -1). In practice, the simpler syntax is preferred unless you need to pass slice objects around.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 31 '18 at 14:51

























          answered Dec 31 '18 at 14:44









          jppjpp

          100k2161111




          100k2161111













          • thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

            – Dongyang Fu
            Dec 31 '18 at 14:54



















          • thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

            – Dongyang Fu
            Dec 31 '18 at 14:54

















          thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

          – Dongyang Fu
          Dec 31 '18 at 14:54





          thx! I am new to python. I m a little crazy about how difficult it is to me. this helps so much!

          – Dongyang Fu
          Dec 31 '18 at 14:54




















          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%2f53988637%2filoc-method-return-different-type-of-data%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

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'