ler

pdf

School

University of North Texas *

*We aren’t endorsed by this school

Course

123

Subject

Industrial Engineering

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by BailiffFlowerIbex39

Report
File - C:\Users\srima\PycharmProjects\Logistic_Regression_Diagnostics\main.py 1 # Import Statements 2 import numpy as np 3 from sklearn.preprocessing import StandardScaler 4 from sklearn.model_selection import train_test_split 5 import tensorflow as tf 6 import matplotlib.pyplot as plt 7 import matplotlib.lines as mlines 8 from keras.models import Sequential 9 from keras.layers import Dense 10 from keras.optimizers import Adam 11 from keras.losses import BinaryCrossentropy 12 13 14 class Classification_ANN_Diagnoser: 15 def __init__(self): 16 self.data = np.loadtxt('data_w3_ex2.csv', delimiter=',"' ) 17 self.x, self.y = self.divide_x_y() 18 self.x_train, self.y_train, \ 19 self.x_cv, self.y_cv, \ 20 self.x_test, self.y_test = self.split_data() 21 self.x_train_scaled, self.x_cv_scaled, self. X_test_scaled = self.feature_scaling() 22 self.threshold = 0.5 23 self.models = self.build_models() 24 self.nn_train_error = [] 25 self.nn_cv_error = [] 26 27 def divide_x_y(self): 28 # Dividing features and targets 29 X = self.datal:, :-1] 30 y = self.datal[:, 2:3] 31 return X, Yy 32 33 def split_data(self): 34 # Splitting data into 3 sets 35 x_train, x_, y_train, y_ = train_test_split(self.x, self.y, test_size=0.40, random_state=1) 36 X_cv, X_test, y_cv, y_test = train_test_split(x_, y_, test_size=0.50, random_state=1) 37 # Delete x_, y_ 38 del x_, y_ 39 40 return x_train, y_train, x_cv, y_cv, x_test, y_test 41 42 def feature_scaling(self): 43 scaler_linear = StandardScaler() 44 X_train_scaled = scaler_linear.fit_transform(self. Page 1 of 4
File - C:\Users\srima\PycharmProjects\Logistic_Regression_Diagnostics\main.py 44 x_train) 45 Xx_cv_scaled = scaler_linear.transform(self.x_cv) 46 x_test_scaled = scaler_linear.transform(self.x_test) 47 return x_train_scaled, x_cv_scaled, x_test_scaled 48 49 def plot_dataset(self, title): 50 for i in range(len(self.y)): 51 marker = 'x' if self.y[i] == 1 else 'o' 52 c = 'r' if self.y[i] == 1 else 'b' 53 plt.scatter(self.x[i, 0], self.x[i, 1], marker= marker, c=c) 54 plt.title("x1 vs x2") 55 plt.xlabel("x1") 56 plt.ylabel("x2") 57 y_0 = mlines.Line2D([], []1, color='r', marker='x', markersize=12, linestyle='None', label='y=1"') 58 y_1 = mlines.Line2D([], [], color='b', marker='o', markersize=12, linestyle='None', label='y=0') 59 plt.title(title) 60 plt.legend(handles=[y_0, y_11) 61 plt.show() 62 63 @staticmethod 64 def build_models(): 65 tf.random.set_seed(20) 66 model_1 = Sequential( 67 [ 68 Dense(25, activation='relu'), 69 Dense(15, activation='relu'), 70 Dense(1, activation='sigmoid"') 71 1, 72 name="model_1' 73 ) 74 75 model_2 = Sequential( 76 [ 77 Dense(20, activation='relu'), 78 Dense(12, activation='relu'), 79 Dense(12, activation='relu'), 80 Dense(20, activation='relu'), 81 Dense(1, activation='sigmoid"') 82 1, 83 name="'model_2' 84 ) 85 86 model_3 = Sequential( 87 [ 88 Dense(32, activation='relu'), Page 2 of 4
File - C:\Users\srima\PycharmProjects\Logistic_Regression_Diagnostics\main.py 89 90 91 92 93 94 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 131 132 133 134 135 ) Dense(16, activation='relu'), Dense(8, activation='relu'), Dense(4, activation='relu'), Dense(12, activation='relu'), Dense(1, activation='sigmoid') ]I name='model_3' models = [model_1, model_2, model_3] return models def run_ for for diagnostics(self): model in self.models: model.compile( loss=BinaryCrossentropy(from_logits=True), optimizer=Adam(learning_rate=0.01), ) print("Training {}...".format(model.name)) model.fit( self.x_train_scaled, self.y_train, epochs=200, verbose=0 ) print("Done!\n") vhat = model.predict(self.x_train_scaled) vyhat = tf.math.sigmoid(yhat) vhat = np.where(yhat >= self.threshold, 1, 0) train_error = np.mean(yhat != self.y_train) print(train_error) self.nn_train_error.append(train_error) vyhat = model.predict(self.x_cv_scaled) vhat = tf.math.sigmoid(yhat) vhat = np.where(yhat >= self.threshold, 1, 0) cv_error = np.mean(yhat != self.y_cv) print(cv_error) self.nn_cv_error.append(cv_error) model_num in range(len(self.nn_train_error)): print( "Model {}: Training Set Classification Error - {}\n".format( 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\Logistic_Regression_Diagnostics\main.py 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 model_num] model_num + 1, self.nn_train_error[ ) ) print( "CV Set Classification Error: {}".format( self.nn_cv_error[model_num] ) ) # Results: Train Test # Model 1: 0.05833, 0.17500 # Model 2: 0.06667, 0.15000 # Model 3: 0.05000, 0.15000 <----Selected def final_testing(self, model_num): # Compute the test error vyhat = self.models[model_num - 1].predict(self. X_test_scaled) vyhat = tf.math.sigmoid(yhat) vhat = np.where(yhat >= self.threshold, 1, 0) nn_test_error = np.mean(yhat != self.y_test) print(f"Selected Model: {model_num}") print(f"Training Set Classification Error: {self. nn_train_error[model_num - 1]}") print(f"CV Set Classification Error: {self.nn_cv_error [model_num - 11}") print(f"Test Set Classification Error: {nn_test_error} ") # Results: Train cVv Test # Model 3: 0.0500, 0.1500, 0.1750 diagnoser = Classification_ANN_Diagnoser() diagnoser.plot_dataset("x1 Vs x2") diagnoser.run_diagnostics() # Select the model with the lowest error diagnoser.final_testing(model_num=3) Page 4 of 4