added CV codes to regression
This commit is contained in:
@@ -3259,6 +3259,216 @@ plt.show()
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
===== More examples on bootstrap and cross-validation and errors =====
|
||||
|
||||
!bc pycod
|
||||
# Common imports
|
||||
import os
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.utils import resample
|
||||
from sklearn.metrics import mean_squared_error
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
if not os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
if not os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
def image_path(fig_id):
|
||||
return os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
def data_path(dat_id):
|
||||
return os.path.join(DATA_ID, dat_id)
|
||||
|
||||
def save_fig(fig_id):
|
||||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||||
|
||||
infile = open(data_path("EoS.csv"),'r')
|
||||
|
||||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||||
EoS = EoS.dropna()
|
||||
Energies = EoS['Energy']
|
||||
Density = EoS['Density']
|
||||
# The design matrix now as function of various polytrops
|
||||
|
||||
Maxpolydegree = 30
|
||||
X = np.zeros((len(Density),Maxpolydegree))
|
||||
X[:,0] = 1.0
|
||||
testerror = np.zeros(Maxpolydegree)
|
||||
trainingerror = np.zeros(Maxpolydegree)
|
||||
polynomial = np.zeros(Maxpolydegree)
|
||||
|
||||
trials = 100
|
||||
for polydegree in range(1, Maxpolydegree):
|
||||
polynomial[polydegree] = polydegree
|
||||
for degree in range(polydegree):
|
||||
X[:,degree] = Density**(degree/3.0)
|
||||
|
||||
# loop over trials in order to estimate the expectation value of the MSE
|
||||
testerror[polydegree] = 0.0
|
||||
trainingerror[polydegree] = 0.0
|
||||
for samples in range(trials):
|
||||
x_train, x_test, y_train, y_test = train_test_split(X, Energies, test_size=0.2)
|
||||
model = LinearRegression(fit_intercept=True).fit(x_train, y_train)
|
||||
ypred = model.predict(x_train)
|
||||
ytilde = model.predict(x_test)
|
||||
testerror[polydegree] += mean_squared_error(y_test, ytilde)
|
||||
trainingerror[polydegree] += mean_squared_error(y_train, ypred)
|
||||
|
||||
testerror[polydegree] /= trials
|
||||
trainingerror[polydegree] /= trials
|
||||
print("Degree of polynomial: %3d"% polynomial[polydegree])
|
||||
print("Mean squared error on training data: %.8f" % trainingerror[polydegree])
|
||||
print("Mean squared error on test data: %.8f" % testerror[polydegree])
|
||||
|
||||
plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
|
||||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||||
plt.xlabel('Polynomial degree')
|
||||
plt.ylabel('log10[MSE]')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
!split
|
||||
===== The same example but now with cross-validation =====
|
||||
|
||||
!bc pycod
|
||||
# Common imports
|
||||
import os
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||||
from sklearn.metrics import mean_squared_error
|
||||
from sklearn.model_selection import KFold
|
||||
from sklearn.model_selection import cross_val_score
|
||||
|
||||
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
if not os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
if not os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
def image_path(fig_id):
|
||||
return os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
def data_path(dat_id):
|
||||
return os.path.join(DATA_ID, dat_id)
|
||||
|
||||
def save_fig(fig_id):
|
||||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||||
|
||||
infile = open(data_path("EoS.csv"),'r')
|
||||
|
||||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||||
EoS = EoS.dropna()
|
||||
Energies = EoS['Energy']
|
||||
Density = EoS['Density']
|
||||
# The design matrix now as function of various polytrops
|
||||
|
||||
Maxpolydegree = 30
|
||||
X = np.zeros((len(Density),Maxpolydegree))
|
||||
X[:,0] = 1.0
|
||||
estimated_mse_sklearn = np.zeros(Maxpolydegree)
|
||||
polynomial = np.zeros(Maxpolydegree)
|
||||
k =5
|
||||
kfold = KFold(n_splits = k)
|
||||
|
||||
for polydegree in range(1, Maxpolydegree):
|
||||
polynomial[polydegree] = polydegree
|
||||
for degree in range(polydegree):
|
||||
X[:,degree] = Density**(degree/3.0)
|
||||
OLS = LinearRegression()
|
||||
# loop over trials in order to estimate the expectation value of the MSE
|
||||
estimated_mse_folds = cross_val_score(OLS, X, Energies, scoring='neg_mean_squared_error', cv=kfold)
|
||||
#[:, np.newaxis]
|
||||
estimated_mse_sklearn[polydegree] = np.mean(-estimated_mse_folds)
|
||||
|
||||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||||
plt.xlabel('Polynomial degree')
|
||||
plt.ylabel('log10[MSE]')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
!split
|
||||
===== Cross-validation with Ridge =====
|
||||
!bc pycod
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import KFold
|
||||
from sklearn.linear_model import Ridge
|
||||
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.
|
||||
np.random.seed(3155)
|
||||
# Generate the data.
|
||||
n = 100
|
||||
x = np.linspace(-3, 3, n).reshape(-1, 1)
|
||||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
|
||||
# Decide degree on polynomial to fit
|
||||
poly = PolynomialFeatures(degree = 10)
|
||||
|
||||
# Decide which values of lambda to use
|
||||
nlambdas = 500
|
||||
lambdas = np.logspace(-3, 5, nlambdas)
|
||||
# Initialize a KFold instance
|
||||
k = 5
|
||||
kfold = KFold(n_splits = k)
|
||||
estimated_mse_sklearn = np.zeros(nlambdas)
|
||||
i = 0
|
||||
for lmb in lambdas:
|
||||
ridge = Ridge(alpha = lmb)
|
||||
estimated_mse_folds = cross_val_score(ridge, x, y, scoring='neg_mean_squared_error', cv=kfold)
|
||||
estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
|
||||
i += 1
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
|
||||
plt.xlabel('log10(lambda)')
|
||||
plt.ylabel('MSE')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
!split
|
||||
===== The Ising model =====
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
@@ -0,0 +1,90 @@
|
||||
3.3773726001100143E-005, 3.1715032621225665E-002
|
||||
2.7018980800880114E-004, 0.25379346864959029
|
||||
9.1189060202970370E-004, 0.85691856357595775
|
||||
2.1615184640704091E-003, 2.0322700794292126
|
||||
4.2217157501375172E-003, 3.9716946611295496
|
||||
7.2951248162376296E-003, 6.8678138410008760
|
||||
1.1584388018377344E-002, 10.913973030767592
|
||||
1.7292147712563273E-002, 16.304871533080519
|
||||
2.4621046254802003E-002, 23.235226466525493
|
||||
3.3773726001100138E-002, 31.902701432416372
|
||||
4.4952829307464297E-002, 42.504284679798289
|
||||
5.8360998529901037E-002, 55.243642496340065
|
||||
7.4200876024417023E-002, 70.325036648868448
|
||||
9.2675104147018753E-002, 87.967299710326188
|
||||
0.11398632525371297, 108.39264632432710
|
||||
0.13833718170050618 , 131.84282952216495
|
||||
0.16593031584340495 , 158.57124952965228
|
||||
0.19696837003841602 , 188.82820513039681
|
||||
0.203199995458126059, 195.042764094891368
|
||||
0.211199995279312130, 202.937172130123315
|
||||
0.219199995100498229, 210.852580165355278
|
||||
0.227199994921684245, 218.789988200587175
|
||||
0.235199994742870316, 226.748396235819115
|
||||
0.243199994564056388, 234.740804271051104
|
||||
0.251199994385242487, 242.757212306283037
|
||||
0.259199994206428530, 250.797620341515000
|
||||
0.267199994027614574, 258.874028376746878
|
||||
0.275199993848800673, 266.977436411978829
|
||||
0.283199993669986716, 275.092844447210780
|
||||
0.291199993491172815, 283.248252482442751
|
||||
0.299199993312358858, 291.445660517674696
|
||||
0.307199993133544902, 299.655068552906641
|
||||
0.315199992954731001, 307.909476588138546
|
||||
0.323199992775917044, 316.192884623370503
|
||||
0.339199992418289187, 332.846700693834407
|
||||
0.355199992060661329, 349.620516764298316
|
||||
0.371199991703033416, 366.537332834762140
|
||||
0.387199991345405559, 383.604148905225998
|
||||
0.403199990987777701, 400.801964975689941
|
||||
0.419199990630149844, 418.158781046153820
|
||||
0.435199990272521986, 435.624597116617792
|
||||
0.451199989914894073, 453.288413187081574
|
||||
0.467199989557266215, 471.062229257545425
|
||||
0.483199989199638358, 489.048045328009380
|
||||
0.499199988842010500, 507.146861398473277
|
||||
0.515199988484382643, 525.392677468937222
|
||||
0.531199988126754730, 543.829493539401028
|
||||
0.531999988108873390, 544.796634342924222
|
||||
0.550079987704753859, 565.860416502548446
|
||||
0.567999987304210641, 586.865970501467928
|
||||
0.585599986910820047, 607.703068178978242
|
||||
0.603199986517429343, 628.645165856488575
|
||||
0.620639986127614951, 649.578035373294142
|
||||
0.637919985741376872, 670.496676729395176
|
||||
0.655199985355138792, 691.549318085496111
|
||||
0.672479984968900713, 712.766959441597237
|
||||
0.689599984586238834, 733.981372636993456
|
||||
0.706879984200000755, 755.521013993094471
|
||||
0.724159983813762675, 777.228655349195492
|
||||
0.741439983427524596, 799.117296705296553
|
||||
0.758719983041286516, 821.191938061397536
|
||||
0.775999982655048326, 843.460579417498366
|
||||
0.793439982265233934, 866.080448934304059
|
||||
0.810879981875419542, 888.907318451109631
|
||||
0.828479981482028949, 912.100416128620054
|
||||
0.846239981085061932, 935.664741966834868
|
||||
0.864159980684518825, 959.607295965754474
|
||||
0.882079980283975607, 983.787849964674024
|
||||
0.900159979879856187, 1008.36063212429838
|
||||
0.918399979472160344, 1033.33564244462718
|
||||
0.936639979064464612, 1058.56865276495591
|
||||
0.955199978649616255, 1084.36811940669395
|
||||
0.973919978231191585, 1110.59381420913678
|
||||
0.992799977809190715, 1137.25073717228429
|
||||
1.01183997738361353, 1164.35288829613614
|
||||
1.06047997629642499, 1234.41724915034661
|
||||
1.11039997518062594, 1307.72443529019392
|
||||
1.16191997402906422, 1384.74890303708753
|
||||
1.21535997283458719, 1466.00110871243692
|
||||
1.27071997159719463, 1551.73305231624181
|
||||
1.32847997030615828, 1642.68741833061654
|
||||
1.38895996895432461, 1739.56266307697001
|
||||
1.45263996753096580, 1843.32947103741640
|
||||
1.52031996601820008, 1955.19398301547858
|
||||
1.59295996439456933, 2077.14256797538474
|
||||
1.67167996263504026, 2211.44382304206692
|
||||
1.75855996069312082, 2361.74471430468566
|
||||
1.85647995850443825, 2533.64534865592441
|
||||
1.96959995597600934, 2734.93465827410455
|
||||
2.10543995293974895, 2980.03336671234320
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,84 @@
|
||||
# Common imports
|
||||
import os
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.utils import resample
|
||||
from sklearn.metrics import mean_squared_error
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
if not os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
if not os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
def image_path(fig_id):
|
||||
return os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
def data_path(dat_id):
|
||||
return os.path.join(DATA_ID, dat_id)
|
||||
|
||||
def save_fig(fig_id):
|
||||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||||
|
||||
infile = open(data_path("EoS.csv"),'r')
|
||||
|
||||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||||
EoS = EoS.dropna()
|
||||
Energies = EoS['Energy']
|
||||
Density = EoS['Density']
|
||||
# The design matrix now as function of various polytrops
|
||||
|
||||
Maxpolydegree = 30
|
||||
X = np.zeros((len(Density),Maxpolydegree))
|
||||
X[:,0] = 1.0
|
||||
testerror = np.zeros(Maxpolydegree)
|
||||
trainingerror = np.zeros(Maxpolydegree)
|
||||
polynomial = np.zeros(Maxpolydegree)
|
||||
|
||||
trials = 100
|
||||
for polydegree in range(1, Maxpolydegree):
|
||||
polynomial[polydegree] = polydegree
|
||||
for degree in range(polydegree):
|
||||
X[:,degree] = Density**(degree/3.0)
|
||||
|
||||
# loop over trials in order to estimate the expectation value of the MSE
|
||||
testerror[polydegree] = 0.0
|
||||
trainingerror[polydegree] = 0.0
|
||||
for samples in range(trials):
|
||||
x_train, x_test, y_train, y_test = train_test_split(X, Energies, test_size=0.2)
|
||||
model = LinearRegression(fit_intercept=True).fit(x_train, y_train)
|
||||
ypred = model.predict(x_train)
|
||||
ytilde = model.predict(x_test)
|
||||
testerror[polydegree] += mean_squared_error(y_test, ytilde)
|
||||
trainingerror[polydegree] += mean_squared_error(y_train, ypred)
|
||||
|
||||
testerror[polydegree] /= trials
|
||||
trainingerror[polydegree] /= trials
|
||||
print("Degree of polynomial: %3d"% polynomial[polydegree])
|
||||
print("Mean squared error on training data: %.8f" % trainingerror[polydegree])
|
||||
print("Mean squared error on test data: %.8f" % testerror[polydegree])
|
||||
|
||||
plt.plot(polynomial, np.log10(trainingerror), label='Training Error')
|
||||
plt.plot(polynomial, np.log10(testerror), label='Test Error')
|
||||
plt.xlabel('Polynomial degree')
|
||||
plt.ylabel('log10[MSE]')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# Common imports
|
||||
import os
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||||
from sklearn.metrics import mean_squared_error
|
||||
from sklearn.model_selection import KFold
|
||||
from sklearn.model_selection import cross_val_score
|
||||
|
||||
|
||||
# Where to save the figures and data files
|
||||
PROJECT_ROOT_DIR = "Results"
|
||||
FIGURE_ID = "Results/FigureFiles"
|
||||
DATA_ID = "DataFiles/"
|
||||
|
||||
if not os.path.exists(PROJECT_ROOT_DIR):
|
||||
os.mkdir(PROJECT_ROOT_DIR)
|
||||
|
||||
if not os.path.exists(FIGURE_ID):
|
||||
os.makedirs(FIGURE_ID)
|
||||
|
||||
if not os.path.exists(DATA_ID):
|
||||
os.makedirs(DATA_ID)
|
||||
|
||||
def image_path(fig_id):
|
||||
return os.path.join(FIGURE_ID, fig_id)
|
||||
|
||||
def data_path(dat_id):
|
||||
return os.path.join(DATA_ID, dat_id)
|
||||
|
||||
def save_fig(fig_id):
|
||||
plt.savefig(image_path(fig_id) + ".png", format='png')
|
||||
|
||||
infile = open(data_path("EoS.csv"),'r')
|
||||
|
||||
# Read the EoS data as csv file and organize the data into two arrays with density and energies
|
||||
EoS = pd.read_csv(infile, names=('Density', 'Energy'))
|
||||
EoS['Energy'] = pd.to_numeric(EoS['Energy'], errors='coerce')
|
||||
EoS = EoS.dropna()
|
||||
Energies = EoS['Energy']
|
||||
Density = EoS['Density']
|
||||
# The design matrix now as function of various polytrops
|
||||
|
||||
Maxpolydegree = 30
|
||||
X = np.zeros((len(Density),Maxpolydegree))
|
||||
X[:,0] = 1.0
|
||||
estimated_mse_sklearn = np.zeros(Maxpolydegree)
|
||||
polynomial = np.zeros(Maxpolydegree)
|
||||
k =5
|
||||
kfold = KFold(n_splits = k)
|
||||
|
||||
for polydegree in range(1, Maxpolydegree):
|
||||
polynomial[polydegree] = polydegree
|
||||
for degree in range(polydegree):
|
||||
X[:,degree] = Density**(degree/3.0)
|
||||
OLS = LinearRegression()
|
||||
# loop over trials in order to estimate the expectation value of the MSE
|
||||
estimated_mse_folds = cross_val_score(OLS, X, Energies, scoring='neg_mean_squared_error', cv=kfold)
|
||||
#[:, np.newaxis]
|
||||
estimated_mse_sklearn[polydegree] = np.mean(-estimated_mse_folds)
|
||||
|
||||
plt.plot(polynomial, np.log10(estimated_mse_sklearn), label='Test Error')
|
||||
plt.xlabel('Polynomial degree')
|
||||
plt.ylabel('log10[MSE]')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.linear_model import LinearRegression, Ridge, Lasso
|
||||
from sklearn.preprocessing import PolynomialFeatures
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.utils import resample
|
||||
from sklearn import preprocessing
|
||||
|
||||
|
||||
np.random.seed(2018)
|
||||
|
||||
n = 40
|
||||
n_boostraps = 100
|
||||
maxdegree = 14
|
||||
|
||||
|
||||
# Make data set.
|
||||
x = np.linspace(-3, 3, n).reshape(-1, 1)
|
||||
x = preprocessing.scale(x)
|
||||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
|
||||
error = np.zeros(maxdegree)
|
||||
bias = np.zeros(maxdegree)
|
||||
variance = np.zeros(maxdegree)
|
||||
polydegree = np.zeros(maxdegree)
|
||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
||||
|
||||
for degree in range(maxdegree):
|
||||
model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False))
|
||||
y_pred = np.empty((y_test.shape[0], n_boostraps))
|
||||
for i in range(n_boostraps):
|
||||
x_, y_ = resample(x_train, y_train)
|
||||
y_pred[:, i] = model.fit(x_, y_).predict(x_test).ravel()
|
||||
|
||||
polydegree[degree] = degree
|
||||
error[degree] = np.mean( np.mean((y_test - y_pred)**2, axis=1, keepdims=True) )
|
||||
bias[degree] = np.mean( (y_test - np.mean(y_pred, axis=1, keepdims=True))**2 )
|
||||
variance[degree] = np.mean( np.var(y_pred, axis=1, keepdims=True) )
|
||||
|
||||
plt.plot(polydegree, error, label='Error')
|
||||
plt.plot(polydegree, bias, label='bias')
|
||||
plt.plot(polydegree, variance, label='Variance')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
@@ -0,0 +1,36 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import KFold
|
||||
from sklearn.linear_model import Ridge
|
||||
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.
|
||||
np.random.seed(3155)
|
||||
# Generate the data.
|
||||
n = 100
|
||||
x = np.linspace(-3, 3, n).reshape(-1, 1)
|
||||
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape)
|
||||
# Decide degree on polynomial to fit
|
||||
poly = PolynomialFeatures(degree = 10)
|
||||
|
||||
# Decide which values of lambda to use
|
||||
nlambdas = 500
|
||||
lambdas = np.logspace(-3, 5, nlambdas)
|
||||
# Initialize a KFold instance
|
||||
k = 5
|
||||
kfold = KFold(n_splits = k)
|
||||
estimated_mse_sklearn = np.zeros(nlambdas)
|
||||
i = 0
|
||||
for lmb in lambdas:
|
||||
ridge = Ridge(alpha = lmb)
|
||||
estimated_mse_folds = cross_val_score(ridge, x, y, scoring='neg_mean_squared_error', cv=kfold)
|
||||
estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
|
||||
i += 1
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
|
||||
plt.xlabel('log10(lambda)')
|
||||
plt.ylabel('MSE')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user