Array manipulation routines


In [1]:
import numpy as np

In [2]:
np.__version__


Out[2]:
'1.11.2'

Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150.


In [5]:



[[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
   1.  1.  1.  1.  1.  1.]]

Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6].


In [22]:



[1 4 2 5 3 6]

Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element.


In [23]:



5

Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).


In [36]:



(4L, 3L, 5L)

Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3).


In [38]:



(4L, 3L)

Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4).


In [42]:



(3L, 1L, 4L)

Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4).


In [43]:



(3L, 4L)

Q7. Lex x be an array
[[ 1 2 3]
[ 4 5 6].

and y be an array
[[ 7 8 9]
[10 11 12]].
Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].


In [31]:



[[ 1  2  3  7  8  9]
 [ 4  5  6 10 11 12]]

Q8. Lex x be an array
[[ 1 2 3]
[ 4 5 6].

and y be an array
[[ 7 8 9]
[10 11 12]].
Concatenate x and y so that a new array looks like
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]


In [38]:



[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]].


In [54]:



[[1 4]
 [2 5]
 [3 6]]

Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]].


In [34]:



[[[1 4]]

 [[2 5]]

 [[3 6]]]

Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order.


In [62]:



[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]

Q11. Let x be an array
[[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.]],

[[ 8., 9., 10., 11.],
[ 12., 13., 14., 15.]]].
Split it into two such that the first array looks like
[[[ 0., 1., 2.],
[ 4., 5., 6.]],

[[ 8., 9., 10.],
[ 12., 13., 14.]]].

and the second one look like:

[[[ 3.],
[ 7.]],

[[ 11.],
[ 15.]]].


In [72]:



[array([[[ 0,  1,  2],
        [ 4,  5,  6]],

       [[ 8,  9, 10],
        [12, 13, 14]]]), array([[[ 3],
        [ 7]],

       [[11],
        [15]]])]

Q12. Let x be an array
[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 12., 13., 14., 15.]].
Split it into two arrays along the second axis.


In [74]:



[array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])]

Q13. Let x be an array
[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 12., 13., 14., 15.]].
Split it into two arrays along the first axis.


In [75]:



[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]])]

Q14. Let x be an array [0, 1, 2]. Convert it to
[[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]].


In [93]:



[[0 1 2 0 1 2]
 [0 1 2 0 1 2]]

Q15. Let x be an array [0, 1, 2]. Convert it to
[0, 0, 1, 1, 2, 2].


In [83]:



[0 0 1 1 2 2]

Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
remove the leading the trailing zeros.


In [105]:



[1 2 3 0 2 1]

Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.


In [107]:



[1 2 3 4 5] [2 3 1 1 2]

Q18. Lex x be an array
[[ 1 2]
[ 3 4].
Flip x along the second axis.



In [120]:



[[2 1]
 [4 3]]

Q19. Lex x be an array
[[ 1 2]
[ 3 4].
Flip x along the first axis.


In [121]:



[[3 4]
 [1 2]]

Q20. Lex x be an array
[[ 1 2]
[ 3 4].
Rotate x 90 degrees counter-clockwise.


In [122]:



[[2 4]
 [1 3]]

Q21 Lex x be an array
[[ 1 2 3 4]
[ 5 6 7 8].
Shift elements one step to right along the second axis.


In [126]:



[[4 1 2 3]
 [8 5 6 7]]

In [ ]: