0% found this document useful (0 votes)
9 views10 pages

NUPLE

python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

NUPLE

python
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to AI and Data Science using

Python

(Numpy)
NumPy is also called Numerical Python. NumPy is an open source Python
library that is used in almost every field of science and engineering. It
contains a powerful N-dimensional array object.
>>>import numpy np.arange() method
>>>import numpy as np np.arange() will create arrays with regularly
np.array() method incrementing values.
Creating 1-dimension array. >>> import numpy as np
>>> import numpy as np >>> a = np.arange(5)
>>> print(a)
>>> a = np.array([10,20,30])
[0 1 2 3 4]
>>> print(a) >>> a = np.arange(3.0)
[10 20 30] >>> print(a)
Creating 2-dimension array [0. 1. 2.]
>>> import numpy as np >>> a = np.arange(3,8)
x = np.array([[10,20,30],[40,50,60]]) >>> print(a)
[3 4 5 6 7]
>>> print(x)
>>> a = np.arange(1,10,2)
[[10 20 30] [40 50 60]] >>> print(a)
[1 3 5 7 9]
np.zeros() method
np.zeros() method will create an array filled with 0 values. The default type is
float. Array Indexing
>>> import numpy as np >>> import numpy as np
>>> a = np.zeros(3) >>> a = np.array([10, 20, 30, 40])
>>> print(a) >>> print(a[0])
[0. 0. 0.] 10
>>> sum = a[1] + a[3]
>>> x = np.zeros((2, 3))
>>> print(sum)
>>> print(x) 60
[[0. 0. 0.] [0. 0. 0.]] >>> b = np.array([[10, 20, 30] , [40, 50, 60]])
np.ones() method >>> print(b[0][1])
>>> import numpy as np 20
>>> a = np.ones(4) >>> sum = b[1][0] + a[1][1] + b[1][2]
>>> print(sum)
>>> print(a)
70
[1. 1. 1. 1.]
>>> x = np.ones((2,3))
>>> print(x)
[[1. 1. 1.] [1. 1. 1.]]
Concatenating Arrays
You can use np.concatenate() method to join a sequence of arrays along an
existing axis.
>>> import numpy as np Sorting Arrays
>>> a = np.array([1, 2, 3, 4]) The np.sort() method returns a sorted
>>> b = np.array([5, 6, 7, 8]) copy of an array.
>>> c = np.concatenate((a,b)) >>> import numpy as np
>>> print(c) >>> a = np.array([2, 1, 5, 3, 7, 4, 6, 8])
[1 2 3 4 5 6 7 8] >>> b = np.sort(a)
>>> print(b)
>>> x = np.array([[1, 2], [3, 4]])
[1 2 3 4 5 6 7 8]
>>> y = np.array([[5, 6]]) >>> x = np.array([[10,4],[3,1]])
>>> z = np.concatenate((x, y), axis=0) >>> y = np.sort(x)
>>> print(z) >>> print(y)
[[1 2] [3 4] [5 6]] [[ 4 10] [ 1 3]]
>>> z = np.concatenate((x, y.T), axis=1) >>> y = np.sort(x, axis=None)
>>> print(z) y >>> print(y)
[[1 2 5] [3 4 6]] [ 1 3 4 10]
>>> z = np.concatenate((a, b), axis=None) >>> y = np.sort(x, axis=0)
>>> print(z) >>> print(y)
[1 2 3 4 5 6 7 8] [[ 3 1] [10 4]])
Shape and Size of an array Addition
>>> import numpy as np >>> import numpy as np
>>> x = np.array([[10,4,9],[3,1,8]]) >>> a = np.array([10,20,30])
>>> b = np.array([1,2,3])
>>> print(x.shape)
>>> c = a + b
(2, 3) >>> print(c)
>>> print(x.size) [11 22 33]
6 Subtraction
Reshaping an array >>> a = np.array([10,20,30])
>>> import numpy as np >>> b = np.array([1,2,3])
>>> c = a – b
>>> arr = np.arange(12)
>>> print(c)
>>> print(arr) [ 9 18 27]
[ 0 1 2 3 4 5 6 7 8 9 10 11] Division
>>> b = arr.reshape(3, 4) >>> a = np.array([10,20,30])
>>> print(b) >>> b = np.array([1,2,3]) >>> c = a / b >>>
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] print(c)
[10. 10. 10.]
Array Slicing
Multiplication
>>> import numpy as np >>> a = np.array([10,20,30])
>>> a = np.array([10, 20, 30, 40, 50, 60]) >>> b = np.array([1,2,3])
>>> print(a[1:3]) >>> c = a * b
[20 30] >>> print(c)
[10 40 90]
max() Method min() Method
One Dimensional Array One Dimensional Array
>>> import numpy as np >>> import numpy as np
>>> a = np.array([10, 20, 30, 40])
>>> a = np.array([10, 20, 30, 40])
>>> b = a.min()
>>> b = a.max() >>> print(b)
>>> print(b) 10
40 Two Dimensional Array
Two Dimensional Array >>> a = np.array([[0, 1, 6], [2, 4, 1]])
>>> a = np.array([[0, 1, 6], [2, 4, 1]]) >>> b = np.min(a)
>>> print(b)
>>> b = np.max(a)
0
>>> print(b) >>> b = np.min(a, axis=0)
6 >>> print(b)
>>> b = np.max(a, axis=0) [0 1 1]
>>> print(b) [2 4 6] >>> b = np.min(a, axis=1)
>>> b = np.max(a, axis=1) >>> print(b)
[0 1]
>>> print(b)
[6 4]
sum() Method
prod() Method
Sum of array elements over a given axis.Return the product of array elements over a
One Dimensional Array given axis.
>>> import numpy as np One Dimensional Array
>>> a = np.array([10, 20, 30, 40]) >>> import numpy as np
>>> b = a.sum() >>> a = np.array([10, 20, 30, 40])
>>> b = a.prod()
>>> print(b)
>>> print(b)
100 240000
Two Dimensional Array Two Dimensional Array
>>> a = np.array([[0, 1, 6], [2, 4, 1]]) >>> a = np.array([[0, 1, 6], [2, 4, 1]])
>>> b = np.sum(a) >>> b = np.prod(a)
>>> print(b) >>> print(b)
0
14
>>> b = np.prod(a, axis=0)
>>> b = np.sum(a, axis=0) >>> print(b) [0 4 6]
>>> print(b) >>> b = np.prod(a, axis=1)
[2 5 7] >>> print(b)
>>> b = np.sum(a, axis=1) [0 8]
>>> print(b)
[7 7]
mean() Method median() Method
One Dimensional Array >>> import numpy as np
>>> import numpy as np >>> a = np.array([9.2, 10.7, 6.8, 9, 3
5.7])
>>> a = np.array([9.2, 10.7, 6.8, 9, 3.4, 5.7, 5.7])
>>> b = np.median(a)
>>> b = np.mean(a) >>> print(b)
>>> print(b) 6.8
7.214285714285715 Two Dimensional Array
Two Dimensional Array >>> a = np.array([[9.2, 10.7, 6.8],[ 9
>>> a = np.array([[9.2, 10.7, 6.8],[ 9, 3.4, 5.7]]) 5.7]])
>>> b = np.median(a)
>>> b = np.mean(a)
>>> print(b)
>>> print(b) 7.9
7.466666666666668 >>> b = np.median(a, axis=0)
>>> b = np.mean(a, axis=0) >>> print(b)
>>> print(b) [9.1 7.05 6.25]
[9.1 7.05 6.25] >>> b = np.median(a, axis=1)
print(b)
>>> b = np.mean(a, axis=1)
[9.2 5.7]
>>> print(b)
[8.9 6.03333333]
std() Method var() Method
>>> import numpy as np >>> import numpy as np
>>> a = np.array([9.2, 10.7, 6.8, 9, 3.4, 5.7, 5.7]) >>> a = np.array([9.2, 10.7, 6.8, 9,
5.7])
>>> b = np.std(a)
>>> b = np.var(a) >>>
>>> print(b) print(b)
2.3479039718916295 5.512653061224489
Two Dimensional Array two Dimensional Array
>>> a = np.array([[9.2, 10.7, 6.8],[ 9, 3.4, 5.7]]) >>> a = np.array([[9.2, 10.7, 6.8],[
>>> b = np.std(a) 5.7]])
>>> b = np.var(a)
>>> print(b)
>>> print(b)
2.4465395062323347 5.985555555555556
>>> b = np.std(a, axis=0) >>> b = np.var(a, axis=0)
>>> print(b) >>> print(b)
[0.1 3.65 0.55] [1.00000e-02 1.33225e+01 3.02500
>>> b = np.std(a, axis=1) >>> b = np.var(a, axis=1)
>>> print(b)
>>> print(b)
[2.58 5.28222222]
[1.60623784 2.29830856]
Finding LCM (Lowest Common Multiple)
Finding GCD (Greatest Common
>>>import numpy as np
>>>num1 = 4
Denominator)
>>>num2 = 6
>>>import numpy as np
>>>x = np.lcm(num1, num2) >>>num1 = 6
>>>print(x) >>>num2 = 9
12 >>>x = np.gcd(num1, num2)
>>>import numpy as np >>>print(x)
>>>arr = np.array([3, 6, 9]) 3
>>>x = np.lcm.reduce(arr) >>>import numpy as np
>>>print(x) >>>arr = np.array([20, 8, 32, 36, 1
18 >>>x = np.gcd.reduce(arr)
>>>import numpy as np >>>print(x)
>>>arr = np.arange(1, 11) 4
>>>x = np.lcm.reduce(arr)
>>>print(x)
2520

You might also like