Scikit learn scikit学习回归。predict()返回类型不一致
我从sklearn.linear_模型运行三种不同的回归模型 在我的具体示例中,linear\u model.LinearRegression.predict()和linear\u model.Ridge.predict()都返回相同格式的(1300,1) 线性_model.Lasso.predict()在传递完全相同的输入数据时返回a(1300,)。这会导致程序出错,打印失败 通过使用np.shape()检查所使用的每个变量,我已经尝试确保确实传递了相同格式的数据。我将其追溯到.predict()中不同的返回值Scikit learn scikit学习回归。predict()返回类型不一致,scikit-learn,python-3.6,Scikit Learn,Python 3.6,我从sklearn.linear_模型运行三种不同的回归模型 在我的具体示例中,linear\u model.LinearRegression.predict()和linear\u model.Ridge.predict()都返回相同格式的(1300,1) 线性_model.Lasso.predict()在传递完全相同的输入数据时返回a(1300,)。这会导致程序出错,打印失败 通过使用np.shape()检查所使用的每个变量,我已经尝试确保确实传递了相同格式的数据。我将其追溯到.predict
# Crashes when .Lasso is included in mdls
# If I only have the first two in the list (LinearRegression and Ridge) it run and plots fine.
mdls=[linear_model.LinearRegression, linear_model.Ridge, linear_model.Lasso]
argdic=[{'fit_intercept':True},{'fit_intercept':True,'alpha':.5},{'fit_intercept':True,'alpha':0.1}]
i=0
for m,a in zip(mdls,argdic):
## Run regression Fit
res=m(**a).fit(xsk,ysk)
predZmesh=res.predict(meshpts)
predZact=res.predict(actpts)
reZ=ysk['Eff. At Rated Lift'].values.reshape(len(ysk['Eff. At Rated Lift']),1)
zerr=np.subtract(predZact,reZ)
zerr_r=zerr.ravel()
#Normalize the errror for colormap
nrm=colors.Normalize(vmin=zerr_r.min(),vmax=zerr_r.max())
r2=res.score(xsk,ysk)
#Setup Plot
ax=fig.add_subplot(1,len(mdls),i+1,projection='3d')
#Plot scatter of actual data points
#ax.scatter(xsk['Minor Comp. At Temp'],xsk['Major Comp. At Temp'],ysk,marker='o',alpha=0.9)
print('Shape of x,y,z,err.ravel={0},{1},{2},{3}'.format(np.shape(xsk['Minor Comp. At Temp']),np.shape(xsk['Major Comp. At Temp']),np.shape(ysk['Eff. At Rated Lift']),np.shape(zerr_r)))
ax.scatter(xsk['Minor Comp. At Temp'],xsk['Major Comp. At Temp'],ysk['Eff. At Rated Lift'],c=zerr_r,norm=nrm,cmap=plt.cm.get_cmap('RdBu'),marker='o',alpha=0.9)
ax.plot_surface(xmeshpts,ymeshpts,
predZmesh.reshape(xmeshpts.shape),color='red',alpha=0.1)
i+=1
回归函数是否应该返回相同格式的数据?当我阅读文档时,它显示格式是一致的。有人能确认我对一致返回值的期望是真的吗?然后我可以继续调试。您可以添加一个条件,如果使用的模型是
linear\u model.Lasso,则可以检查并重新调整预测的输出
以下代码可用于执行重塑:x=x.restorate(-1,1)
下面是一个简单的例子:
如果您的展平数组没有这样的列:
x = np.array([1,2,3])
x.shape
的输出当然是(3,)
然后,我们可以对其进行重塑,以便将值存储为一个具有形状的列(3,1)
:
在此之后,x.shape
的输出现在是(3,1)
从视觉上看,阵列不再是一个平面列表,而是包含一列:
array([[1],
[2],
[3]])
这是2015年首次报道的。当您提供具有2维的y_数组(test_y.shape=(n,1))时,就会出现问题
最简单的解决方案是在调用.predict()方法之前展平y_数组:
(假设test_y是一个numpy数组)根据我的经验,有时预测输出为一个平面列表,有时输出为单个列——预测的形状取决于算法。如果你想用第二副眼睛检查一下你正在复习的sklearn文档中的特定页面,请随意添加一个链接。我建议只需重新调整线性模型.Lasso.predict()的预测,然后看看图表是否符合您的直觉。
array([[1],
[2],
[3]])
test_y = test_y.flatten()