Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views

Python Cheat Sheet For NumPy

This document provides a cheat sheet for commonly used NumPy functions for working with NumPy arrays. It lists 20 functions including array(), fromiter(), arange(), linspace(), reshape(), empty(), zeros(), ones(), eye(), full(), rand(), randint(), vstack(), hstack(), concatenate(), hsplit(), vsplit() and provides examples of using each function to create, manipulate, or combine NumPy arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Python Cheat Sheet For NumPy

This document provides a cheat sheet for commonly used NumPy functions for working with NumPy arrays. It lists 20 functions including array(), fromiter(), arange(), linspace(), reshape(), empty(), zeros(), ones(), eye(), full(), rand(), randint(), vstack(), hstack(), concatenate(), hsplit(), vsplit() and provides examples of using each function to create, manipulate, or combine NumPy arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Cheat Sheet for NumPy

Built-in Functions
S.No Function Name Example Output
1 array() a1= np.array([1,2,3,4])
a2=np.array([[2,4,6],[1,3,5]])
a1.shape (4,)
a2.shape (2,3)
a2.itemsize 4
list1=[1,2,3,4,5]
ar1=np.array(list1)
print(type(ar1)) <class ‘numpy.ndarray’>
print(ar1.dtype) Int32
print(ar1.itemsize) 4
print(ar1) [1 2 3 4 5]
print(ar1[0],ar1[3]) 14
tup1=(2.5,3.5,4.5,6.5)
ar2=np.array(tup1)
print(type(ar2)) <class ‘numpy.ndarray’>
print(ar2.dtype) float64
print(ar2.itemsize) 8
print(ar2) [2.5 3.5 4.5 6.5]
print(ar2[0],ar2[3]) 2.5 6.5
ar3=np.array(list1,dtype=int64)
print(ar3.dtype) int64
print(ar3.itemsize) 8
ar4=np.array(tup1,dtype=np.float16)
print(ar4.dtype) float16
print(ar4.itemsize) 2
2 fromiter() adict={1:”A”,2:”B”,3:”C”,4:”D”,5:”E”}
ar5=np.fromiter(adict,dtype=np.int32)
ar5.dtype,ar5.itemsize dtype(‘int32’),4)
print(ar5[0],ar5[3]) 1 4
print(ar5) [1 2 3 4 5]
alist=[‘a’,’b’,’c’,’d’,’e’]
atup=(1.5,2.5,3.5,4.5)
lst2=[a*2-3 for a in range(2,6)]
nd1=np.fromiter(alist,dtype=”U1”) array([‘a’,’b’,’c’,’d’,’e’],dtype=”<U1”)
nd2=np.fromiter(atup,dtype=”U1”) array([[‘1’,’2’,’3’,’4’],dtype=”<U1”)
nd3=np.fromiter(lst2,dtype=np.int16) array([1,3,5,7],dtype=int16)
3 arange() arr5=np.arange(7) array([0,1,2,3,4,5,6])
arr6=np.arange(1,7,2,np.float32) array([1.,3.,5.],dtype=float32)
4 linspace() arr1=np.linspace(2.5,5,6) array([2.5,3.,3.5,4.,4.5,5.])
5 reshape() ar=np.arange(10) array([0,1,2,3,4,5,6,7,8,9])
ar1=np.reshape(5,2) array([[0,1],
[2,3],
[4,5],
[6,7],
[8,9]])
ar2=ar.reshape(2,5) array([0,1,2,3,4],
[5,6,7,8,9]])
ary=np.arange(8.0).reshape(2,4) array([[0.,1.,2.,3.],
[4., 5.,6.,7.]])
6 empty() arr1=np.empty([3,2]) If you display arr1,arr2, it will display any random
arr2=np.empty([3,4],dtype=np.int8) contents, which are uninitialized garbage values.
7 zeros() arr3=np.zeros([2,3],dtype=np.int64,order=”c”) array([0,0,0],
[0,0,0]],dtype=int64)
8 ones() arr4=np.ones([2,3],dtype=np.float32) array([1.,1.,1.],
[1.,1.,1.]],dtype=float32)
9 empty_like() arr5=np.empty_like(arr4) If you display ar5 it will display any random contents,
which are uninitialized garbage values.
10 zeros_like() arr6=np.zeros_like(arr1) array([[0,0],
[0,0],
[0,0]])
11 ones_like() arr7=np.ones_like(arr3) array([[1,1,1],
[1,1,1]],dtype=int64)
12 eye() np.eye(5) Create a 5 x 5 array with values 0 at non-diagonal
positions and with 1 on diagonal
13 full() np.full((2,3),6) Create a 2 x 3 array with all values as 6
14 rand() np.random.rand(3,4) Create a 3 x 4 array of random floats in the range 0-1
np.random.rand(4,5)*100 Create a 4 x 5 array of random floats in the rage 0-100
15 randint() np.random.randint(5,size=(2,3)) Create a 2 x 3 array with random ints in the range 0-4
16 vstack() Arr1=np.array([[0,1,2],
[3,4,5],
[6,7,8]])
Arr2=np.array([[10,11,12],
[13,14,15],
[16,17,18]])
Arr3=np.vstack((Arr1,Arr2)) array([[0,1,2],
[3,4,5],
[6,7,8],
[10,11,12],
[13,14,15],
[16,17,18]])
17 Hstack() Arr4=np.hstack((Arr1,Arr2)) array([[0,1,2,10,11,12],
[3,4,5,13,14,15],
[6,7,8,16,17,18]])
18 concatenate()
jar1=np.concatenate((ar1,ar2),axis=0) array([[3,4,1],

ar1= [6,8,5],
[2,1,3],
[2,4,2],
3 4 1
[1,9,1]])
6 8 5
2 1 3

