small omission in code
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
Code to test Ridge and NNs using Scikit-Learn only
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn import linear_model
|
||||
from sklearn.neural_network import MLPRegressor
|
||||
from sklearn.metrics import accuracy_score
|
||||
import seaborn as sns
|
||||
|
||||
|
||||
def MSE(y_data,y_model):
|
||||
n = np.size(y_model)
|
||||
return np.sum((y_data-y_model)**2)/n
|
||||
# A seed just to ensure that the random numbers are the same for every run.
|
||||
# Useful for eventual debugging.
|
||||
np.random.seed(315)
|
||||
|
||||
n = 1000
|
||||
x = np.random.rand(n)
|
||||
y = x+x*x
|
||||
X = np.zeros((n,1))
|
||||
X[:,0] = x
|
||||
|
||||
|
||||
# We split the data in test and training data
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||||
|
||||
# Decide which values of lambda to use
|
||||
|
||||
nlambdas = 5
|
||||
lmbd_vals = np.logspace(-4, 0, nlambdas)
|
||||
MSERidgePredict = np.zeros(nlambdas)
|
||||
for i in range(nlambdas):
|
||||
lmb = lmbd_vals[i]
|
||||
RegRidge = linear_model.Ridge(lmb)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
ypredictRidge = RegRidge.predict(X_test)
|
||||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lmbd_vals), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
|
||||
plt.xlabel('log10(lambda)')
|
||||
plt.ylabel('MSE')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
# Neural Network part
|
||||
|
||||
n_hidden_neurons = 100
|
||||
epochs = 100
|
||||
# store models for later use
|
||||
eta_vals = np.logspace(-4, 0, 5)
|
||||
# store the models for later use
|
||||
DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||||
sns.set()
|
||||
for i, eta in enumerate(eta_vals):
|
||||
for j, lmbd in enumerate(lmbd_vals):
|
||||
dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
|
||||
alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
|
||||
dnn.fit(X_train, y_train)
|
||||
ypredictMLP = dnn.predict(X_test)
|
||||
test_accuracy[i][j] = MSE(ypredictMLP, y_test)
|
||||
|
||||
fig, ax = plt.subplots(figsize = (10, 10))
|
||||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||||
ax.set_title("Test Accuracy")
|
||||
ax.set_ylabel("$\eta$")
|
||||
ax.set_xlabel("$\lambda$")
|
||||
plt.show()
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
"""
|
||||
Code to test Ridge and NNs using Scikit-Learn only
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn import linear_model
|
||||
from sklearn.neural_network import MLPRegressor
|
||||
from sklearn.metrics import accuracy_score
|
||||
import seaborn as sns
|
||||
|
||||
|
||||
def MSE(y_data,y_model):
|
||||
n = np.size(y_model)
|
||||
return np.sum((y_data-y_model)**2)/n
|
||||
# A seed just to ensure that the random numbers are the same for every run.
|
||||
# Useful for eventual debugging.
|
||||
np.random.seed(315)
|
||||
|
||||
n = 100
|
||||
x = np.random.rand(n)
|
||||
y = x+x*x#np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
|
||||
|
||||
Maxpolydegree = 2
|
||||
X = np.zeros((n,Maxpolydegree-1))
|
||||
|
||||
for degree in range(1,Maxpolydegree): #No intercept column
|
||||
X[:,degree-1] = x**(degree)
|
||||
|
||||
# We split the data in test and training data
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||||
|
||||
# Decide which values of lambda to use
|
||||
|
||||
nlambdas = 10
|
||||
lmbd_vals = np.logspace(-4, 0, nlambdas)
|
||||
MSERidgePredict = np.zeros(nlambdas)
|
||||
for i in range(nlambdas):
|
||||
lmb = lmbd_vals[i]
|
||||
RegRidge = linear_model.Ridge(lmb)
|
||||
RegRidge.fit(X_train,y_train)
|
||||
ypredictRidge = RegRidge.predict(X_test)
|
||||
MSERidgePredict[i] = MSE(y_test,ypredictRidge)
|
||||
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lmbd_vals), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
|
||||
plt.xlabel('log10(lambda)')
|
||||
plt.ylabel('MSE')
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
# Neural Network part
|
||||
|
||||
n_hidden_neurons = 50
|
||||
epochs = 100
|
||||
# store models for later use
|
||||
eta_vals = np.logspace(-4, 0, 10)
|
||||
# store the models for later use
|
||||
DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||||
sns.set()
|
||||
for i, eta in enumerate(eta_vals):
|
||||
for j, lmbd in enumerate(lmbd_vals):
|
||||
dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
|
||||
alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
|
||||
dnn.fit(X_train, y_train)
|
||||
ypredictMLP = dnn.predict(X_test)
|
||||
test_accuracy[i][j] = MSE(ypredictMLP, y_test)
|
||||
|
||||
fig, ax = plt.subplots(figsize = (10, 10))
|
||||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||||
ax.set_title("Training Accuracy")
|
||||
ax.set_ylabel("$\eta$")
|
||||
ax.set_xlabel("$\lambda$")
|
||||
plt.show()
|
||||
|
||||
# Now we redefine our design matrix to include only the x-values and try out our NN
|
||||
|
||||
X = np.zeros((n,1))
|
||||
X[:,0] = x
|
||||
|
||||
# We split the data in test and training data again
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
||||
# Repeat the NN calculation
|
||||
DNN_scikit = np.zeros((len(eta_vals), len(lmbd_vals)), dtype=object)
|
||||
test_accuracy = np.zeros((len(eta_vals), len(lmbd_vals)))
|
||||
sns.set()
|
||||
for i, eta in enumerate(eta_vals):
|
||||
for j, lmbd in enumerate(lmbd_vals):
|
||||
dnn = MLPRegressor(hidden_layer_sizes=(n_hidden_neurons), activation='logistic',
|
||||
alpha=lmbd, learning_rate_init=eta, max_iter=epochs)
|
||||
dnn.fit(X_train, y_train)
|
||||
ypredictMLP = dnn.predict(X_test)
|
||||
test_accuracy[i][j] = MSE(ypredictMLP, y_test)
|
||||
|
||||
fig, ax = plt.subplots(figsize = (10, 10))
|
||||
sns.heatmap(test_accuracy, annot=True, ax=ax, cmap="viridis")
|
||||
ax.set_title("Training Accuracy")
|
||||
ax.set_ylabel("$\eta$")
|
||||
ax.set_xlabel("$\lambda$")
|
||||
plt.show()
|
||||
|
||||
@@ -40,6 +40,7 @@ from pydot import graph_from_dot_data
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.svm import SVC
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
from sklearn.model_selection import cross_validate
|
||||
|
||||
Reference in New Issue
Block a user