Add elements to 2-D array - python

I have the following Arrays
import numpy as np
A = np.array([[1,2], [3,4]])
B = np.array([5,6])
Now I want to add the second element of A to B using the append function:
B = np.append(B, A[1])
What I want to get is:
B = np.array([[5, 6],[3,4]])
But what I get is:
B = np.array([5, 6, 3, 4])
How can I solve this? Should I use another function instead of numpy.append?

import numpy as np
A = np.array([[1,2], [3,4]])
B = np.array([[5,6]])
B = np.append(B, [A[1]], axis=0)
print(B)
Output
array([[5, 6],
[3, 4]])
This would be one of the way using np.append() specifying the axis = 0.

You can use np.stack for this instead of np.append:
>>> np.stack([B, A[1]])
array([[5, 6],
[3, 4]])
You could also add [:, None] to your two arrays and append with axis=1:
>>> np.append(B[:, None], A[1][:, None], axis=1)
array([[5, 3],
[6, 4]])

Related

Python matrix row shifting(Also column)

Is there any way that I can shift specific row in python? (It would be nice if numpy is used).
I want
[[1,2],
[3,4]]
to be
[[1,2],
[4,3]].
Also for the column would be nice!
[[1,2],
[3,4]]
to be
[[1,4],
[3,2]]
.
Thank you.
np.roll is your friend.
>>> import numpy as np
>>> x = np.array([[1,2],[3,4]])
>>> x
array([[1, 2],
[3, 4]])
>>> x[1]
array([3, 4])
>>> np.roll(x[1],1)
array([4, 3])
>>> x[1] = np.roll(x[1],1)
>>> x
array([[1, 2],
[4, 3]])
>>> x[1] = np.roll(x[1],1)
>>> x[:,1]
array([2, 4])
>>> x[:,1] = np.roll(x[:,1],1)
>>> x
array([[1, 4],
[3, 2]])
>>>
For the row shifting you can do:
import numpy as np
matrix = np.random.rand(5, 5)
row_no = 2
matrix[row_no, :] = np.array([matrix[row_no, -1]] + list(matrix[row_no, :-1]))
and similarly for column shifting you can simply switch the dimension orders.
import numpy as np
matrix = np.random.rand(5, 5)
col_no = 2
matrix[:, col_no] = np.array([matrix[-1, col_no]] + list(matrix[:-1, col_no]))

Numpy : convert 2D array to 3D array

I have a 2 dimensional array : A = numpy.array([[1, 2, 3], [4, 5, 6]]) and would like to convert it to a 3 dimensional array : B = numpy.array([[[1, 2, 3], [4, 5, 6]]])
Is there a simple way to do that ?
Simply add a new axis at the start with np.newaxis -
import numpy as np
B = A[np.newaxis,:,:]
We could skip listing the trailing axes -
B = A[np.newaxis]
Also, bring in the alias None to replace np.newaxis for a more compact solution -
B = A[None]
It is also possible to create a new NumPy array by using the constructor so that it takes in a list. This list contains a single element which is the array A and it will allow you to create same array with the singleton dimension being the first one. The result would be the 3D array you desire:
B = numpy.array([A])
Example Output
In [13]: import numpy as np
In [14]: A = np.array([[1, 2, 3], [4, 5, 6]])
In [15]: B = np.array([A])
In [16]: B
Out[16]:
array([[[1, 2, 3],
[4, 5, 6]]])

Append a 1d array to a 2d array in Numpy Python

I have a numpy 2D array [[1,2,3]].
I need to append a numpy 1D array,( say [4,5,6]) to it, so that it becomes [[1,2,3], [4,5,6]]
This is easily possible using lists, where you just call append on the 2D list.
But how do you do it in Numpy arrays?
np.concatenate and np.append dont work. they convert the array to 1D for some reason.
Thanks!
You want vstack:
In [45]: a = np.array([[1,2,3]])
In [46]: l = [4,5,6]
In [47]: np.vstack([a,l])
Out[47]:
array([[1, 2, 3],
[4, 5, 6]])
You can stack multiple rows on the condition that The arrays must have the same shape along all but the first axis.
In [53]: np.vstack([a,[[4,5,6], [7,8,9]]])
Out[53]:
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]])
Try this:
np.concatenate(([a],[b]),axis=0)
when
a = np.array([1,2,3])
b = np.array([4,5,6])
then result should be:
array([[1, 2, 3],
[4, 5, 6]])

NumPy/Python array

I created a NumPy array,
a = numpy.array([[1,2,3][4,5,6]])
I want to make the array look like this [[1,4],[2,5],[3,6]] and also after I make change I want to return to the original structure.
Is there a NumPy command to run a function on all values, like a[0] * 2?
The result should be
[[2,8][2,5][3,6]
You want to transpose the array (think matrices). Numpy arrays have a method for that:
a = np.array([[1,2,3],[4,5,6]])
b = a.T # or a.transpose()
But note that b is now a view of a; if you change b, a changes as well (this saves memory and time otherwise spent copying).
You can change the first column of b with
b[0] *= 2
Which gives you the result you want, but a has also changed! If you don't want that, use
b = a.T.copy()
If you do want to change a, note that you can also immediately change the values you want in a itself:
a[:, 0] *= 2
You can use zip on the ndarray and pass it to numpy.array:
In [36]: a = np.array([[1,2,3], [4,5,6]])
In [37]: b = np.array(zip(*a))
In [38]: b
Out[38]:
array([[1, 4],
[2, 5],
[3, 6]])
In [39]: b*2
Out[39]:
array([[ 2, 8],
[ 4, 10],
[ 6, 12]])
Use numpy.column_stack for a pure NumPy solution:
In [44]: b = np.column_stack(a)
In [45]: b
Out[45]:
array([[1, 4],
[2, 5],
[3, 6]])
In [46]: b*2
Out[46]:
array([[ 2, 8],
[ 4, 10],
[ 6, 12]])

matrix multiplication with a constant

I Want to multiply the given 1x1x3 matrix with a constant value.
a= [[[1, 2, 3]]]
expected result is
a*3 = [[[3,6,9]]]
please help me to solve this
Use NumPy:
In [1]: import numpy as np
In [2]: a = np.array([[[1, 2, 3]]])
In [3]: a
Out[3]: array([[[1, 2, 3]]])
In [4]: a*3
Out[4]: array([[[3, 6, 9]]])
Try:
a= [[[1, 2, 3]]]
for i in range(len(a[0][0])):
a[0][0][i] *= 3
print a
Here's one way using list comprehensions:
>>> a = [[[1, 2, 3]]]
>>> b = [[x*3 for x in a[0][0]]]
>>> b
[[3, 6, 9]]
Here is a way to do it using pure Python:
a3 = [[[el * 3 for el in col] for col in row] for row in a]
This works with 3D matrices of any shape, not just 1x1x3.
However, if this is the sort of thing you need to do on a regular basis, I would encourage you to learn NumPy. Then you'll be able to write:
a3 = a * 3

Categories