Я пытаюсь запустить следующий код. Я знаю, что мне нужно изменить свои массивы, чтобы они соответствовали модели линейной регрессии. Тем не менее, после того, как я изменил их форму, все равно выдается сообщение о том, что мои массивы являются скалярными. Я также попробовал 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")
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\base.py in predict(self, X)
211 Returns predicted values.
212 """
--> 213 return self._decision_function(X)
214
215 _preprocess_data = staticmethod(_preprocess_data)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\base.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_
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.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.