Skip to main content

Learn Numpy by Examples

Examples of functions made available by numpy.
Run the code here: https://repl.it/@VinitKhandelwal/numpy
import numpy as np

list1 = [1,2,3]
print(list1)
print(np.array(list1))

list2 = [[1,2,3],[4,5,6],[7,8,9]]
print(list2)
print(np.array(list2))

print(np.arange(0,10))
print(np.arange(0,10,2))
print(np.zeros(5))
print(np.zeros((5,5)))
print(np.ones(3))
print(np.ones((3,3)))
print(np.linspace(0,99,10))
print(np.eye(4))
print(np.random.rand(5))
print(np.random.rand(5,5))
print(np.random.randn(2,2))
print(np.random.randint(1,100, 10))

arr1 = np.arange(0, 25)
print(arr1)
print(arr1.reshape(5,5))
randarr1 = np.random.randint(0,100,10)
print(randarr1)
print(randarr1.min())
print(randarr1.max())
print(randarr1.argmin())
print(randarr1.argmax())
print(arr1.shape)
print(arr1[2:6])
print(arr1[2:])
print(arr1[:6])
arr1 = arr1.reshape(5,5)
print(arr1.shape)
print(arr1.dtype)
print(arr1[1:3])
arr1[4][1:4] = 100
print(arr1)
arr1 = np.arange(0,25)
print(arr1)
arr1[10:20] = 100
print(arr1)
arr1 = np.arange(0,25)
print(arr1)
slice_of_arr1 = arr1[10:15]
print(slice_of_arr1)
slice_of_arr1[:] = 99
print(slice_of_arr1)

print(f"Printing arr1 {arr1}")
arr1_copy = arr1.copy()
print(f"Copy of arr1 {arr1_copy}")
arr1_copy[:] = 101
print(f"Copy after broadcasting {arr1_copy}")

arr_2d = [[1,2,3],[4,5,6],[7,8,9]]
print(arr_2d)
arr_2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr_2d)
print(arr_2d[1])
print(arr_2d[1][2])
print(arr_2d[1,2])
print(arr_2d[1][1:3])
print(arr_2d[1:,1:])

arr1 = np.arange(0,10)
print(arr1)
bool_arr1 = arr1 > 5
print(bool_arr1)
arr2 = arr1[bool_arr1]
print(arr2)
print(arr1[arr1<5])

arr_2d = np.arange(0,50).reshape(5,10)
print(arr_2d)
print(arr_2d[2:4,5:7])

arr1 = np.arange(0,11)
print(arr1)
print(arr1+arr1)
print(arr1-arr1)
print(arr1*arr1)
print(arr1/arr1)
print(1+arr1)
print(arr1**arr1)
print(np.sqrt(arr1))
print(np.exp(arr1))
print(np.max(arr1))
print(np.min(arr1))
print(np.sin(arr1))
print(np.log(arr1))
OUTPUT
[1, 2, 3]
[1 2 3]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[0 1 2 3 4 5 6 7 8 9]
[0 2 4 6 8]
[0. 0. 0. 0. 0.]
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
[1. 1. 1.]
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
[ 0. 11. 22. 33. 44. 55. 66. 77. 88. 99.]
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
[0.58536131 0.121866   0.78973481 0.36854691 0.48029132]
[[0.05123787 0.53955972 0.59739976 0.8228303  0.566202  ]
 [0.99085132 0.68906901 0.05258071 0.9829009  0.2779436 ]
 [0.850794   0.19051684 0.63237285 0.50288235 0.90044353]
 [0.23280357 0.71815456 0.07412001 0.29499834 0.09095108]
 [0.77565526 0.53934636 0.92837746 0.65878371 0.8373636 ]]
[[-1.03635449  1.00597245]
 [-1.24290427  1.6505177 ]]
[57 40 98 88  8 96 96 65 52 90]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24]
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[51 44 57 66 80 78 75 50 83 50]
44
83
1
8
(25,)
[2 3 4 5]
[ 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
[0 1 2 3 4 5]
(5, 5)
int64
[[ 5  6  7  8  9]
 [10 11 12 13 14]]
[[  0   1   2   3   4]
 [  5   6   7   8   9]
 [ 10  11  12  13  14]
 [ 15  16  17  18  19]
 [ 20 100 100 100  24]]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24]
[  0   1   2   3   4   5   6   7   8   9 100 100 100 100 100 100 100 100
 100 100  20  21  22  23  24]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24]
[10 11 12 13 14]
[99 99 99 99 99]
Printing arr1 [ 0  1  2  3  4  5  6  7  8  9 99 99 99 99 99 1516 17 18 19 20 21 22 23
 24]
