0% found this document useful (0 votes)
9 views

Python - Lesson 3

hoc python 3

Uploaded by

Mit Jiu
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 views

Python - Lesson 3

hoc python 3

Uploaded by

Mit Jiu
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/ 32

PYTHON

Tutor: Nguyễn Lê Chí Bảo


01 03

Introduction to Python Numpy


MODULE

02 04
Function Pandas
MODULE 3.
NUMPY
Module 3:NumPY

Introduction to NumPy

Random with NumPy


Introduction to Numpy
Module 3: NumpY

NumPy

• Introduces objects for multidimensional arrays and matrices, as well as functions that allow to
easily perform advanced mathematical and statistical operations on those objects

• Provides vectorization of mathematical operations on arrays and matrices which


significantly improves the performance

• Many other python libraries are built on NumPy


Module 3: NumpY

Quick start
Module 3: NumpY

Python List vs NumPy Array


Module 3: NumpY

Array shape
Module 3: NumpY

Element type (dtype)


Module 3: NumpY

Array creation
Module 3: NumpY

Array creation
Module 3: NumpY

NumPy – Basic syntax


SIMPLE ARRAY CREATION BYTES PER ELEMENT

>>> a = np.array([0,1,2,3]) >>> a.itemsize # per element


>>> a 4
array([0, 1, 2, 3])
ARRAY SHAPE
CHECKING THE TYPE
# shape returns a tuple
>>> type(a) # listing the length of the
<type 'array'> # array along each dimension.
>>> a.shape
NUMERIC ‘TYPE’ OF ELEMENTS (4,)
>>> shape(a)
>>> a.dtype (4,)
dtype(‘int32’)
Module 3: NumpY

NumPy – Basic syntax


ARRAY SIZE ARRAY COPY

# size reports the entire # create a copy of the array


# number of elements in an >>> b = a.copy()
# array. >>> b
>>> a.size array([0, 1, 2, 3])
4
>>> size(a) BYTES OF MEMORY USED
4
NUMBER OF DIMENSIONS # returns the number of bytes
# used by the data portion of
# the array.
>>> a.ndim >>> a.nbytes
1 12
Module 3: NumpY

NumPy – Basic syntax


CONVERSION TO LIST

# convert a numpy array to a


# python list.
>>> a.tolist()
[0, 1, 2, 3]

# For 1D arrays, list also


# works equivalently, but
# is slower.
>>> list(a)
[0, 1, 2, 3]
Module 3: NumpY

NumPy – Setting Array Elements


ARRAY INDEXING FILL

>>> a[0] # set all values in an array.


0 >>> a.fill(0)
>>> a[0] = 10 >>> a
>>> a [0, 0, 0, 0]
[10, 1, 2, 3]
# This also works, but may
# be slower.
>>> a[:] = 1
>>> a
[1, 1, 1, 1]
Module 3: NumpY

NumPy – Setting Array Elements


BEWARE OF TYPE COERSION

>>> a.dtype
dtype('int32')

# assigning a float to into


# an int32 array will
# truncate decimal part.
>>> a[0] = 10.6
>>> a
[10, 1, 2, 3]

# fill has the same behavior


>>> a.fill(-4.8)
>>> a
[-4, -4, -4, -4]
Module 3: NumpY

NumPy – Multi-Dimensional Arrays


MULTI-DIMENSIONAL ARRAYS ELEMENT COUNT

>>> a = np.array([[ 0, 1, 2, 3],[10,11,12,13]]) >>> a.size


>>> a 8
array([[ 0, 1, 2, 3], >>> size(a)
[10,11,12,13]]) 8
(ROWS,COLUMNS) NUMBER OF DIMENSIONS

>>> a.shape >>> a.ndims


(2, 4) 2
>>> shape(a)
(2, 4)
Module 3: NumpY

NumPy – Array Slicing


SLICING WORKS MUCH LIKE STANDARD PYTHON SLICING

>>> a[0,3:5]
array([3, 4])
0 1 2 3 4 5
>>> a[4:,4:]
array([[44, 45], 10 11 12 13 14 15
[54, 55]])
20 21 22 23 24 25
>>> a[:,2]
array([2,12,22,32,42,52]) 30 31 32 33 34 35
40 41 42 43 44 45
50 51 52 53 54 55
Module 3: NumpY

NumPy – Array Calculation Methods


SUM FUNCTION SUM ARRAY METHOD

>>> a = np.array([[1,2,3],[4,5,6]], float)


# The a.sum() defaults to
# Sum defaults to summing all
# summing *all* array values
# *all* array values.
>>> a.sum()
>>> sum(a)
21.
21.
# supply the keyword axis to
# Supply an axis argument to
# sum along the 0th axis.
# sum along a specific axis.
>>> sum(a, axis=0)
>>> a.sum(axis=0)
array([5., 7., 9.])
array([5., 7., 9.])
# supply the keyword axis to
# sum along the last axis.
>>> sum(a, axis=-1)
array([6., 15.])
Module 3: NumpY

