【已解决】报错: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