【已解決】報錯:ValueError: Expected 2D array, got scalar array instead


報錯代碼:

model = LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
model.fit(sInfonew['Weight'].values.reshape(-1,1),sInfonew['Height'].values.reshape(-1,1))
y1 = model.predict(55)
y1

報錯結果:

ValueError: Expected 2D array, got scalar array instead:
array=55.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

解決辦法:

代碼中加上兩個 [] 即可

y1 = model.predict([[55]])

修改后的代碼為;

model = LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
model.fit(sInfonew['Weight'].values.reshape(-1,1),sInfonew['Height'].values.reshape(-1,1))
y1 = model.predict([[55]])
y1

修改之后問題解決!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM