Untitled
unknown
python
3 years ago
1.7 kB
6
Indexable
import numpy as np import random from sklearn.linear_model import LinearRegression from sklearn.model_selection import KFold np.random.seed(101) random.seed(123) hold_out_errors = [] loocv_errors = [] cv5_errors = [] for _ in range(100): n = 100 x = np.random.uniform(-3, 3, n) ones = np.repeat(1, n) X = np.array([x, ones]).T y = 2*x+1 + np.random.normal(0, 1, n) # Hold out indices = np.array(random.sample(range(n), int(n/2))) reg = LinearRegression().fit(X[indices], y[indices]) preds = reg.predict(X[~indices]) error = np.mean((preds - y[~indices])**2) hold_out_errors.append(error) # 5-fold kf = KFold(n_splits=5) errors = [] for train_index, test_index in kf.split(x): reg = LinearRegression().fit(X[train_index], y[train_index]) preds = reg.predict(X[test_index]) error = np.mean((preds - y[test_index])**2) errors.append(error) errors = np.array(errors) cv5_errors.append(errors.mean()) # LOOCV kf = KFold(n_splits=n) errors = [] for train_index, test_index in kf.split(x): reg = LinearRegression().fit(X[train_index], y[train_index]) preds = reg.predict(X[test_index]) error = np.mean((preds - y[test_index])**2) errors.append(error) errors = np.array(errors) loocv_errors.append(errors.mean()) hold_out_errors = np.array(hold_out_errors) cv5_errors = np.array(cv5_errors) loocv_errors = np.array(loocv_errors) print("Hold out") print(hold_out_errors.mean()) print(hold_out_errors.std()) print("CV5") print(cv5_errors.mean()) print(cv5_errors.std()) print("LOOCV") print(loocv_errors.mean()) print(loocv_errors.std())
Editor is loading...