0

I have a function, where I calculate a prediction with the shape 1 x 250. I do this B times. To save all predictions for the B runs I would like to fill an empty B x 250 matrix row wise. Which python method is the best to fill an empty matrix, so that I can access the rows in a simple way (for example to subtract the rows from each other)

There must be something better then the following

indx = np.argsort(y_real[:,1])
print(len(indx)) #250

hat_y_b = []
for b in range(0,B):
      y_pred = function()
      hat_y_b = np.append(hat_y_b, y_pred[indx,1], axis=0)
amzon-ex
  • 1,645
  • 1
  • 6
  • 28
Suslik
  • 929
  • 8
  • 28

1 Answers1

2

I'm not sure I understand your question fully, but here're some general tips on dealing with numpy arrays:

In general, appending things to np arrays is very slow. Instead, allocate the entire array at the start, and fill with array indexing:

import numpy as np

# first, make an array with 250 rows and B cols.
# Each col will be a single prediction vector
all_b_arr = np.zeros((250, B))

# if no lower bound is specified, it is assumed to be 0
for idx in range(B):
    # This array indexing means "every row (:), column idx"
    all_b_arr[:, idx] = function()
David
  • 424
  • 3
  • 16