Reputation: 9579
I have a method that returns a numpy array. What I want to do is in each iteration take this array and concatenate it into another array of arrays so that at the end of the loop I have a matrix
This is my code so far, but its crashing the program at the given 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 this
[ 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 like should look something like this after 2 iterations. Also I do not know in advance the number of iterations the lop would go through.So any number of arrays could be produced after the method runs. But either way all arrays should form the 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
Upvotes: 4
Views: 7703
Reputation: 25833
I have a project were I need to do something very similar. You can implement dynamic resizing in your loop, but python's list type is actually implemented as a dynamic array so you might as well take advantage of the tools that are already available to you. 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)
I should mention that if you already know the size you expect for theta_Matrix
, you'll get the best 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
Upvotes: 3
Reputation: 363817
np.append
returns the concatenated array, and you're ignoring its return value. You should also consider using np.vstack
instead, since that stacks row vectors into matrices (append
can do it but it takes extra arguments).
However, running np.append
or np.vstack
in a loop is still not a very good idea as constructing the matrix will take quadratic time. It's better to preallocate an array and then fill it row by row using slicing. If you don't know how large it will need to be, consider iteratively doubling its size (see Wikipedia, Dynamic array) with np.resize
.
Upvotes: 2