This article mainly introduces the implementation of the TensorFlow to implement the logical regression algorithm, has a certain reference value, now share to everyone, the need for friends can refer to
This paper will implement the logistic regression algorithm to predict the probability of low birth weight.
# logistic regression# Logistic regression #----------------------------------# # This function shows how to use TensorFlow to# solve Logis Tic regression.# y = sigmoid (Ax + b) # # We'll use the low birth weight data, specifically:# y = 0 or 1 = low birth weight # x = Demographic and medical history DataImport Matplotlib.pyplot as Pltimport numpy as Npimport TensorFlow as Tfimport R Equestsfrom tensorflow.python.framework Import opsimport os.pathimport csvops.reset_default_graph () # Create Graphsess = TF. Session () # # # # Obtain and prepare data for modeling#### name of data Filebirth_weight_file = ' birth_weight.csv ' # download D ATA and create data file if file does not exist in current directoryif not os.path.exists (birth_weight_file): Birthdata_u RL = ' https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/ Birthweight_data/birthweight.dat ' birth_file = Requests.get (birthdata_url) birth_data = Birth_file.text.split (' \ r \ n ' ) Birth_header = Birth_data[0].split (' \ t ') birth_data = [[Float (x) for x in Y.split (' \ t ') if Len (x) >=1] for y in birth_data[1:] If Len (y) >=1] with open (Birth_weight_file, "w") as F:writer = Csv.writer (f) writer.writerow (Birth_header) writer.writerows (birth_data) F.close () # Read birth weight data into memorybirth_data = []with open (Birth_weight_file, newline= ") as Csvfile:csv_ Reader = Csv.reader (csvfile) Birth_header = Next (Csv_reader) for row in Csv_reader:birth_data.append (row) Birth_da Ta = [[Float (x) for x in row] for row in birth_data]# pull out target variabley_vals = Np.array ([x[0] for x in Birth_data] # Pull out predictor variables (not ID, not target, and not birthweight) x_vals = Np.array ([X[1:8] for x in Birth_data]) # Set for reproducible resultsseed = 99np.random.seed (seed) Tf.set_random_seed (SEED) # split data into train/test = 80%/20%# points Cut datasets for test sets and training sets Train_indices = Np.random.choice (len (x_vals), round (Len (x_vals) *0.8), replace=false) test_indices = Np.array (List (set (Len (x_vals)))-Set (train_indices)) X_vals_train = X_vals[train_indices]x_vals_test = X_vals[test_indices]y_vals_train = y_vals[ Train_indices]y_vals_test = y_vals[test_indices]# Normalize by column (Min-max norm) # scales all features to 0 and 1 intervals (Min-max scale), The effect of logical regression convergence is better # normalized feature def normalize_cols (m): Col_max = M.max (axis=0) col_min = M.min (axis=0) return (m-col_min)/(Col_max -col_min) X_vals_train = Np.nan_to_num (Normalize_cols (x_vals_train)) X_vals_test = Np.nan_to_num (Normalize_cols (x_ vals_test) # # Define TensorFlow computational graph¶#### Declare Batch sizebatch_size = 25# Initialize Placeholdersx_da Ta = Tf.placeholder (Shape=[none, 7], dtype=tf.float32) Y_target = Tf.placeholder (Shape=[none, 1], Dtype=tf.float32) # Create variables for linear Regressiona = TF. Variable (Tf.random_normal (shape=[7,1])) b = tf. Variable (Tf.random_normal (shape=[1,1)) # Declare Model operationsmodel_output = Tf.add (Tf.matmul (X_data, A), b) # Declare loss function (cross Entropy loss) loss = Tf.reduce_mean (tf.nn.sigmoid_cross_entropy_with_logits (Logits=model_output, Labels=y_target)) # Declare optimizermy_opt = Tf.train.GradientDescentOptimizer (0.01) Train_step = my_opt.minimize (loss) # # train model#### Initialize variablesinit = Tf.global_variables_initializer () Sess.run (init) # Actual prediction# In addition to recording the loss function, it is also necessary to record the accuracy of the classifier on the training set and the test set. # so create a predictive function that returns accuracy prediction = Tf.round (tf.sigmoid (model_output)) Predictions_correct = Tf.cast (tf.equal (prediction, Y_target), tf.float32) accuracy = Tf.reduce_mean (predictions_correct) # Training loop# start traversing iteration training, recording loss values and accuracy Loss_vec = [] TRAIN_ACC = []TEST_ACC = []for i in range]: Rand_index = Np.random.choice (len (x_vals_train), size=batch_size) Rand_ x = X_vals_train[rand_index] rand_y = Np.transpose ([Y_vals_train[rand_index]]) sess.run (Train_step, Feed_dict={x_data : rand_x, y_target:rand_y}) Temp_loss = Sess.run (loss, feed_dict={x_data:rand_x, y_target:rand_y}) loss_vec.append (Te Mp_loss) Temp_acc_train = sess.run (accuracy, Feed_dict={x_data:x_vals_train, y_target:np.transpose ([y_vals_tRain])}) train_acc.append (temp_acc_train) temp_acc_test = sess.run (accuracy, feed_dict={x_data:x_vals_test, y_target : Np.transpose ([y_vals_test])}) test_acc.append (temp_acc_test) if (i+1)%300==0:print (' Loss = ' + str (temp_loss)) # # # # # # # # # # # # Display model performance#### Draw loss and accuracy Plt.plot (Loss_vec, ' K ') Plt.title (' Cross Entropy loss per Generation ') Plt.xlabel (' Generation ') Plt.ylabel (' Cross Entropy Loss ') plt.show () # Plot train and Test Accuracyplt.plot (TRAIN_ACC, ' K ', label= ' Train set accuracy ') Plt.plot (TEST_ACC, ' r--', label= ' test Set accuracy ') plt.title (' Train and test Accuracy ') Plt.xlabel ( ' Generation ') plt.ylabel (' accuracy ') plt.legend (loc= ' lower right ') plt.show ()
Data results:
Loss = 0.845124
Loss = 0.658061
Loss = 0.471852
Loss = 0.643469
Loss = 0.672077
Iterative 1500-time cross-entropy loss graph
An accuracy graph of 1500 iterations of the test set and training set