how to append a array to a np.array? - arrays

coordinates = np.empty([0,5])
np.vstack( (coordinates, np.array([1, 2, 3, 4, 5]) ))
print coordinates # []
np.append(coordinates, np.array([1, 2, 3, 4, 5]), axis=0)
print coordinates
In the code shown above, I tried to append the array, but both approaches failed. In the first approach, the output is still empty, in the second approach, the output is an error saying
ValueError: all the input arrays must have same number of dimensions
What is wrong with my method?

You need to capture the results of numpy.vstack()
From the (Docs)
numpy.vstack(arrays, axis=0)
Returns:
stacked : ndarray
Test Code:
coordinates = np.empty([0, 5])
x = np.vstack((coordinates, np.array([1, 2, 3, 4, 5])))
print x
Results:
[[ 1. 2. 3. 4. 5.]]

Related

Doubling Numpy array into bigger array

Is there a way without creating a 2nd variable storing array [1,2,3] and then concatenating
to get [1,2,3,1,2,3] from array1 = [1,2,3].
Could I use numpy.repeat for this?
Input:
[2,3,4]
Output:
[2,3,4,2,3,4]
You can use numpy.tile:
>>> np.tile([1,2,3], 2)
array([1, 2, 3, 1, 2, 3])

Python change the array's dimension from (n,1) for (n,)

