Sie sind auf Seite 1von 40

Numpy

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

Python does numerical computations 1000 x 1000 matrix multiply


slowly.
Python triple loop takes > 10 min.
Numpy takes ~0.03 seconds
Data type Description
bool_ Boolean (True or False) stored as a byte
Default integer type (same as C long; normally
int_
either int64 or int32)
intc Identical to C int (normally int32 or int64)
Integer used for indexing (same as C ssize_t; normally
intp
either int32 or int64)
int8 Byte (-128 to 127)

Numpy datatypes int16


int32
Integer (-32768 to 32767)
Integer (-2147483648 to 2147483647)
The numerical dtypes are int64 Integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
named the same way: uint16 Unsigned integer (0 to 65535)
a type name, uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
like float or int, followed float_ Shorthand for float64.
by a number indicating the float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
number of bits per element float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa

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

Arrays Shaping and Mathematical Indexing and Broadcasting


transposition Operations slicing The term
broadcasting
describes how numpy
treats arrays with
different shapes
during arithmetic
https://docs.scipy.org/doc/numpy-1.15.0/user/basics.broadcasting.html
operations
Example of Broadcast
NumPy operations are usually done on pairs of arrays on an element-by-
element basis. In the simplest case, the two arrays must have exactly the
same shape, as in the following example:
>>> a = np.array([1.0, 2.0, 3.0])
>>> b = np.array([2.0, 2.0, 2.0])
>>> a * b array([ 2., 4., 6.])
NumPy’s broadcasting rule relaxes this constraint when the arrays’
shapes meet certain constraints. The simplest broadcasting example
occurs when an array and a scalar value are combined in an operation:
>>> a = np.array([1.0, 2.0, 3.0])
>>> b = 2.0
>>> a * b array([ 2., 4., 6.])
Arrays 8

Structured lists of numbers.   


 Vectors
 Matrices
 Images
 Tensors
 ConvNets
Arrays 9

Structured lists of numbers.


 Vectors
 Matrices
 Images
 Tensors
 ConvNets
Arrays 10

Structured lists of numbers.


 Vectors
 Matrices
 Images
 Tensors
 ConvNets
There are a couple of mechanisms for creating arrays in
NumPy:
• Conversion from other Python structures (e.g., lists,
tuples).

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.ones, np.zeros, np.diag


 np.arange
 np.concatenate
 np.astype
 np.zeros_like, np.ones_like
 np.random.random

>>> np.diag((3,7,8))
array([[3, 0, 0],
[0, 7, 0],
[0, 0, 8]])
Arrays, creation 14

 np.ones, np.zeros, np.diag


 np.arange
 np.concatenate
 np.astype
 np.zeros_like, np.ones_like
 np.random.random
Arrays, creation 15

 np.ones, np.zeros, np.diag


 np.arange
 np.concatenate
 np.astype
 np.zeros_like, np.ones_like
 np.random.random
Arrays, creation 16

 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.diag


 np.arange
 np.concatenate
 np.astype
 np.zeros_like, np.ones_like
 np.random.random
Arrays, creation 18

 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

 Must be dense, no holes.


 Must be one type
 Cannot combine arrays of different shape
indexing
 Single-dimension indexing is accomplished as usual.
>>> x = np.arange(10)
>>> x[2]
2
>>> x[-2]
8 0 1 2 3 4 5 6 7 8 9

 Multi-dimensional arrays support multi-dimensional indexing.

>>> x.shape = (2,5) # now x is 2-dimensional


>>> x[1,3]
8 0 1 2 3 4 5
>>> x[1,-1] 6 7 8 9
9
22

x[0,0] # top-left element


Indexing x[0,-1] # first row, last column
x[0,:] # first row (many entries)
x[:,0] # first column (many entries)
Notes:
 Zero-indexing
 Multi-dimensional indices are comma-separated
indexing
 Using fewer dimensions to index will result in a subarray.
>>> x[0]
array([0, 1, 2, 3, 4])

 This means that x[i, j] == x[i][j] but the second method is


less efficient.
SLICING
>>> x = np.arange(10)

#from start to 4th position #return elements at even place


>>> x[:5] >>> x[: : 2]
array([0, 1, 2, 3, 4]) array([0, 2, 4, 6, 8])

#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])

#from 4th to 6th position #reverse the array