jar2=np.concatenate((ar1,ar3),axis=1) array([[3,4,1,6,1],

ar2= [6,8,5,2,1],
[2,1,3,8,5]])
2 4 2
1 9 1 jar3=np.concatenate((ar1,ar4),axis=1) array([[3,4,1,2],
[6,8,5,8],

ar3=
[2,1,3,5]])

jar4=np.concatenate((ar2,ar1),axis=0) array([[2,4,2],
6 1
[1,9,1],
2 1
[3,4,1],
8 5
[6,8,5],
[2,1,3]])

ar4=
2 jar5=np.concatenate((ar2,ar5),axis=1) array([[2,4,2,1,2,8,9],
8 [1,9,1,6,3,4,5]])
5 array([[3,4,1,2,1],
[6,8,5,4,9],
jar6=np.concatenate((ar1,ar2.T),axis=1) [2,1,3,2,1]])

ar5= array([[3,4,1,6,8,5,2,1,3,2,8,5])

1 2 8 9 jar7=np.concatenate((ar1,ar4),axis=None)
6 3 4 5

19 hsplit() ary

np.hsplit(ary,2)

0. 1. 2. 3. 4. 5.
6. 7. 8. 9. 10 11.
array([[0.,1.,2.],
12. 13. 14. 15. 16. 17. [6.,7.,8.],
18. 19. 20. 21. 22. 23. [12.,13.14.],
[18.,19.,20.]])
array([[3.,4.,5.],
[9.,10.,11.],
[15.,16.,17.],
[21.,22.,23.]])

np.hsplit(ary,3) array([[0.,1.],
[6.,7.],
[12.,13.],
[18.,19.]])

array([[2.,3.],
[8.,9.],
[14.,15.],
[20.,21.]])

array([[4.,5.],
[10.,11.],
[16.,17.],
[22.,23.]])

20 vsplit() ar1,ar2=np.vsplit(ary,2) >>>ar1


array([[0.,1.,2.,3.,4.,5.],
[6.,7.,8.,9.,10.,11.]])
>>>ar2
array([[12.,13.,14.,15.,16.,17.],
[18.,19.,20.,21.,22.,23.]])
arid=[10,11,12,13,14,15,16,17,18,19] array([10,11]),array([12,13,14,15]),array([16,17,18,19])
21 split() np.split(arid,[2,6])
[2,6] can be understood as [0:2],[2:6],[6:]

ary

0. 1. 2. 3. 4. 5.
6. 7. 8. 9. 10 11.

12. 13. 14. 15. 16. 17.


18. 19. 20. 21. 22. 23.
np.split(ary,[1,4]) array([[0.,1.,2.,3.,4.,5.]])
can be understood as [0:1],[1:4],[4:]
array([[6.,7.,8.,9.,10.,11.],
[12.,13.,14.,15.,16.,17.,],
[18.,19.,20.,21.,22.,23.]])

22 extract() cond1=

You might also like