Beruflich Dokumente
Kultur Dokumente
NUMERICAL PYTHON
What is numpy (NUMERICAL PYTHON)
A POWERFUL N- SOPHISTICATED TOOLS FOR INTEGRATING USEFUL LINEAR ALGEBRA, FAST NUMERICAL
DIMENSIONAL ARRAY (BROADCASTING/UNIVERS C/C++ AND FORTRAN FOURIER TRANSFORM, COMPUTATIONS
OBJECT. AL) FUNCTIONS. CODE. AND RANDOM NUMBER
CAPABILITIES.
3
Why do we need NumPy
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_ Shorthand for complex128.
Complex number, represented by two 32-bit floats (real and
complex64
imaginary components)
Complex number, represented by two 64-bit floats (real and
complex128
imaginary components)
>>> import numpy as np
>>> x = np.float32(1.0)
>>> x
1.0
>>> y = np.int_([1,2,4])
Example of >>> y
array([1, 2, 4])
Numpy
datatypes >>> z = np.arange(3, dtype=np.uint8)
>>> z
array([0, 1, 2], dtype=uint8)
>>> z.dtype
dtype('uint8')
6
NumPy Overview
Numpy Arrays • Built-in NumPy array creation (e.g., arange, ones, zeros,
etc.).
• Reading arrays from disk, either from standard or
custom formats (e.g. reading in from a CSV file).
• and others …
Arrays, creation 12
np.array
np.ones, np.zeros, np.diag
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
Arrays, creation 13
>>> np.diag((3,7,8))
array([[3, 0, 0],
[0, 7, 0],
[0, 0, 8]])
Arrays, creation 14
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
Arrays, creation 17
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
Arrays, creation 19
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
Arrays, danger zone 20
#from 4th position to end #return elements from first position step by two
>>> x[4:] >>> x[1::2]
array([4, 5, 6, 7, 8, 9]) array([1, 3, 5, 7, 9])
a = np.array([1,2,3,4,5,6])
a = a.reshape(3,2)
a = a.reshape(2,-1)
a = a.ravel()
1. Total number of elements cannot change.
2. Use -1 to infer axis shape
3. Row-major by default (MATLAB is column-major)
>>> from numpy import *
A x = b where
Matrices [[ 1. 2.]
[ 3. 4.]]
>>> type(A)
<class 'numpy.matrixlib.defmatrix.matrix'>
There is also a matrix class which >>> A.T # transpose
inherits from the ndarray class. [[ 1. 3.]
[ 2. 4.]]
There are some slight differences but >>> X = matrix('5.0 7.0')
>>> Y = X.T
matrices are very similar to general
>>> A.I # inverse
arrays. [[-2. 1. ]
[ 1.5 -0.5]]
In NumPy’s own words, the question of >>> solve(A, Y) # solving linear equation
whether to use arrays or matrices comes matrix([[-3.], [ 4.]])
down to the short answer of “use arrays”. >>> A*Y # matrix multiplication
[[19.]
[43.]]
>>> multiply(A,Y) # matrix elemen wise
multiplication
Matrix([[ 5. 10.]
[ 21. 28.]])
For matrix, '*'means matrix multiplication, and the multiply()
function is used for element-wise multiplication
Numpy Statistics
numpy.median()
numpy.percentile()
numpy.mean()
Standard Deviation: np.std()
Variance : np.var()
numpy.median()
numpy.percentile(a,q,axis)
a Input array
q The percentile to compute must be between 0-100
Axis The axis along which the percentile is to be calculated
Exercise 1. Solving a linear system
Solve the linear system Ax = b using Python numpy
Equation 1: 2*x1 - x2 + x3 = -1
Equation 2: 3*x1 + 3*x2 + 9*x3 = 0
Equation 3: 3*x1 + 3*x2 + 5*x3 = 4
Exercise 2.
Extract semua bilangan ganjil dari
>>> arr = np.arange(10)
Exercise 3
Generate array X berdimensi 2 berukuran 4 x 5 dengan nilai elemen antara 0 dan 1
Tentukan persentil ke 80
Tentukan median tiap baris