Trying to predict, prediction value greater than 0.5 it will be positive prediction (1) , under 0.5 value as 0 ,negative , but having problem in loop

Issue

After training the model , evaluating prediction that is on dtype=float32.

y_pred = model.predict(x_test)
y_pred

array([[0.952564  ],
       [0.40119413],
       [0.8223132 ],
       ...,
       [0.03289893],
       [0.16677496],
       [0.882395  ]], dtype=float32)

result = model.evaluate(np.asarray(x_test), np.asarray(y_test))  
loss = result[0]
accuracy = result[1]
print(f"[+] Accuracy: {accuracy*100:.2f}%")

so , for this i have float32 but 1 for positive & 0 for neg, for this i have some issue , so i am trying to do, make it float32 to int32 & a loop that if value is greater than 0.5 then it will count as 1 & if less than 0.5 then count as 0 means neg.
the loop i tried :

y_pred = model.predict(x_test)
y_pred ( for i in range(len(y_pred)):
  if y_pred[i][0] >= 0.5:
    y_pred[i][0] = int(1)
  else:
    y_pred[i][0] = 0



print(y_pred[0]) )

error is : invalid syntax .

can anyone help to sort out this one ?

Solution

Seems like you should use y_pred[0][i]. The array looks to be 2d with only one sub array, so [0][i] should be used. Also, if the code is exactly what is written, i think

y_pred ( for i in range(len(y_pred)):

should be

for i in range(len(y_pred)):

Answered By – Mercury

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published