-
Notifications
You must be signed in to change notification settings - Fork 1
Python
what is rank *
[[ 1, 2, 3],
[ 4, 2, 5]]
Here, rank = 2 (as it is 2-dimensional or it has 2 axes)
First dimension(axis) length = 2, second dimension has length = 3
overall shape can be expressed as: (2, 3)
Example:
arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )
what is array type ?
type(arr)
<class ‘numpy.ndarray’>
how to Print array dimensions (axes) ? *
arr.ndim
2
how to Print shape of array ?
arr.shape
(2, 3)
How to Print size (total number of elements) of array ? *
arr.size
6
How to Print type of elements in array ? *
arr.dtype
int64
How to create array from list with type float ?
a = np.array(1,2,3],[4,5,6],[7,8,9,dtype=‘f’)
print(a.dtype)
float32
How to create Array from tuple ?
a = np.array((1,2,3))
print(a)
[1 2 3]
How to create 3X4 Array with all zeros?
arr = np.zeros((3,4))
How to create a constant value of array of complex type ?
arr= np.full((3,3),6,dtype=‘complex’)
Example-2
arr = np.array([[-1, 2, 0, 4 , 5 ,6],
[4, -0.5, 6, 0,5,6],
[2.6, 0, 7, 8,5,6],
[3, -7, 4, 2.0,6,6]])
How to slice Array with first two rows and alternate columns (0 and 2 ) ?
print(arr[:2,::2]
[[-1. 0. 5.]
[ 4. 6. 5.]]
How to slice array with all rows and every 3rd column ( 0,3,6)?
print(arr[:,::3])
[[-1. 4. ]
[ 4. 0. ]
[ 2.6 8. ]
[ 3. 2. ]]
How to get maximum element in array ?
print(arr.max())
How to get maximum element RowWise ?
print(arr.max(axis=1))
[6. 6. 8. 6.]
How to get maximum element ColumnWise ?
print(arr.max(axis=0))
[4. 2. 7. 8. 6. 6.]
How to get sum of all element of array?
print(arr.sum())
79.1
How to get sum along each row ?
print(arr.sum(axis=1))
[16. 20.5 28.6 14. ]
How to get sum along each column ?
print(arr.sum(axis=0))
[ 8.6 -5.5 17. 14. 21. 24. ]
How to create int16 into data type object ?
import numpy as np
print(np.dtype(np.int16))
How to initialize array with array values?
import array
arr = array.array(‘i’,[1,2,3,4])
for i in range(0,3):
print(i)
1 2 3 4
Note:Please check further what use of i here
How to create Array using numpy method ?
import numpy as np
arr= np.empty(2,dtype=int)
print(arr)
[-1 -1]
also tried same function with different array dimensions, need to check how these default values are populated , not sure now.
arr=np,empty([2,2],dtype=int)
print(arr)
[[0 1]
[2 3]]
arr= np.empty([4,4],dtype=int)
print(arr)
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 1288 0]
[ 0 0 0 0]]
arr= np.empty([3,2],dtype=int)
print(arr)
[[ 0 1072168960]
[ 0 1072168960]
[ 0 0]]
How to return the new array of given shape with zero values ?
arr=np.zeros(2,dtype=int)
[0 0]
arr=np.zeros([2,2],dtype=int)
print(arr)
[[0 0]
[0 0]]
what is arange?
arr=np.arange(9)
print(arr)
[0 1 2 3 4 5 6 7 8]
Note: arange can be used with int and list .
How to reshape array and what is mandatory condition in order to reshape work ?
Condition
Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM).
a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)