4

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

Fred Foo
365k80 gold badges765 silver badges851 bronze badges
asked Nov 17, 2012 at 17:51

2 Answers 2

3

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
answered Nov 17, 2012 at 18:09

Comments

2

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.

answered Nov 17, 2012 at 17:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.