【问题标题】:10-fold cross-validation and obtaining RMSE10折交叉验证并获得RMSE
【发布时间】:2023-04-03 14:44:01
【问题描述】:

我正在尝试使用 scikit learn 中的 KFold 模块将我从对完整数据集执行多元线性回归的 RMSE 与 10 倍交叉验证的 RMSE 进行比较。我发现了一些我试图调整的代码,但我无法让它工作(我怀疑它从一开始就没有工作过。

TIA 寻求帮助!

这是我的线性回归函数

  def standRegres(xArr,yArr):
      xMat = np.mat(xArr); yMat = np.mat(yArr).T
      xTx = xMat.T*xMat
      if np.linalg.det(xTx) == 0.0:
          print("This matrix is singular, cannot do inverse")
          return
      ws = xTx.I * (xMat.T*yMat)
      return ws

  ##  I run it on my matrix ("comm_df") and my dependent var (comm_target)

  ##  Calculate RMSE (omitted some code)

  initial_regress_RMSE = np.sqrt(np.mean((yHat_array - comm_target_array)**2)

  ##  Now trying to get RMSE after training model through 10-fold cross validation

  from sklearn.model_selection import KFold
  from sklearn.linear_model import LinearRegression

  kf = KFold(n_splits=10)
  xval_err = 0
  for train, test in kf:
      linreg.fit(comm_df,comm_target)
      p = linreg.predict(comm_df)
      e = p-comm_target
      xval_err += np.sqrt(np.dot(e,e)/len(comm_df))

  rmse_10cv = xval_err/10

我收到关于 kfold 对象不可迭代的错误

【问题讨论】:

    标签: python scikit-learn linear-regression mse k-fold


    【解决方案1】:

    您需要在此代码中更正几处。

    • 您不能迭代 kf。你只能迭代kf.split(comm_df)

    • 您需要以某种方式使用 KFold 提供的训练测试拆分。您没有在代码中使用它们! KFold 的目标是根据训练观察拟合您的回归,并根据测试观察评估回归(即在您的情况下计算 RMSE)。

    考虑到这一点,这就是我将如何更正您的代码(这里假设您的数据位于 numpy 数组中,但您可以轻松切换到 pandas)

    kf = KFold(n_splits=10)
    xval_err = 0
    for train, test in kf.split(comm_df):
        linreg.fit(comm_df[train],comm_target[train])
        p = linreg.predict(comm_df[test])
        e = p-comm_label[test]
        xval_err += np.sqrt(np.dot(e,e)/len(comm_target[test]))
    
    rmse_10cv = xval_err/10
    

    【讨论】:

      【解决方案2】:

      所以您提供的代码仍然抛出错误。我放弃了上面的内容,转而采用以下方法:

      ## KFold cross-validation
      
      from sklearn.model_selection import KFold
      from sklearn.linear_model import LinearRegression
      
      ## Define variables for the for loop
      
      kf = KFold(n_splits=10)
      RMSE_sum=0
      RMSE_length=10
      X = np.array(comm_df)
      y = np.array(comm_target)
      
      for loop_number, (train, test) in enumerate(kf.split(X)):
      
          ## Get Training Matrix and Vector
      
          training_X_array = X[train]
          training_y_array = y[train].reshape(-1, 1)
      
          ## Get Testing Matrix Values
      
          X_test_array = X[test]
          y_actual_values = y[test]
      
          ## Fit the Linear Regression Model
      
          lr_model = LinearRegression().fit(training_X_array, training_y_array)
      
          ## Compute the predictions for the test data
      
          prediction = lr_model.predict(X_test_array)      
          crime_probabilites = np.array(prediction)   
      
          ## Calculate the RMSE
      
          RMSE_cross_fold = RMSEcalc(crime_probabilites, y_actual_values)
      
          ## Add each RMSE_cross_fold value to the sum
      
          RMSE_sum=RMSE_cross_fold+RMSE_sum
      
      ## Calculate the average and print    
      
      RMSE_cross_fold_avg=RMSE_sum/RMSE_length
      
      print('The Mean RMSE across all folds is',RMSE_cross_fold_avg)
      

      【讨论】:

        猜你喜欢
        • 2011-11-29
        • 2020-02-10
        • 2012-05-11
        • 2021-06-03
        • 2013-08-16
        • 2012-01-07
        • 1970-01-01
        • 2014-04-19
        • 2017-12-09
        相关资源
        最近更新 更多