ger

pdf

School

University of North Texas *

*We aren’t endorsed by this school

Course

123

Subject

Statistics

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by BailiffFlowerIbex39

Report
File - C:\Users\srima\PycharmProjects\ANN_diagnostics\main.py 1 # Import Statements 2 import numpy as np 3 from sklearn.preprocessing import StandardScaler, PolynomialFeatures 4 from sklearn.model_selection import train_test_split 5 from sklearn.metrics import mean_squared_error 6 import tensorflow as tf 7 from keras.models import Sequential 8 from keras.layers import Dense 9 from keras.optimizers import Adam 10 11 12 class ANN_Diagnoser: 13 def __init__(self): 14 # Loading the csv data using numpy module 15 self.data = np.loadtxt("data_w3_exl.csv", delimiter=',"' ) 16 self.x, self.y = self.cleaned_data() 17 self.x_train, self.y_train, \ 18 self.x_cv, self.y_cv, \ 19 self.x_test, self.y_test = self.split_data() 20 self.train_data, self.cv_data, self.test_data = self. data_mapping_scaling() 21 self.nn_models = self.build_models() 22 self.nn_train_mses = [] 23 self.nn_cv_mses = [] 24 25 def cleaned_data(self): 26 # Separating features(x) and targets(y) 27 x = self.datal[:, 0] 28 y = self.datal[:, 1] 29 # Changing 1-D arrays into 2D which helps in the code moving forward 30 X = np.expand_dims(x, axis=1) 31 y = np.expand_dims(y, axis=1) 32 return x, y 33 34 def split_data(self): 35 # training data = 60%, cross-verification data = 20% and testing data = 20% 36 x_train, x_, y_train, y_ = train_test_split(self.x, self.y, test_size=.40, random_state=1) 37 X_cv, x_test, y_cv, y_test = train_test_split(x_, y_, test_size=.50, random_state=1) 38 # Deleting temporary variables 39 del x_, y_ 40 return x_train, y_train, x_cv, y_cv, x_test, y_test 41 Page 1 of 4
File - C:\Users\srima\PycharmProjects\ANN_diagnostics\main.py 42 43 44 45 46 47 48 49 50 51 52 53 94 55 96 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 def data_mapping_scaling(self): # Mapping poly = PolynomialFeatures(1l, include_bias=False) x_train_mapped = poly.fit_transform(self.x_train) Xx_cv_mapped = poly.transform(self.x_cv) Xx_test_mapped = poly.transform(self.x_test) # Feature Scaling scaler = StandardScaler() x_train_mapped_scaled = scaler.fit_transform( X_train_mapped) X_cv_mapped_scaled = scaler.transform(x_cv_mapped) x_test_mapped_scaled = scaler.transform(x_test_mapped) return x_train_mapped_scaled, Xx_cv_mapped_scaled, x_test_mapped_scaled @staticmethod def build_models(): tf.random.set_seed(20) model_1 = Sequential( [ Dense(25, activation='relu'), Dense(15, activation='relu'), Dense(1, activation='linear') 1, name="'model_1" ) model_2 = Sequential( [ Dense(20, activation='relu'), Dense(12, activation='relu'), Dense(12, activation='relu'), Dense(20, activation='relu'), Dense(1, activation='linear') 1, name="'model_2' ) model_3 = Sequential( [ Dense(32, activation='relu'), Dense(16, activation='relu'), Dense(8, activation='relu'), Dense(4, activation='relu'), Dense(12, activation='relu'), Dense(1, activation='linear') Page 2 of 4
File - C:\Users\srima\PycharmProjects\ANN_diagnostics\main.py 88 89 90 921 92 93 924 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 151 1, name="'model_3" ) return model_1, model_2, model_3 def run_models(self): # Running 3 models for model in self.nn_models: model.compile( loss="mse"', optimizer=Adam(learning_rate=0.1) ) model.fit( self.train_data, self.y_train, epochs=300, verbose=0 ) # Record the training MSEs vhat = model.predict(self.train_data) train_mse = mean_squared_error(self.y_train, yhat ) /2 self.nn_train_mses.append(train_mse) # Record the CV MSEs vhat_cv = model.predict(self.cv_data) cv_mse = mean_squared_error(self.y_cv, yhat_cv) / 2 self.nn_cv_mses.append(cv_mse) print ("RESULTS:") for model_num in range(len(self.nn_train_mses)): print( "Model {}: Train MSE: {} and CV MSE: {}". format( model_num + 1, self.nn_train_mses[ model_num], self.nn_cv_mses[model_num] ) ) # RESULTS: # Model 1: Training MSE: 73.44, CV MSE: 113.87 # Model 2: Training MSE: 73.40, CV MSE: 112.28 # Model 3: Training MSE: 44.56, CV MSE: 88.51 # Model 3 is selected Page 3 of 4
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
File - C:\Users\srima\PycharmProjects\ANN_diagnostics\main.py 132 def final_test(self, model_num): 133 vyhat_test = self.nn_models[model_num - 1].predict(self .test_data) 134 test_mse = mean_squared_error(self.y_test, yhat_test ) /2 135 136 print("Selected Model: {}".format(model_num)) 137 print("Training MSE: {}".format(self.nn_train_mses[ model_num - 1])) 138 print("Cross Validation MSE: {}".format(self. nn_cv_mses[model_num - 1])) 139 print("Test MSE: {}".format(test_mse)) 140 141 # RESULTS: 142 # Selected Model: 3 143 # Training MSE: 44.56 144 # Cross Validation MSE: 88.51 145 # Test MSE: 87.77 146 147 148 # Instance of class ANN_Diagnoser 149 diagnostor = ANN_Diagnoser() 150 diagnostor.run_models() 151 diagnostor.final_test(model_num=3) 152 Page 4 of 4