ValueError: Expected 2D array, got scalar array instead

前端 未结 2 1084
余生分开走
余生分开走 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:23

    The ValueError is fairly clear, predict expects a 2D array but you passed a scalar.

    hgt = np.random.randint(50, 70, 10).reshape(-1, 1)
    wgt = np.random.randint(90, 120, 10).reshape(-1, 1)
    from sklearn.linear_model import LinearRegression
    from sklearn.metrics import r2_score
    
    regression = LinearRegression()
    regression.fit(hgt,wgt)
    
    regression.predict([[60]])
    

    You get

    array([[105.10013717]])
    

提交回复
热议问题