If I declare an array "v" whose shape is (3,100) when I want to change its values column by column making use a "for" python changes the dimension of "v[:,i]" for (3,) this is annoying and I can't make the change because at the left member it has a (3,) array and in the right, it has an (3,1) array.
I would like to know, why does this happen? and which are my options to cope with this?
Thanks.
v = np.ones( (3, 100) );
for i in range( 0 , 100 ):
v[:,i] = np.array([[1],
[2],
[3]])
ValueError: could not broadcast input array from shape (3,1) into shape (3)
In [379]: M = np.arange(12).reshape(3,4)
Indexing with a scalar reduced the dimension by one. That's a basic rule of indexing - in numpy and python.
In [380]: M[0,:]
Out[380]: array([0, 1, 2, 3])
In [381]: M[:,0]
Out[381]: array([0, 4, 8])
Same for a list:
In [383]: M.tolist()
Out[383]: [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
In [384]: M.tolist()[0]
Out[384]: [0, 1, 2, 3]
Index with a list/array or slice, does preserve the dimension:
In [385]: M[:,[0]]
Out[385]:
array([[0],
[4],
[8]])
So assigning a (3,) to the (3,) slot is fine:
In [386]: M[:,0] = [10,20,30]
Assigning a (3,1) to that slot produces an error:
In [387]: M[:,0] = [[10],[20],[30]]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-387-1bbfa6dfa93c> in <module>
----> 1 M[:,0] = [[10],[20],[30]]
ValueError: setting an array element with a sequence.
In [388]: M[:,0] = np.array([[10],[20],[30]]) # or with an array
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-388-6e511ffdc44e> in <module>
----> 1 M[:,0] = np.array([[10],[20],[30]])
ValueError: could not broadcast input array from shape (3,1) into shape (3)
By broadcasting (3,) can go into (1,3), but not (3,1) into (3,). One solution is to flatten the RHS:
In [389]: M[:,0] = np.array([[10],[20],[30]]).ravel()
Assigning to a (3,1) slot also works:
In [390]: M[:,[0]] = np.array([[10],[20],[30]])
In [391]: M[:,0:1] = np.array([[10],[20],[30]])
We could also transpose the (3,1) to (1,3). Or assign to M[:,0][:,None] or M[:,0,None] (both of which create a (3,1)).
What I think you are asking is: how to set them column wise.
v = np.ones( (3,100) )
for i in range( 0 , 100 ):
v[:,i] = np.array([1,
3,
2])
The change is to remove extra brackets in your assignment.
If you are trying to do something else, you can try doing it for rows, and flip array sideways:
v = np.ones((100,3))
for i in range(0,100):
v[i] = np.array([1,3,2])
EDIT: changed the whitespace to be the same as the author

Loop to perform operation on i+1 in numpy array

I have a numpy array, I'd like to take the 3 numbers in each row, minus them from the next row and store those values in another array.
something like
for i in array:
a = i - i+1
I know this is very wrong, but at least this gives the idea of what I want.
Obviously i+1 will just result in the value + 1 and then all I have is a = 1,1,1
When I say i+1 I mean the next in line.
So for example:
input = np.array([[4,4,5], [2,3,1],[1,2,0]])
output = np.array([2,1,4],[1,1,1]) etc....
What would be the best way to do this iteratively on thousands of rows?
IIUC, instead of looping, you can just shift your arrays 1 up using np.roll, subtract that from your original input, and take all the resulting arrays except the last (because there will be nothing to subtract from the last array):
>>> inp = np.array([[4,4,5], [2,3,1],[1,2,0]])
>>> inp
array([[4, 4, 5],
[2, 3, 1],
[1, 2, 0]])
>>> (inp - np.roll(inp,-1,axis=0))[:-1]
array([[2, 1, 4],
[1, 1, 1]])
Or, a more straightforward way would just be to use numpy indexing:
>>> inp[:-1] - inp[1:]
array([[2, 1, 4],
[1, 1, 1]])

String to array of integer

I have a string '[1. 2. 3. 4. 5.]' and I would like to convert to get only the int such that i obtain an array of integer of [1, 2, 3, 4, 5]
How do I do that? I tried using map but unsuccessful.
Use strip for remove [], split for convert to list of values which are converted to int in list comprehension:
s = '[1. 2. 3. 4. 5.]'
print ([int(x.strip('.')) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
Similar solution with replace for remove .:
s = '[1. 2. 3. 4. 5.]'
print ([int(x) for x in s.strip('[]').replace('.','').split()])
[1, 2, 3, 4, 5]
Or with convert to float first and then to int:
s = '[1. 2. 3. 4. 5.]'
print ([int(float(x)) for x in s.strip('[]').split()])
[1, 2, 3, 4, 5]
Solution with map:
s = '[1. 2. 3. 4. 5.]'
#add list for python 3
print (list(map(int, s.strip('[]').replace('.','').split())))
[1, 2, 3, 4, 5]

In NumPy, is np.array([1, 2, 3, 4, 5]) different from np.array([[1], [2], [3], [4], [5]])?

I switched from Matlab/Octave to SciPy/NumPy recently, and I like it. But I found sometimes I get confused due to subtle differences.
In Matlab, when we declare a "row vector" of size 5, then we use the following command:
x = [1, 2, 3 ,4, 5] % in matlab
The size of this vector may be checked using the "size" command as shown below:
size(x)
ans =
1 5
I had assumed that the following in NumPy is doing the same thing as above.
x = np.array([1, 2, 3, 4, 5]) # in NumPy
But the size is somewhat weird.
>>> np.shape(x)
(5,)
The size is not (5, 1), but it is (5, ). What does it exactly mean? I'm not quite sure why the second element of this tuple is empty.
I checked that the following returns (5, 1)
y = np.array([[1], [2], [3], [4], [5]])
np.shape(y)
(5, 1)
Then, is "y" the same as "x" in NumPy? I assume it is not. I might be misunderstanding something, but could any one enlighten me on this topic?
Thanks!
The shape of a NumPy array is always a tuple. (5) is not a tuple since Python evaluates it to be equal to the number 5. To obtain a tuple, one must add a comma after the 5, as in (5,).
Thus, (5,) is a tuple containing 1 value, the number 5 and
(5, 1) is a tuple containing 2 values, the numbers 5 and 1.
The number of elements in the tuple equals the number dimensions of the array. In NumPy lingo, dimensions are also called "axes".
So
x = np.array([1, 2, 3, 4, 5])
is an array of shape (5,), and it has 1 dimension.
In contrast,
y = np.array([[1], [2], [3], [4], [5]])
is an array of shape (5, 1) and has 2 dimensions.
Thus, x and y are not the same.
Note in NumPy lingo, the "size" or an array refers to the number of values in the array:
In [48]: x.size
Out[48]: 5
In [49]: y.size
Out[49]: 5
NumPy arrays can "broadcast" their values to act like arrays of higher dimension. Since broadcasting can add new axes to the left side of an array's shape, an array of shape (5,) can broadcast to shape (1, 5).
Since for 2-dimensional arrays the first axis can be thought of as corresponding to the rows and the second axis the columns, a 1-dimensional array like x can behave like a 1-row, 5-column array -- i.e. a row vector.
y has shape (5, 1) which makes it a 5-row, 1-column array -- i.e. a column vector.
If you add an array of shape (1, 5) (i.e. a row vector) with an array of shape (5, 1) (i.e. a column vector), the broadcasting rules produces an array of shape (5, 5):
In [60]: x + y
Out[60]:
array([[ 2, 3, 4, 5, 6],
[ 3, 4, 5, 6, 7],
[ 4, 5, 6, 7, 8],
[ 5, 6, 7, 8, 9],
[ 6, 7, 8, 9, 10]])
If you want x to act like a column vector, you would need to add a new axis on the right. Broadcasting never does that, so you have to do it manually with x[:, np.newaxis].
Adding two column vectors produces another column vector:
In [61]: x[:, np.newaxis].shape
Out[61]: (5, 1)
In [56]: x[:, np.newaxis] + y
Out[56]:
array([[ 2],
[ 4],
[ 6],
[ 8],
[10]])

Resources