title: How to fix nan values coming from the implementation of Logistic Regression?
body: <p>After I get some processes to get x_train and y_train, I flatten them. The code snippets are shown below.</p>
  <p><strong>The flatten code</strong></p>
  <pre><code>x_train = x_train_flatten.T
  x_test = x_test_flatten.T
  y_test = Y_test.T
  y_train = Y_train.T
  print(&quot;x train: &quot;,x_train.shape)
  print(&quot;x test: &quot;,x_test.shape)
  print(&quot;y train: &quot;,y_train.shape)
  print(&quot;y test: &quot;,y_test.shape)
  </code></pre>
  <p><strong>Result</strong></p>
  <pre><code>x train:  (16384, 38)
  x test:  (16384, 10)
  y train:  (1, 38)
  y test:  (1, 10)
  </code></pre>
  <p><strong>Normalization Process(AfterFlatten)</strong> :</p>
  <pre><code>from sklearn.impute import SimpleImputer
  imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
  imputer = imputer.fit(x_train)
  x_train= imputer.transform(x_train)
  x_train = (x_train-np.min(x_train))/(np.max(x_train)-np.min(x_train))
  </code></pre>
  <p>Then I wrote some methods for <strong>Logistic Regression</strong></p>
  <p><strong>Logistic Regression Methods</strong></p>
  <pre><code>def initialize_weights_and_bias(dimension):
      w = np.full((dimension,1),0.01)
      b = 0.0
      return w, b
  
  def sigmoid(z):
      y_head = 1/(1+np.exp(-z))
      return y_head
  
  def forward_backward_propagation(w,b,x_train,y_train):
      # forward propagation
      z = np.dot(w.T,x_train) + b    
      y_head = sigmoid(z)    
      loss = -(1-y_train)*np.log(1-y_head)-y_train*np.log(y_head)        
      cost = (np.sum(loss))/x_train.shape[1]  # x_train.shape[1]  is for scaling
      
      # backward propagation
      derivative_weight = (np.dot(x_train,((y_head-y_train).T)))/x_train.shape[1] # x_train.shape[1]  is for scaling
      derivative_bias = np.sum(y_head-y_train)/x_train.shape[1]                   # x_train.shape[1]  is for scaling
      gradients = {&quot;derivative_weight&quot;: derivative_weight,&quot;derivative_bias&quot;: derivative_bias}
      return cost,gradients
  
  def update(w, b, x_train, y_train, learning_rate,number_of_iterarion):
      cost_list = []
      cost_list2 = []
      index = []
      # updating(learning) parameters is number_of_iterarion times
      for i in range(number_of_iterarion):
          # make forward and backward propagation and find cost and gradients
          cost,gradients = forward_backward_propagation(w,b,x_train,y_train)
          cost_list.append(cost)
          # lets update
          w = w - learning_rate * gradients[&quot;derivative_weight&quot;]
          b = b - learning_rate * gradients[&quot;derivative_bias&quot;]
          if i % 50 == 0:
              cost_list2.append(cost)
              index.append(i)
              print (&quot;Cost after iteration %i: %f&quot; %(i, cost))
      # we update(learn) parameters weights and bias
      parameters = {&quot;weight&quot;: w,&quot;bias&quot;: b}
      plt.plot(index,cost_list2)
      plt.xticks(index,rotation='vertical')
      plt.xlabel(&quot;Number of Iterarion&quot;)
      plt.ylabel(&quot;Cost&quot;)
      plt.show()
      return parameters, gradients, cost_list
  
  def predict(w,b,x_test):
      # x_test is a input for forward propagation
      z = sigmoid(np.dot(w.T,x_test)+b)
      Y_prediction = np.zeros((1,x_test.shape[1]))
      # if z is bigger than 0.5, our prediction is woman (y_head=1),
      # if z is smaller than 0.5, our prediction is man (y_head=0),
      for i in range(z.shape[1]):
          if z[0,i]&lt;= 0.5:
              Y_prediction[0,i] = 0
          else:
              Y_prediction[0,i] = 1
      return Y_prediction
  
  def logistic_regression(x_train, y_train, x_test, y_test, learning_rate ,  num_iterations):
      # initialize
      dimension =  x_train.shape[0] 
      w,b = initialize_weights_and_bias(dimension)
      parameters, gradients, cost_list = update(w, b, x_train, y_train, learning_rate,num_iterations)
      
      y_prediction_test = predict(parameters[&quot;weight&quot;],parameters[&quot;bias&quot;],x_test)
      y_prediction_train = predict(parameters[&quot;weight&quot;],parameters[&quot;bias&quot;],x_train)
  
      train_acc_lr = round((100 - np.mean(np.abs(y_prediction_train - y_train)) * 100),2)
      test_acc_lr = round((100 - np.mean(np.abs(y_prediction_test - y_test)) * 100),2)
      # Print train/test Errors
      print(&quot;train accuracy: %&quot;, train_acc_lr)
      print(&quot;test accuracy: %&quot;, test_acc_lr)
  </code></pre>
  <p>Then I start to call logistic_regression method to implement Logistic Regression.</p>
  <pre><code>logistic_regression(x_train, y_train, x_test, y_test,learning_rate = 0.01, num_iterations = 700)
  </code></pre>
  <p>After showing some cost results, some of them has nan values as shown below.</p>
  <pre><code>Cost after iteration 0: nan
  Cost after iteration 50: 10.033753
  Cost after iteration 100: 11.253421
  Cost after iteration 150: nan
  Cost after iteration 200: nan
  Cost after iteration 250: nan
  Cost after iteration 300: nan
  Cost after iteration 350: nan
  Cost after iteration 400: nan
  Cost after iteration 450: 0.321755
  ...
  </code></pre>
  <p>How can I fix the issue?</p>