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 - Mathematical Functions - Hexadecimal, Binary, Exponential, Absolute, Round

🐍 Some mathematical functions are in-built in python. Here are some of them. Convert Decimal to Hexadecimal hex(246) Output: '0xf6' hex(512) Output: '0x200' 0x is prefix to all hexadecimal numbers in output. The number after 0x is the hexadecimal value. Convert Decimal to Binary bin(1234) Output: '0b10011010010' bin(246) Output: '0b11110110' bin(512) Output: '0b1000000000' Exponential Function pow() function is similar to num1**num2, but it comes useful when you want to execute (num1**num2)%num3. Passing three arguments to pow(), does this. pow(3,4) Output: 81 This works just like 3**4 pow(3,4,5) Output: 1 But this, works equivalent to (3**4)%5. Either of the two ways can be used. At this point I feel (3**4)%5 is better, as anybody reading the code will understand what is being done immediately. While with pow(2,3,4), the person may have to refer to documentation to understand how this function works. Absolute Value The function abs() is used ...

Two Ways of rendering in React Examples

Two Ways of rendering in React Examples 1 - Render twice HTML < div id = "p1" ></ div > < div id = "p2" ></ div > CSS . person { display : inline - block ; margin : 10px ; border : 1px solid # eee ; box - shadow : 0 2px 2px # ccc ; } JAVASCRIPT (Babel) function Person ( props ) { return ( < div className = ' person "> < h1 >{ props . name }</ h1 > < p > Age :{ props . age }</ p > </ div > ); } ReactDOM . render (< Person name = "VK" age = "29" />, document . querySselector ( '#p1' )); ReactDOM . render (< Person name = "HK" age = "28" />, document . querySselector ( '#p2' )); 2 Render all at once HTML < div id = "app" ></ div > CSS . person { display : inline - block ; margin : 1...

Binary Tree Traversal in Python - In Order, Pre Order, Post Order

Binary Tree Traversal in Python - In Order, Pre Order, Post Order Run the code here to see output:  https://repl.it/@VinitKhandelwal/iterative-traversal-of-binary-tree # Node Class to Create Object of Nodes class Node (): def __init__ ( self , value , left= None , right= None ): self .value = value self .left = left self .right = right # Creation of a Binary Tree class BinaryTree (): def __init__ ( self , mylist ): self .mylist = mylist self .root=Node ( None ) for value in self .mylist : self .curr= self .root self ._add ( value ) def _add ( self , value ): if self .curr.value == None : self .curr.value = value else : if self .curr.value > value : if self .curr.left == None : self .curr.left = Node ( value ) else : self .curr = self .curr.left self ._add ( value ) else : if self...