week 37
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import KFold
|
||||
from sklearn.linear_model import Ridge, LinearRegression
|
||||
from sklearn.model_selection import cross_val_score
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
|
||||
# A seed just to ensure that the random numbers are the same for every run.
|
||||
# Useful for eventual debugging.
|
||||
np.random.seed(3155)
|
||||
|
||||
# Generate the data.
|
||||
nsamples = 1000
|
||||
x = np.random.randn(nsamples)
|
||||
y = 3*x**2 + np.random.randn(nsamples)
|
||||
|
||||
## Cross-validation on Ridge regression using KFold only
|
||||
|
||||
# Decide degree on polynomial to fit
|
||||
poly = PolynomialFeatures(degree = 6)
|
||||
|
||||
|
||||
# Initialize a KFold instance
|
||||
k = 10
|
||||
kfold = KFold(n_splits = k)
|
||||
|
||||
# Perform the cross-validation to estimate MSE using OLS
|
||||
scores_KFold = np.zeros((k))
|
||||
model = LinearRegression()
|
||||
j = 0
|
||||
for train_inds, test_inds in kfold.split(x):
|
||||
xtrain = x[train_inds]
|
||||
ytrain = y[train_inds]
|
||||
xtest = x[test_inds]
|
||||
ytest = y[test_inds]
|
||||
Xtrain = poly.fit_transform(xtrain[:, np.newaxis])
|
||||
model.fit(Xtrain, ytrain[:, np.newaxis])
|
||||
Xtest = poly.fit_transform(xtest[:, np.newaxis])
|
||||
ypred = model.predict(Xtest)
|
||||
scores_KFold[j] = np.sum((ypred - ytest[:, np.newaxis])**2)/np.size(ypred)
|
||||
print(f"Score for each fold:{scores_KFold[j]}")
|
||||
j += 1
|
||||
|
||||
|
||||
estimated_mse_KFold = np.mean(scores_KFold)
|
||||
print(f"Average OLS score:{estimated_mse_KFold}")
|
||||
|
||||
@@ -5,11 +5,9 @@ DATE: today
|
||||
!split
|
||||
===== Plans for week 37 =====
|
||||
|
||||
* Thursday September 16: Summary of Ridge and Lasso with examples and start resampling techniques
|
||||
o "Video of Lecture, first part":"https://www.uio.no/studier/emner/matnat/fys/FYS-STK3155/h21/forelesningsvideoer/LectureSeptember16Firstpart.mp4?vrtx=view-as-webpage"
|
||||
o "Video of Lecture, second part":"https://www.uio.no/studier/emner/matnat/fys/FYS-STK3155/h21/forelesningsvideoer/LectureSeptember16SecondPart.mp4?vrtx=view-as-webpage"
|
||||
* Friday September 17: Resampling methods, Cross-validation, Bootstrapping and jackknife
|
||||
o "Video of Lecture":"https://www.uio.no/studier/emner/matnat/fys/FYS-STK3155/h21/forelesningsvideoer/LectureSeptember17.mp4?vrtx=view-as-webpage"
|
||||
* Thursday September 15: Summary of Ridge and Lasso with examples and start resampling techniques
|
||||
* Friday September 16: Resampling methods, Cross-validation, Bootstrapping and jackknife
|
||||
|
||||
Recommended Reading:
|
||||
o Lectures on Resampling methods (these lectures)
|
||||
o Bishop 1.3 (cross-validation) and 3.2 (bias-variance tradeoff)
|
||||
@@ -18,7 +16,7 @@ o Hastie et al Chapter 7, here we recommend 7.1-7.5 and 7.10 (cross-validation)
|
||||
|
||||
|
||||
!split
|
||||
===== Thursday September 16, Summary of Ridge and Lasso Regression and start Resampling methods =====
|
||||
===== Thursday September 15, Summary of Ridge and Lasso Regression and start Resampling methods =====
|
||||
|
||||
|
||||
!split
|
||||
|
||||
Reference in New Issue
Block a user