Home > Software design >  How to add values in an empty numpy array?
How to add values in an empty numpy array?

Time:10-19

I created an array using numpy.empty(). Now I want to add new values to that array based on the if-else condition. I wrote a for loop for that with the if-else condition inside. I only want to add values to the columns belonging to the first row. But after running the code I've written below, I'm getting some random values in the asr matrix instead of 1 and 0.

My code:

number_rows = 16
number_cols = 10
asr = np.empty((number_rows, number_cols))
 
for i in range(10): 
  if prediction[i] == check_keep[i]:
    asr[0][i] = 1 
  else:
    asr[0][i] = 0 
 
asr

I'd appreciate any help here.

CodePudding user response:

Use np.zeros or np.ones, np.empty initialises with random numbers

number_rows = 16
number_cols = 10
asr = np.zeros((number_rows, number_cols)) #np.zeros instead
 
for i in range(10): 
  if prediction[i] == check_keep[i]:
    asr.iloc[0, i] = 1 #also it is better to use np indexing here
  else:
    asr.iloc[0, i] = 0 
  • Related