自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在評估機器學習模型時防止數(shù)據(jù)泄漏

人工智能 深度學習
本文討論了評估模型性能時的數(shù)據(jù)泄漏問題以及避免數(shù)據(jù)泄漏的方法。

 本文討論了評估模型性能時的數(shù)據(jù)泄漏問題以及避免數(shù)據(jù)泄漏的方法。

 

如何在評估機器學習模型時防止數(shù)據(jù)泄漏

 

在模型評估過程中,當訓(xùn)練集的數(shù)據(jù)進入驗證/測試集時,就會發(fā)生數(shù)據(jù)泄漏。這將導(dǎo)致模型對驗證/測試集的性能評估存在偏差。讓我們用一個使用Scikit-Learn的“波士頓房價”數(shù)據(jù)集的例子來理解它。數(shù)據(jù)集沒有缺失值,因此隨機引入100個缺失值,以便更好地演示數(shù)據(jù)泄漏。

 

  1. import numpy as np  
  2. import pandas as pd  
  3. from sklearn.datasets import load_boston  
  4. from sklearn.preprocessing import StandardScaler  
  5. from sklearn.pipeline import Pipeline  
  6. from sklearn.impute import SimpleImputer  
  7. from sklearn.neighbors import KNeighborsRegressor  
  8. from sklearn.model_selection import cross_validate, train_test_split  
  9. from sklearn.metrics import mean_squared_error  
  10.  
  11. #Importing the dataset  
  12. data = pd.DataFrame(load_boston()['data'],columns=load_boston()['feature_names'])  
  13. data['target'] = load_boston()['target']  
  14.  
  15.  
  16. #Split the input and target features  
  17. X = data.iloc[:,:-1].copy()  
  18. y = data.iloc[:,-1].copy()  
  19.  
  20.  
  21. # Adding 100 random missing values  
  22. np.random.seed(11)  
  23. rand_cols = np.random.randint(0,X.shape[1],100)  
  24. rand_rows = np.random.randint(0,X.shape[0],100)  
  25. for i,j in zip(rand_rows,rand_cols):  
  26. X.iloc[i,j] = np.nan  
  27.  
  28. #Splitting the data into training and test sets  
  29. X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=11)  
  30.  
  31. #Initislizing KNN Regressor  
  32. knn = KNeighborsRegressor()  
  33.  
  34. #Initializing mode imputer  
  35. imp = SimpleImputer(strategy='most_frequent')  
  36.  
  37. #Initializing StandardScaler  
  38. standard_scaler = StandardScaler()  
  39.  
  40. #Imputing and scaling X_train  
  41. X_train_impute = imp.fit_transform(X_train).copy()  
  42. X_train_scaled = standard_scaler.fit_transform(X_train_impute).copy()  
  43.  
  44. #Running 5-fold cross-validation  
  45. cv = cross_validate(estimator=knn,X=X_train_scaled,y=y_train,cv=5,scoring="neg_root_mean_squared_error",return_train_score=True)  
  46.  
  47. #Calculating mean of the training scores of cross-validation  
  48. print(f'Training RMSE (with data leakage): {-1 * np.mean(cv["train_score"])}')  
  49.  
  50. #Calculating mean of the validation scores of cross-validation  
  51. print(f'validation RMSE (with data leakage): {-1 * np.mean(cv["test_score"])}')  
  52.  
  53. #fitting the model to the training data  
  54. lr.fit(X_train_scaled,y_train)  
  55.  
  56. #preprocessing the test data  
  57. X_test_impute = imp.transform(X_test).copy()  
  58. X_test_scaled = standard_scaler.transform(X_test_impute).copy()  
  59.  
  60. #Predictions and model evaluation on unseen data  
  61. pred = lr.predict(X_test_scaled)  
  62. print(f'RMSE on unseen data: {np.sqrt(mean_squared_error(y_test,pred))}'

 

 

如何在評估機器學習模型時防止數(shù)據(jù)泄漏

 

在上面的代碼中,‘Xtrain’是訓(xùn)練集(k-fold交叉驗證),‘Xtest’用于對看不見的數(shù)據(jù)進行模型評估。上面的代碼是一個帶有數(shù)據(jù)泄漏的模型評估示例,其中,用于估算缺失值的模式(strategy= ' mostfrequent ')在' Xtrain '上計算。類似地,用于縮放數(shù)據(jù)的均值和標準偏差也使用' Xtrain '計算。' Xtrain的缺失值將被輸入,' X_train '在k-fold交叉驗證之前進行縮放。

在k-fold交叉驗證中,' Xtrain '被分割成' k '折疊。在每次k-fold交叉驗證迭代中,其中一個折用于驗證(我們稱其為驗證部分),其余的折用于訓(xùn)練(我們稱其為訓(xùn)練部分)。每次迭代中的訓(xùn)練和驗證部分都有已經(jīng)使用' Xtrain '計算的模式輸入的缺失值。類似地,它們已經(jīng)使用在' Xtrain '上計算的平均值和標準偏差進行了縮放。這種估算和縮放操作會導(dǎo)致來自' Xtrain '的信息泄露到k-fold交叉驗證的訓(xùn)練和驗證部分。這種信息泄漏可能導(dǎo)致模型在驗證部分上的性能估計有偏差。下面的代碼展示了一種通過使用管道來避免它的方法。

 

  1. #Preprocessing and regressor pipeline  
  2. pipeline = Pipeline(steps=[['imputer',imp],['scaler',standard_scaler],['regressor',knn]])  
  3.  
  4. #Running 5-fold cross-validation using pipeline as estimator  
  5. cv = cross_validate(estimator=pipeline,X=X_train,y=y_train,cv=5,scoring="neg_root_mean_squared_error",return_train_score=True)  
  6.  
  7. #Calculating mean of the training scores of cross-validation  
  8. print(f'Training RMSE (without data leakage): {-1 * np.mean(cv["train_score"])}')  
  9.  
  10. #Calculating mean of the validation scores of cross-validation  
  11. print(f'validation RMSE (without data leakage): {-1 * np.mean(cv["test_score"])}')  
  12.  
  13. #fitting the pipeline to the training data  
  14. pipeline.fit(X_train,y_train)  
  15.  
  16. #Predictions and model evaluation on unseen data  
  17. pred = pipeline.predict(X_test)  
  18. print(f'RMSE on unseen data: {np.sqrt(mean_squared_error(y_test,pred))}'

 

在上面的代碼中,我們已經(jīng)在管道中包含了輸入器、標量和回歸器。在本例中,' X_train '被分割為5個折,在每次迭代中,管道使用訓(xùn)練部分計算用于輸入訓(xùn)練和驗證部分中缺失值的模式。同樣,用于衡量訓(xùn)練和驗證部分的平均值和標準偏差也在訓(xùn)練部分上計算。這一過程消除了數(shù)據(jù)泄漏,因為在每次k-fold交叉驗證迭代中,都在訓(xùn)練部分計算歸責模式和縮放的均值和標準偏差。在每次k-fold交叉驗證迭代中,這些值用于計算和擴展訓(xùn)練和驗證部分。

我們可以看到在有數(shù)據(jù)泄漏和沒有數(shù)據(jù)泄漏的情況下計算的訓(xùn)練和驗證rmse的差異。由于數(shù)據(jù)集很小,我們只能看到它們之間的微小差異。在大數(shù)據(jù)集的情況下,這個差異可能會很大。對于看不見的數(shù)據(jù),驗證RMSE(帶有數(shù)據(jù)泄漏)接近RMSE只是偶然的。

因此,使用管道進行k-fold交叉驗證可以防止數(shù)據(jù)泄漏,并更好地評估模型在不可見數(shù)據(jù)上的性能。

責任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2020-09-22 14:59:52

機器學習人工智能計算機

2022-10-08 11:11:25

機器學習人工智能

2020-09-25 09:59:52

人工智能

2022-08-10 15:56:40

機器學習算法深度學習

2021-12-09 19:13:10

遠程辦公時數(shù)據(jù)泄露網(wǎng)絡(luò)攻擊

2010-05-17 22:53:19

2010-05-07 15:22:41

數(shù)據(jù)安全數(shù)據(jù)泄漏DLP

2020-06-24 07:53:03

機器學習技術(shù)人工智能

2010-09-03 16:44:22

2023-12-18 10:45:23

內(nèi)存泄漏計算機服務(wù)器

2010-09-06 10:26:30

2023-07-21 12:48:37

2020-07-01 08:48:01

Python機器學習工具

2024-11-26 08:09:40

2021-11-02 09:40:50

TensorFlow機器學習人工智能

2017-07-07 14:41:13

機器學習神經(jīng)網(wǎng)絡(luò)JavaScript

2022-06-02 15:42:05

Python機器學習

2017-10-13 15:59:24

iPhone機器學習iOS

2017-07-13 10:12:58

機器學習

2017-03-24 15:58:46

互聯(lián)網(wǎng)
點贊
收藏

51CTO技術(shù)棧公眾號