繁体   English   中英

即使重塑后,sklearn也无法将我的数组识别为2d?

[英]sklearn doesn't recognize my array as 2d even after reshape?

我正在尝试运行以下代码。 我知道我需要重塑数组以使其适合线性回归模型。 但是,在我对它们进行整形之后,仍然会出现错误,表明我的数组是标量的。 我也尝试过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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM