#! /usr/bin/env python3 # def hourly_wages ( ): #*****************************************************************************80 # ## hourly_wages uses keras to solve a multivariate regression case. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 29 April 2019 # # Author: # # John Burkardt. # import keras import matplotlib.pyplot as plt import numpy as np import platform print ( '' ) print ( 'hourly_wages:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Neural network to solve a multivariate regression problem.' ) print ( ' The data is read from an external file.' ) print ( '' ) # # Load the data. # data = np.loadtxt ( 'hourly_wages.csv', delimiter = ',', skiprows = 1 ) print ( ' Data contains %d records with %d features.' % ( data.shape[0], data.shape[1] ) ) # # Data column 1 is the wage, which will be our target. # Data columns 2-10 are features. # Extract 450 records as training, 84 as test. # train_data = data[0:450,1:10] train_targets = data[0:450,0] train_num = train_data.shape[0] feature_num = train_data.shape[1] print ( ' Training data uses %d records with %d features and 1 target.' % ( train_num, feature_num ) ) test_data = data[450:534,1:10] test_targets = data[450:534,0] test_num = test_data.shape[0] print ( ' Test data uses %d records with %d features and 1 target.' % ( test_num, feature_num) ) # # Exhibit the contents of one item of the training data: # print ( ' train_data[0,0:9]:' ) print ( train_data[0,0:9] ) print ( ' train_targets[0]:' ) print ( train_targets[0] ) # # Build the model. # from keras import models from keras import layers model = models.Sequential() model.add ( layers.Dense ( 15, activation = 'relu', input_shape = ( feature_num, ) ) ) model.add ( layers.Dense ( 15, activation = 'relu' ) ) model.add ( layers.Dense ( 1 ) ) # # Compile the model. # model.compile ( optimizer = 'adam', loss = 'mean_squared_error' ) # # Allow for early stopping if training is not improving the estimate. # from keras.callbacks import EarlyStopping early_stopping_monitor = EarlyStopping ( patience = 3 ) # # Train the model. # print ( '' ) print ( 'Training:' ) print ( '' ) model.fit ( train_data, train_targets, validation_split = 0.2, epochs = 30, \ callbacks = [ early_stopping_monitor ] ) # # Test # print ( '' ) print ( 'Testing:' ) print ( '' ) test_predictions = model.predict ( test_data ) for i in range ( test_num ): print ( ' %d: %f %f' % ( i, test_predictions[i], test_targets[i] ) ) # # Terminate. # print ( '' ) print ( 'hourly_wages:' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) hourly_wages ( ) timestamp ( )