当 .dropna() 使它们的大小不同时,处理两个数据集的正确方法是什么?

What is the proper way to deal with two datasets when .dropna() leaves them different sizes?

我正在测试机器学习技术,首先使用模型数据集,然后使用测试数据集(csv 文件)。但是,使用 df.dropna(inplace=True) 会使它们的大小不同(模型为 48470,测试为 48571)。这会导致我的代码出现多个问题。

因此,例如,虽然这有效:

linear = linear_model.LinearRegression()

#Model fit to data
linear.fit(xmodel,ymodel.values.ravel())

#Prediction made by model
pred_linear = linear.predict(xtest)
## Compute RMSE
linear_rmse = mean_squared_error(ytest,pred_logit)
print("Linear regression RMSE is ",linear_rmse)
#Linear regression RMSE is  0.07557238168273192 

另一方面,我无法绘制所有结果:

x1=np.linspace(0,1,48571)
fig, ax = plt.subplots()
ax.plot(x1, yt, 'o', label="Data")
#ax.plot(x1, y_true, 'b-', label="True")
ax.plot(np.hstack((x1, xmodel)), np.hstack((ym, yt)), 'r', label="OLS prediction")
ax.legend(loc="best");

returns

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-124-29b0a94ea028> in <module>
     24 ax.plot(x1, yt, 'o', label="Data")
     25 #ax.plot(x1, y_true, 'b-', label="True")
---> 26 ax.plot(np.hstack((x1, xmodel)), np.hstack((ym, yt)), 'r', label="OLS prediction")
     27 ax.legend(loc="best");

C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\shape_base.py in hstack(tup)
    284     # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
    285     if arrs and arrs[0].ndim == 1:
--> 286         return _nx.concatenate(arrs, 0)
    287     else:
    288         return _nx.concatenate(arrs, 1)

ValueError: all the input arrays must have same number of dimensions

同样(我认为这是一个相关的问题,但如果不是我的道歉)

reg_model = sm.OLS(endog=ymodel, exog=xmodel)
reg_test = sm.OLS(endog=ytest, exog=xtest)
reg_model.fit()
reg_test.fit()
pred_regmodel=reg_model.predict(xtest)
pred_regtest=reg_test.predict(xtest) #Transpose sorta solved this, but takes forever   #xtest.iloc[9,:].values) technically solved shape problem, but gave false answer
regmodel_rmse = mean_squared_error(ytest,pred_regmodel)
print("OLS RMSE is ",regmodel_rmse)

Returns 类似的不匹配暗淡错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-135-3ab9b9511c74> in <module>
      3 reg_model.fit()
      4 reg_test.fit()
----> 5 pred_regmodel=reg_model.predict(xtest)
      6 pred_regtest=reg_test.predict(xtest) #Transpose sorta solved this, but takes forever   #xtest.iloc[9,:].values) technically solved shape problem, but gave false answer
      7 regmodel_rmse = mean_squared_error(ytest,pred_regmodel)

C:\ProgramData\Anaconda3\lib\site-packages\statsmodels\regression\linear_model.py in predict(self, params, exog)
    343             exog = self.exog
    344 
--> 345         return np.dot(exog, params)
    346 
    347     def get_distribution(self, params, scale, exog=None, dist_class=None):

ValueError: shapes (48470,9) and (48571,9) not aligned: 9 (dim 1) != 48571 (dim 0)

我想形成我的图表,使其看起来像 this statsmodels example 的 [7] 中。至于第二个例子,我的目标是将我的 sm.OLS 方法与模型数据或测试数据进行训练,与 ytest df 进行比较,以便我可以找到 MSRE 范围以与我的 logit/lasso/SVC/etc 方法进行比较用过

没有必要平衡训练和测试数据集。您只需要与输入相同数量的 features/columns 。你的第一个错误是因为 x1x_model 是不同的大小,并且 np.hstack 不能堆叠不同的大小

第二个错误,感觉x_test转置了。尝试 x_test.T()?