Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1)Importing Numpy
- import numpy as np
- #2) Inititlize numpy array
- a = np.array([1,3,5,7,9])
- #3) Initilize numpy array using list
- l = ([1,2,3],[3,6,9],[2,4,6])
- a = np.array(l)
- print("Array elements\n",a,"\n----------------")
- #access elements from array
- a[1,2]
- print("a[1,2] = ",a[1,2],"\n----------------")
- a[1,1:3]
- print("a[1,1:3] = ",a[1,1:3],"\n----------------")
- a[:,1]
- print("a[:,1] = ",a[:,1],"\n----------------")
- a[:,0]=[0,9,8]
- print("a[:,0]=[0,9,8] = ",a,"\n----------------")
- #5) Apply functions on numpy
- print("a.min() : ",a.min(),"\n----------------")
- print("a.max() : ",a.max(),"\n----------------")
- print("a.absolute() : ",np.absolute(a[2]-a[1]),"\n----------------")
- np.random.shuffle(a) #random shuffle
- print("a.shuffle() : ",a,"\n----------------")
- print("a.arange() : ",np.arange(3),"\n----------------")
- print("a.zeros() : ",np.zeros((5), dtype=int),"\n----------------")
- print("a.ones() : ",np.ones((5), dtype=int),"\n----------------")
- print("a.mgrid() : ",np.mgrid[0:3],"\n----------------")
- """
- linspace()
- -> start : [optional] start of interval range. By default start = 0
- -> stop : end of interval range
- -> restep : If True, return (samples, step). By deflut restep = False
- -> num : [int, optional] No. of samples to generate
- -> dtype : type of output array
- """
- print("np.linspace()\n","B\n", np.linspace(2.0, 3.0, num=5, retstep=True), "\n----------------")
- print("rand() = ",np.random.rand(5),"\n----------------")
- #for 2d array of Random Numbers syntax :- print(np.random.rand(2, 3))
- print("sort() = ",np.sort(a, axis = None),"\n----------------")
- print("sum() = ",np.sum(a),"\n----------------")
- print("matrix() = ",np.matrix([[1,2],[3,4]]),"\n----------------")
- print("size() = ",np.size(a),"\n----------------")
- """
- OUTPUT
- Array elements
- [[1 2 3]
- [3 6 9]
- [2 4 6]]
- ----------------
- a[1,2] = 9
- ----------------
- a[1,1:3] = [6 9]
- ----------------
- a[:,1] = [2 6 4]
- ----------------
- a[:,0]=[0,9,8] = [[0 2 3]
- [9 6 9]
- [8 4 6]]
- ----------------
- a.min() : 0
- ----------------
- a.max() : 9
- ----------------
- a.absolute() : [1 2 3]
- ----------------
- a.shuffle() : [[9 6 9]
- [8 4 6]
- [0 2 3]]
- ----------------
- a.arange() : [0 1 2]
- ----------------
- a.zeros() : [0 0 0 0 0]
- ----------------
- a.ones() : [1 1 1 1 1]
- ----------------
- a.mgrid() : [0 1 2]
- ----------------
- np.linspace()
- B
- (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
- ----------------
- rand() = [0.36013086 0.63198353 0.68502288 0.27798931 0.92382514]
- ----------------
- sort() = [0 2 3 4 6 6 8 9 9]
- ----------------
- sum() = 47
- ----------------
- matrix() = [[1 2]
- [3 4]]
- ----------------
- size() = 9
- ----------------
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement