報錯代碼:
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
修改之后問題解決!