Creating an array of numpy arrays during cyclization (Python)

I have a method that returns a numpy array. I want each iteration to take this array and merge it into another array of arrays, so at the end of the loop I have a matrix

This is my code so far, but its program crash on this line

delta_Array = np.array([0.01,0.02,0.03, 0.04, 0.05, 0.06,0.07, 0.08, 0.09, 0.10]) theta_Matrix = np.zeros(8) t = Ridge(Xtrain, ytrain, .3) # This method returns an array of 8 elements np.append(theta_Matrix,t, axis=0) print delta_Array print theta_Matrix 

With this method, My current output is

  [ 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 ] # delta_Array [ 0. 0. 0. 0. 0. 0. 0. 0.] # theta_Matrix 

The output id should look something like this after two iterations. In addition, I do not know in advance the number of iterations that the lop will go through. Thus, after starting the method, any number of arrays can be created. But in any case, all arrays should form a theta matrix:

  theta_Matrix: [[ 0.65 0.65565 0.19181 0.923 0.51561 0.846 0.6464 0.6464] [ 0.9879 0.31213 0.68464 0.611 0.6161 0.61651 0.29858 0.1811]] 

thanks

+4
source share
2 answers

I have a project, I need to do something very similar. You can implement dynamic resizing in your loop, but the python list type is actually implemented as a dynamic array, so you can also use the existing tools. You can do something like this:

 delta_Array = np.array([0.01,0.02,0.03, 0.04, 0.05, 0.06,0.07, 0.08, 0.09, 0.10]) theta_Matrix = [] for i in range(N): t = Ridge(Xtrain, ytrain, .3) theta_Matrix.append(t) theta_Matrix = np.array(theta_Matrix) 

It should be noted that if you already know the size expected for theta_Matrix , you will get maximum performance by doing something like:

 delta_Array = np.array([0.01,0.02,0.03, 0.04, 0.05, 0.06,0.07, 0.08, 0.09, 0.10]) theta_Matrix = np.zeros((N, 8)) for i in range(N): t = Ridge(Xtrain, ytrain, .3) theta_Matrix[i] = t 
+3
source

np.append returns a concatenated array, and you ignore its return value. You should also consider using np.vstack instead, as this stacks row vectors into matrices ( append can do this, but requires additional arguments).

However, running np.append or np.vstack in a loop is still not a good idea, since building a matrix will take quadratic time. It is better to pre-distribute the array, and then fill it in a row using slicing. If you don't know how necessary this is, consider iteratively doubling its size (see Wikipedia, Dynamic Array ) using np.resize .

+2
source

Source: https://habr.com/ru/post/1446677/


All Articles