簡體   English   中英

10折交叉驗證並獲得RMSE

[英]10-fold cross-validation and obtaining RMSE

我正在嘗試使用 scikit learn 中的 KFold 模塊將我從對完整數據集執行多重線性回歸的 RMSE 與 10 倍交叉驗證的 RMSE 進行比較。 我發現了一些我試圖調整的代碼,但我無法讓它工作(我懷疑它從一開始就沒有工作過。

TIA 尋求幫助!

這是我的線性回歸 function

  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 object 如何不可迭代的錯誤

您需要在此代碼中更正幾件事。

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

  • 您需要以某種方式使用 KFold 提供的訓練測試拆分。 您沒有在代碼中使用它們,KFold 的目標是使您的回歸適合訓練觀察。 並評估測試觀察的回歸(即在您的情況下計算 RMSE)。

考慮到這一點,我將如何更正您的代碼(假設您的數據在 numpy arrays 中,但您可以輕松切換到 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

所以你提供的代碼仍然拋出錯誤。 我放棄了上面的內容,轉而使用以下內容,這很有效:

## 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM