ValueError: Expected 2D array, got scalar array instead

前端 未结 2 1117
余生分开走
余生分开走 2021-01-07 00:58

While practicing Simple Linear Regression Model I got this error:

ValueError: Expected 2D array, got scalar array instead:
array=60.
Reshape your data either         


        
2条回答
  •  余生分开走
    2021-01-07 01:37

    Short answer:

    regression.predict([[60]])
    

    Long answer: regression.predict takes a 2d array of values you want to predict on. Each item in the array is a "point" you want your model to predict on. Suppose we want to predict on the points 60, 52, and 31. Then we'd say regression.predict([[60], [52], [31]])

    The reason we need a 2d array is because we can do linear regression in a higher dimension space than just 2d. For example, we could do linear regression in a 3d space. Suppose we want to predict "z" for a given data point (x, y). Then we'd need to say regression.predict([[x, y]]).

    Taking this example further, we could predict "z" for a set of "x" and "y" points. For example, we want to predict the "z" values for each of the points: (0, 2), (3, 7), (10, 8). Then we would say regression.predict([[0, 2], [3, 7], [10, 8]]) which fully demonstrates the need for regression.predict to take a 2d array of values to predict on points.

提交回复
热议问题