NumPy – Min/Max
MIN ARGMIN

>>> a = np.array([2.,3.,0.,1.]) # Find index of minimum value.


>>> a.min(axis=0) >>> a.argmin(axis=0)
0. 2
# use Numpy’s amin() instead # functional form
# of Python’s builtin min() >>> argmin(a, axis=0)
# for speed operations on 2
# multi-dimensional arrays.
>>> amin(a, axis=0)
0.
Module 3: NumpY

NumPy – Min/Max
MAX ARGMAX

>>> a = np.array([2.,1.,0.,3.]) # Find index of maximum value.


>>> a.max(axis=0) >>> a.argmax(axis=0)
3. 1
# functional form
# functional form >>> argmax(a, axis=0)
>>> amax(a, axis=0) 1
3.
Module 3: NumpY

NumPy – Statitics Array Methods


MEAN STANDARD DEV./VARIANCE

>>> a = np.array([[1,2,3],[4,5,6]], float)


# Standard Deviation
# mean value of each column
>>> a.std(axis=0)
>>> a.mean(axis=0)
array([ 1.5, 1.5, 1.5])
array([ 2.5, 3.5, 4.5])
>>> mean(a, axis=0)
# Variance
array([ 2.5, 3.5, 4.5])
>>> a.var(axis=0)
>>> average(a, axis=0)
array([2.25, 2.25, 2.25])
array([ 2.5, 3.5, 4.5])
>>> var(a, axis=0)
# average can also calculate
array([2.25, 2.25, 2.25])
# a weighted average
>>> average(a, weights=[1,2],
... axis=0)
array([ 3., 4., 5.])
Module 3: NumpY

NumPy – Array Operations


SIMPLE ARRAY MATH MATH FUNCTIONS

>>> a = np.array([1,2,3,4]) # Create array from 0 to 10


>>> b = np.array([2,3,4,5]) >>> x = np.arange(11.)
>>> a + b # multiply entire array by scalar value
array([3, 5, 7, 9]) >>> a = (2*pi)/10.
>>> a
0.62831853071795862
>>> a*x
array([ 0.,0.628,…,6.283])
# inplace operations
>>> x *= a
>>> x
array([ 0.,0.628,…,6.283])
# apply functions to array.
>>> y = sin(x)
Module 3: NumpY

NumPy – Array Operations


a + b → np.add(a,b) a * b → np.multiply(a,b)
a - b → np.subtract(a,b) a / b → np.divide(a,b)
a % b → np.remainder(a,b) a ** b → np.power(a,b)
MULTIPLY BY A SCALAR ADDITION USING AN OPERATOR FUNCTION

>>> a = np.array([1,2]) >>> np.add(a,b)


>>> a*3. array([4, 6])
array([3., 6.])
ELEMENT BY ELEMENT ADDITION

>>> a = array([1,2])
>>> b = array([3,4])
>>> a + b
array([4, 6])
Module 3: NumpY

NumPy – Array Operations


np.equal (==) np.not_equal (!=) np.greater (>)
np.greater_equal (>=) np.less (<) np.less_equal (<=)
np.logical_and np.logical_or np.logical_xor
np.logical_not

>>> a = np.array([[1,2,3,4],[2,3,4,5]])
>>> b = np.array([[1,2,5,4],[1,3,4,5]])
>>> a == b
array([[True, True, False, True],
[False, True, True, True]])
# functional equivalent
>>> np.equal(a,b)
array([[True, True, False, True],
[False, True, True, True]])
Random with NumPy
Module 3: NumpY

NumPy – Random
np.random.randint(low, high=None, size=None, dtype=‘l’)
•Return random integers from low (inclusive) to high (exclusive)
Module 3: NumpY

NumPy – Random
np.random.random(size=None)
•Return random floats in the half-open interval [0.0, 1.0).
•size : int or tuple of ints, optional
Module 3: NumpY

NumPy – Random
numpy.random.choice(1-D-array, size=None, replace=True, p=None)
•Generates a random sample from a given 1-D array
•replace : boolean, optional (Whether the sample is with or without replacement)
Module 3: NumpY

NumPy – Random
np.random.shuffle(array)
• Modify a sequence in-place by shuffling its contents.
THANKS
CREDITS: This presentation template was created by
Slidesgo, including icons by Flaticon, and Name: Nguyễn Lê Chí Bảo
infographics & images by Freepik and illustrations Email: bao2101998@gmail.com
by Storyset Phone: 091 544 2420
Linkedin: nguyenlechibao

You might also like