Copy of arr1 [ 0  1  2  3  4  5  6  7  8  9 99 99 99 99 99 15 16 17 18 19 20 21 22 23
 24]
Copy after broadcasting [101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101
 101 101 101 101 101 101 101]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[4 5 6]
6
6
[5 6]
[[5 6]
 [8 9]]
[0 1 2 3 4 5 6 7 8 9]
[False False False False False False  True  True  True  True]
[6 7 8 9]
[0 1 2 3 4]
[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47 48 49]]
[[25 26]
 [35 36]]
[ 0  1  2  3  4  5  6  7  8  9 10]
[ 0  2  4  6  8 10 12 14 16 18 20]
[0 0 0 0 0 0 0 0 0 0 0]
[  0   1   4   9  16  25  36  49  64  81 100]
main.py:87: RuntimeWarning: invalid value encountered in true_divide
  print(arr1/arr1)
[nan  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
[ 1  2  3  4  5  6  7  8  9 10 11]
[          1           1           4          27         256     3125
       46656      823543    16777216   387420489 10000000000]
[0.         1.         1.41421356 1.73205081 2.         2.23606798
 2.44948974 2.64575131 2.82842712 3.         3.16227766]
[1.00000000e+00 2.71828183e+00 7.38905610e+00 2.00855369e+01
 5.45981500e+01 1.48413159e+02 4.03428793e+02 1.09663316e+03
 2.98095799e+03 8.10308393e+03 2.20264658e+04]
10
0
[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427
 -0.2794155   0.6569866   0.98935825  0.41211849 -0.54402111]
main.py:95: RuntimeWarning: divide by zero encountered in log
  print(np.log(arr1))
[      -inf 0.         0.69314718 1.09861229 1.38629436 1.60943791
 1.79175947 1.94591015 2.07944154 2.19722458 2.30258509]

Comments

Popular posts from this blog

Python - List - Append, Count, Extend, Index, Insert, Pop, Remove, Reverse, Sort

🐍 Advance List List is widely used and it's functionalities are heavily useful. Append Adds one element at the end of the list. Syntax list1.append(value) Input l1 = [1, 2, 3] l1.append(4) l1 Output [1, 2, 3, 4] append can be used to add any datatype in a list. It can even add list inside list. Caution: Append does not return anything. It just appends the list. Count .count(value) counts the number of occurrences of an element in the list. Syntax list1.count(value) Input l1 = [1, 2, 3, 4, 3] l1.count(3) Output 2 It returns 0 if the value is not found in the list. Extend .count(value) counts the number of occurrences of an element in the list. Syntax list1.extend(list) Input l1 = [1, 2, 3] l1.extend([4, 5]) Output [1, 2, 3, 4, 5] If we use append, entire list will be added to the first list like one element. Extend, i nstead of considering a list as one element, it joins the two lists one after other. Append works in the following way. Input l1 = [1, 2, 3] l1.append([4, 5]) Output...

Difference between .exec() and .execPopulate() in Mongoose?

Here I answer what is the difference between .exec() and .execPopulate() in Mongoose? .exec() is used with a query while .execPopulate() is used with a document Syntax for .exec() is as follows: Model.query() . populate ( 'field' ) . exec () // returns promise . then ( function ( document ) { console . log ( document ); }); Syntax for .execPopulate() is as follows: fetchedDocument . populate ( 'field' ) . execPopulate () // returns promise . then ( function ( document ) { console . log ( document ); }); When working with individual document use .execPopulate(), for model query use .exec(). Both returns a promise. One can do without .exec() or .execPopulate() but then has to pass a callback in populate.

Python Class to Calculate Distance and Slope of a Line with Coordinates as Input

🐍  Can be run on Jupyter Notebook #CLASS DESIGNED TO CREATE OBJECTS THAT TAKES COORDINATES AND CALCULATES DISTANCE AND SLOPE class Line:     def __init__(self,coor1,coor2):         self.coor1=coor1         self.coor2=coor2 #FUNCTION CALCULATES DISTANCE     def distance(self):         return ((self.coor2[0]-self.coor1[0])**2+(self.coor2[1]-self.coor1[1])**2)**0.5 #FUNCTION CALCULATES SLOPE         def slope(self):         return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0]) #DEFINING COORDINATES coordinate1 = (3,2) coordinate2 = (8,10) #CREATING OBJECT OF LINE CLASS li = Line(coordinate1,coordinate2) #CALLING DISTANCE FUNCTION li.distance() #CALLING SLOPE FUNCTION li.slope()