python - 即使在重塑后,sklearn 也无法将我的数组识别为 2d?
问题描述
我正在尝试运行以下代码。我知道我需要重塑我的数组以使其适合线性回归模型。但是,在我重塑它们之后,它仍然给出错误说我的数组是标量的。我也试过 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.
解决方案
使用挤压找到了这个解决方案
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)
推荐阅读
- java - 如何在 Intellij 中“计时”Java 程序
- javascript - 在 JS 循环中使用 setTimeout
- xmpp - 指定 id 后无法通过 MAM 检索 XMPP 消息
- java - JPA 在任何查询之前自动刷新
- flutter - 无法在颤动中向 TextFormField 添加渐变
- ruby-on-rails - 我是否总是需要使用 Devise on Rails 进行确认?
- scala - 如何在数据框中添加新列并填充该列?
- c# - 编译器错误消息:找不到 CS0234 命名空间
- reactjs - 在函数内部调用动作创建者。错误:操作可能没有未定义的“类型”属性?
- spring-boot - 如何在 @PreAuthorize 错误时重定向到自定义页面?