Showing posts with label 1) Create an arbitrary one dimensional array called "v".. Show all posts
Showing posts with label 1) Create an arbitrary one dimensional array called "v".. Show all posts

Friday, 6 January 2017

Excercise and Answers

1) Create an arbitrary one dimensional array called "v".
Ans:>>>import numpy as np
>>>v=np.arange(5)




2) Create a new array which consists of the odd indices of previously created array "v"?
Ans:
>>>import numpy as np
>>>v=np.arange(5)
>>> odd_elements = v[1::2]
>>> odd_elements
array([1, 3, 5, 7, 9])




3) Create a new array in backwards ordering from v
Ans:
>>>import numpy as np
>>>v=np.arange(5)
>>> rv=v[::-1]
>>> rv
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])



4) What will be the output of the following code:

   a = np.array([1, 2, 3, 4, 5])
   b = a[1:4]    #######  array([1, 2, 3, 4, 5])
   b[0] = 200   #######  array([200,   3,   4])
   print(a[1])   ####### 200
>>> a
array([  1, 200,   3,   4,   5])




5) Create a two dimensional array called "m".
Ans:import numpy as np
>>> m=np.arange(10).reshape(2,5)
>>> m.ndim
2
>>> m
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])


6) Create a new array from m, in which the elements of each row are in reverse order.
Ans:

import numpy as np
>>> m=np.arange(10).reshape(2,5)
>>> m.ndim
2
>>> m
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])>>> m[::,::-1]
array([[4, 3, 2, 1, 0],
       [9, 8, 7, 6, 5]])




7) Another one, where the rows are in reverse order.
Ans:import numpy as np
>>> m=np.arange(10).reshape(2,5)
>>> m.ndim
2
>>> m
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>>>>> m[::-1]
array([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])




8) Create an array from m, where columns and rows are in reverse order.
Ans:
import numpy as np
>>> m=np.arange(10).reshape(2,5)
>>> m.ndim
2
>>> m
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> m[::-1,::-1]
array([[9, 8, 7, 6, 5],
       [4, 3, 2, 1, 0]])



9) Cut of the first and last row and the first and last column?
Ans:
import numpy as np
>>> m=np.arange(10).reshape(2,5)
>>> m.ndim
2
>>> m
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

>>> m[1:-1,1:-1]
array([], shape=(0, 3), dtype=int64)