1

Just trying to add items to an array, but for some reason this returns without the added decimal.

import numpy as np
newposition = np.array([1,2,3])
np.append(newposition,(np.random.uniform(0,0.25)))
print newposition

Returns the following, ie without what I need to add. Any suggestions?

[1 2 3]
5
  • it creates a new array Commented Mar 10, 2015 at 21:15
  • Oh, then how do I add to the original array? Or how do I print the new array? Thanks! Commented Mar 10, 2015 at 21:20
  • 1
    You can't add to the original array. You could do something like newposition = np.append(newposition,(np.random.uniform(0,0.25))) Commented Mar 10, 2015 at 21:50
  • So I can't add a series of new numbers to an original array? I'll need to do this 1700 times. Commented Mar 10, 2015 at 21:54
  • @chicago.coder np.append is slow because it forces a new copy of the array to be created each time. The bigger the array is, the slower this will be, so appending 1700 times to a growing array is not a particularly good idea. A much better way is to create an empty array of the correct length (e.g. using np.empty), then fill in the appropriate rows as you go. Commented Mar 11, 2015 at 0:27

2 Answers 2

1

Got it. I'm going to create a list of new values, then convert it into an array, and assign the old array name to the new array.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks like it was supposed to be a comment to a different answer.
0

Don't get into the bad habit of using np.append to build arrays!

Appending to a numpy array is expensive since there is no way to do it without creating a new copy of the array in memory (the same is true of np.concatenate, np.vstack etc.). As the array gets bigger and bigger, copying it becomes slower and slower. A 1700-long 1D vector still isn't that big, but when you are dealing with millions of elements the copying will really hurt performance.

A much better way is to create an empty array with the correct final size, then fill in the appropriate indices as you go along. For example:

# create an empty array of the final size
newposition = np.empty(1700, np.float)

# fill in the first three values
newposition[:3] = 1, 2, 3

# fill in the rest
for ii in xrange(3, 1700):
    newposition[ii] = np.random.uniform(0, 0.25)

# or whatever...

You haven't shown exactly how you build the rest of your newposition array, but in the silly example above it would be much quicker to use the size= argument to np.random.uniform to fill in the rest of the rows in one go:

newposition[3:] = np.random.uniform(0, 0.25, size=1697)

1 Comment

Thanks @ ali_m! This would probably work, but I already decided to use my solution above -- essentially just create a new list of lists with the right data, then turn that into an array, and assign the old array name into the new array. But I'll keep your solution in mind in the future!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.