我正在尝试运行以下代码。我知道我需要重塑我的数组以使它们适合线性回归模型。但是,在我重塑它们之后,它仍然给出错误,说我的数组是标量的。我也尝试过atleast_2d但没有运气。
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
boston = load_boston()
x = np.array(boston.data[:,5])
y = boston.target
x=np.array(x).reshape(-1,1)
y=np.array(y).reshape(-1,1)
lr = LinearRegression(fit_intercept=True)
lr.fit(x,y)
fig,ax= plt.subplots()
ax.set_xlabel("Average number of rooms(RM)")
ax.set_ylabel("House Price")
xmin = x.min()
xmax = x.max()
ax.plot([xmin,xmax],
[lr.predict(xmin),lr.predict(xmax)],
"-",
lw=2,color="#f9a602")
ax.scatter(x,y,s=2)
> ValueError Traceback (most recent call last)
<ipython-input-6-8c6977f43703> in <module>
7 xmax = xmax
8 ax.plot([xmin,xmax],
----> 9 [lr.predict(xmin), lr.predict(xmax)],
10 "-",
11 lw=2,color="#f9a602")
~AppDataLocalContinuumanaconda3libsite-packagessklearnlinear_modelbase.py in predict(self, X)
211 Returns predicted values.
212 """
--> 213 return self._decision_function(X)
214
215 _preprocess_data = staticmethod(_preprocess_data)
~AppDataLocalContinuumanaconda3libsite-packagessklearnlinear_modelbase.py in _decision_function(self, X)
194 check_is_fitted(self, "coef_")
195
--> 196 X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
197 return safe_sparse_dot(X, self.coef_.T,
198 dense_output=True) + self.intercept_
~AppDataLocalContinuumanaconda3libsite-packagessklearnutilsvalidation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
543 "Reshape your data either using array.reshape(-1, 1) if "
544 "your data has a single feature or array.reshape(1, -1) "
--> 545 "if it contains a single sample.".format(array))
546 # If input is 1D raise error
547 if array.ndim == 1:
ValueError: Expected 2D array, got scalar array instead:
array=<built-in method min of numpy.ndarray object at 0x0000019960BF9CB0>.
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.
使用 squeeze 找到此解决方案
from sklearn.linear_model import LinearRegression
lr = LinearRegression(fit_intercept=True)
xmin
xmax = x.max()
xmax = xmax.reshape(-1,1)
xmin = x.min()
lr.fit(x,y)
xmin =xmin.reshape(-1,1)
print(xmin.shape)
print(xmin)
s = lr.predict(xmin)
s= np.squeeze(s)
r = lr.predict(xmax)
r = np.squeeze(r)
xmax = np.squeeze(xmax)
xmin = np.squeeze(xmin)
lr.fit(x,y)
fig,ax= plt.subplots()
ax.set_xlabel("Average number of rooms(RM)")
ax.set_ylabel("House Price")
xmin = xmin
xmax = xmax
ax.plot([xmin,xmax],
[s, r],
"-",
lw=2,color="#f9a602")
ax.scatter(x,y,s=2)