>>> x[4:7] >>> x[::-1]
array([4, 5, 6]) array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
SUM 25

a.sum() # sum all entries


a.sum(axis=0) # sum over rows in one column
a.sum(axis=1) # sum over columns in one rows
a.sum(axis=1, keepdims=True)
1. Use the axis parameter to control which axis NumPy operates on
2. Typically, the axis specified will disappear, keepdims keeps all
dimensions
Array operations
>>> a = np.arange(5)  Basic operations apply element-wise. The
>>> b = np.arange(5) result is a new array with the resultant
>>> a+b elements.
array([0, 2, 4, 6, 8])
>>> a-b
array([0, 0, 0, 0, 0]) Operations like *= and += will modify the
>>> a**2 existing array.
array([ 0,  1,  4,  9, 16])
>>> a>3
array([False, False, False, False,  True], dtype=bool)
>>> 10*np.sin(a)
array([ 0., 8.41470985, 9.09297427, 1.41120008,
-7.56802495])
>>> a*b
array([ 0,  1,  4,  9, 16])
>>> a = np.ones(4).reshape(2,2)
>>> a
Array operations array([[ 1.,  1.],
       [ 1.,  1.]])
>>> a[1,0] = 1
 Since multiplication is done
>>> I = np.identity(2)
element-wise, you need to >>> I
specifically perform a dot array([[ 1.,  0.],
product to perform matrix        [ 0.,  1.]])
>>> I[1,0] = 0
multiplication.
>>> b = np.arange(4).reshape(2,2)
>>> b
array([[0, 1],
       [2, 3]])
>>> a*b
array([[ 0.,  1.],
       [ 2.,  3.]])
>>> np.dot(a,b)
array([[ 2.,  4.],
       [ 2.,  4.]])
• For array, '*' means element-wise multiplication, and the dot() function is used for matrix multiplication.
 There are also some built-in
methods of ndarray objects.

Universal functions which


may also be applied
Array include exp, sqrt, add, sin,
cos, etc…
operations
>>> a = np.random.random((2,3))
>>> a
array([[ 0.68166391, 0.98943098, 0.69361582],
[ 0.78888081, 0.62197125, 0.40517936]])
>>> a.sum()
4.1807421388722164
>>> a.min()
0.4051793610379143
>>> a.max(axis=0)
array([ 0.78888081, 0.98943098, 0.69361582])
>>> a.min(axis=1)
array([ 0.68166391, 0.40517936])
Array operations
>>> a = np.floor(10*np.random.random((3,4)))
>>> print a
[[ 9. 8. 7. 9.]
 An array shape can be [ 7. 5. 9. 7.]
manipulated by a number of [ 8. 2. 7. 5.]]
methods. >>> a.shape
(3, 4)
>>> a.ravel()
resize(size) will modify an array([ 9., 8., 7., 9., 7., 5., 9., 7., 8., 2., 7., 5.])
array in place. >>> a.shape = (6,2)
>>> print a
[[ 9. 8.]
reshape(size) will return a [ 7. 9.]
copy of the array with a new [ 7. 5.]
shape. [ 9. 7.]
[ 8. 2.]
[ 7. 5.]]
>>> a.transpose()
array([[ 9., 7., 7., 9., 8., 7.],
[ 8., 9., 5., 7., 2., 5.]])
Shaping 31

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 *

Linear algebra >>> from numpy.linalg import *


>>> a = array([[1.0, 2.0], [3.0, 4.0]])
>>> print a
[[ 1. 2.]
[ 3. 4.]]
>>> a.transpose()
 One of the most common reasons for array([[ 1., 3.],
using the NumPy package is its linear [ 2., 4.]])
algebra module. >>> inv(a) # inverse
array([[-2. , 1. ],
[ 1.5, -0.5]])
>>> u = eye(2) # unit 2x2 matrix; "eye"
represents "I"
>>> u
array([[ 1., 0.],
[ 0., 1.]])
>>> j = array([[0.0, -1.0], [1.0, 0.0]])
>>> dot(j, j) # perkalian matrix
array([[-1., 0.],
[ 0., -1.]])
>>> trace(u) # trace
2.0
Linear algebra: Solving systems of equations with numpy

A x = b where

We start by constructing the arrays for A and b.

To solve the system we do


>>> A = matrix('1.0 2.0; 3.0 4.0')
>>> A

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

Das könnte Ihnen auch gefallen