-
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.)
How to reshape array with 2 rows and 4 column ?
arr=np.arange(9)
arr= arr.reshape(3,3)
print(arr)
[[0 1 2]
[3 4 5]
[6 7 8]]
Note:it can be combined into two steps
arr=np.arange(9).reshape(3,3)
How to reshape into 3D array?
arr=np.arange(8).reshape(2,2,2)
print(arr)
[[[0 1]
[2 3]]
what are addtional functions of arange?
arange([start,][stop[,step,][,dtype]) – Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop)
example:
arr=np.arange(4).reshape(2,2)
[[0 1]
[2 3]]
arr=np.arange(4,10) – Here it would start from 4 and end to 9 .
[4 5 6 7 8 9]
arr=np.arange(4,12,3) – start from 4 and interval 3 upto 12
[ 4 7 10]
what is linspace ?
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) : Returns number spaces evenly w.r.t interval. Similiar to arange but instead of step it uses sample number.
How to evaluate sin() in long range ?
arr=np.linspace(0, 2, 10)
print(np.sin(arr))
[0. 0.22039774 0.42995636 0.6183698 0.77637192 0.8961922
0.9719379 0.99988386 0.9786557 0.90929743]
How to return copy of array collapsed into one dimension ?
arr = np.array(1, 2], [3, 4], [5, 6)
print(arr)
arr= arr.flatten()
print(arr)
[[1 2]
[3 4]
[5 6]]
[1 2 3 4 5 6]
How to create list with negative steps?
means – [10 9 8 7 6 …..]
a = np.arange(10, 1, -2)
How indexes are specified in np.array method?
newarr = a[np.array(1)]
print(newarr)
8
newarr = a[np.array(0)]
print(newarr)
10
newarr = a[np.array([3,2,1])]
[4 6 8]