python for datascience

Question

why do have to use reshape(-1,1) or reshape(1,-1) for a single feature before fitting it to the model in sklearn library?

in progress 0
JAI SANDESH LS 55 years 2 Answers 628 views Member 0

Answers ( 2 )

  1. the single feature array is nothing but a 1D-array. when a 1D-array is passed to sklearn library module it considers and indices of the rows so we need to make other than 1D-array so what reshape(-1,1) does data it creates an array of one column and undefined rows(-1 represent there can be any number of rows).
    example:
    a = [1,2,3]
    //after reshape(-1,1) is applied
    a = [[1],
    [2],
    [3]]

  2. the single feature array is nothing but a 1D-array. when a 1D-array is passed to the sklearn library module it considers as indices of the rows. So, we need to make this array other than a 1D-array.when reshape(-1,1) is applied it creates an array of one column and with some rows(-1 represent there can be any number of rows).
    example:
    a = [1,2,3]
    //after reshape(-1,1) is applied
    a = [[1],
    [2],
    [3]]

Leave an answer

Browse
Browse