Can't do linear regression in scikit-Learn due to “reshaping” issue

后端 未结 4 760
故里飘歌
故里飘歌 2021-01-06 15:36

I have a simple CSV with two columns:

  1. ErrorWeek (a number for the week number in the year)
  2. ErrorCount (for the number of errors in a given week)
4条回答
  •  日久生厌
    2021-01-06 16:03

    Doing:

    X_train, X_test, y_train, y_test = train_test_split(
             df['ErrorWeek'], df['ErrorCount'], random_state=0)
    

    will make all output arrays of one dimension because you are choosing a single column value for X and y.

    Now, when you pass a one dimensional array of [n,], Scikit-learn is not able to decide that what you have passed is one row of data with multiple columns, or multiple samples of data with single column. i.e. sklearn may not infer whether its n_samples=n and n_features=1 or other way around (n_samples=1 and n_features=n) based on X data alone.

    Hence it asks you reshape the 1-D data you provided to a 2-d data of shape [n_samples, n_features]

    Now there are multiple ways of doing this.

    • You can do what the scikit-learn says:

      X_train = X_train.reshape(-1,1) X_test = X_test.reshape(-1,1)

    The 1 in the second place of reshape tells that there is a single column only and -1 is to detect the number of rows automatically for this single column.

    • Do as suggested in other answers by @MaxU and @Wen

提交回复
热议问题