Created
July 23, 2017 15:03
-
-
Save csetzkorn/843c2916364618b81e12b926e5b73c0f to your computer and use it in GitHub Desktop.
Based on this: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import pandas | |
from keras.models import Sequential | |
from keras.layers import Dense | |
from keras.wrappers.scikit_learn import KerasRegressor | |
from sklearn.model_selection import cross_val_score | |
from sklearn.model_selection import KFold | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.pipeline import Pipeline | |
# load dataset | |
dataframe = pandas.read_csv("D:/Data/BostonHousePrices.txt", delim_whitespace=True, header=None) | |
dataset = dataframe.values | |
#print(dataframe.head(5)) | |
# split into input (X) and output (Y) variables | |
X = dataset[:,0:13] | |
Y = dataset[:,13] | |
# define base model | |
def baseline_model(): | |
# create model | |
model = Sequential() | |
model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu')) | |
model.add(Dense(1, kernel_initializer='normal')) | |
# Compile model | |
model.compile(loss='mean_squared_error', optimizer='adam') | |
return model | |
# fix random seed for reproducibility | |
seed = 7 | |
numpy.random.seed(seed) | |
# evaluate model with standardized dataset | |
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100, batch_size=5, verbose=0) | |
kfold = KFold(n_splits=10, random_state=seed) | |
results = cross_val_score(estimator, X, Y, cv=kfold) | |
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std())) | |
#Results: 57.64 (42.37) MSE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment