1

I'm trying to take an array on numpy, add a line of code, append it to the array, and then reshape the entire array back to the translated r. While I'm attempting to use plt.imshow to display it again, it keeps saying that the "total size of new array must be unchanged". Is there a better way to get around this?

z = np.array([[[23] * ncols]*nrows]).reshape(nrows, ncols)

for j in range(0,1):
  for i in range(0,ncols):
    np.append(z, [23])

np.reshape(z, nrows+1,ncols)

plt.imshow(z, interpolation='none', cmap=cm.gist_rainbow)

In addition, what if I iterate j from nrows to ncols? Are there specific edits that would need to happen other than just multiplying both by nrows and ncols?

EDIT: I tried reshaping with

np.reshape(z, nrows+2, ncols)

which works; however, when doing it with a range from 0 to 100 to check differences, the program takes forever and doesn't actually display new values. Are there better ways to deal with this?

1 Answer 1

1

As I understand it, you have a numpy array and you want to add two rows.

Let's start by creating our array, called a:

In [61]: a = np.arange(12); a.resize(3, 4); print a
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Now, let's create the new rows that we want to add to a:

In [62]: newrow1 = np.array([100, 101, 102, 103])

In [63]: newrow2 = np.array([200, 201, 202, 203])

Now, let's add those rows:

In [64]: np.vstack((a, newrow1, newrow2))
Out[64]: 
array([[  0,   1,   2,   3],
       [  4,   5,   6,   7],
       [  8,   9,  10,  11],
       [100, 101, 102, 103],
       [200, 201, 202, 203]])

Notes

Let's consider this line of code:

z = np.array([[[23] * ncols]*nrows]).reshape(nrows, ncols)

Let's try two variations:

In [74]: nrows=3; ncols=4; np.array([[[23] * ncols]*nrows])
Out[74]: 
array([[[23, 23, 23, 23],
        [23, 23, 23, 23],
        [23, 23, 23, 23]]])

In [75]: nrows=3; ncols=4; np.array([[[23] * ncols]*nrows]).reshape(nrows, ncols)
Out[75]: 
array([[23, 23, 23, 23],
       [23, 23, 23, 23],
       [23, 23, 23, 23]])

We can conclude that the call to reshape is unnecessary because the array, as created, already has the specified number of rows and columns.

Now, let's consider this loop:

for j in range(0,1):
   for i in range(0,ncols):
      np.append(z, [23])

np.append does not change the array in place; it returns a new array. Consequently, this loop does nothing.

Let's consider reshape:

np.reshape(z, nrows+1,ncols)

As you noted, with reshape, the "total size of new array must be unchanged". The solution is to use resize. Here, we demonstrate resize on array a:

In [86]: a
Out[86]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [91]: a = np.resize(a, (5,4)); print a
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [ 0  1  2  3]
 [ 4  5  6  7]]

If you want all of the last two rows to have the value 23, then it can be simply assigned:

In [94]: a[3:, :] = 23; print a
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [23 23 23 23]
 [23 23 23 23]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.