Introduction to NumPy Maryam Tavakol Machine Learning Group Winter - - PowerPoint PPT Presentation

introduction to numpy
SMART_READER_LITE
LIVE PREVIEW

Introduction to NumPy Maryam Tavakol Machine Learning Group Winter - - PowerPoint PPT Presentation

Introduction to NumPy Maryam Tavakol Machine Learning Group Winter semester 2016/17 1 What is NumPy? Short for Numerical Python Wikipedia : NumPy is an extension to the Python programming language, adding support for large,


slide-1
SLIDE 1

Introduction to NumPy

Maryam Tavakol

Machine Learning Group

1

Winter semester 2016/17

slide-2
SLIDE 2

What is NumPy?

  • Short for Numerical Python
  • Wikipedia: NumPy is an extension to the Python

programming language, adding support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to

  • perate on these arrays

2

slide-3
SLIDE 3

Properties

  • A fast and efficient multidimensional array object

ndarray

  • Functions for performing element-wise

computations with arrays or mathematical

  • perations between arrays
  • Tools for reading and writing array-based data sets

to disk

3

slide-4
SLIDE 4

Properties

  • Linear algebra operations, Fourier transform, and

random number generation

  • Tools for integrating connecting C, C++, and

Fortran code to Python

4

slide-5
SLIDE 5

Areas of Functionality

  • Fast vectorized array operations for data munging

and cleaning, subsetting and filtering, transformation, and any other kinds of computations

  • Common array algorithms like sorting, unique, and

set operations

  • Efficient descriptive statistics and aggregating/

summarizing data

5

slide-6
SLIDE 6

Areas of Functionality

  • Data alignment and relational data manipulations

for merging and joining together heterogeneous data sets

  • Expressing conditional logic as array expressions

instead of loops with if-elif-else branches

  • Group
  • wise data manipulations (aggregation,

transformation, function application)

6

slide-7
SLIDE 7

ndarrays

  • At the core of the NumPy package, is the ndarray
  • bject which encapsulates n-dimensional arrays of

homogeneous data.

  • Many operations performed using ndarray objects

execute in compiled code for performance

  • The standard scientific packages use ndarray

7

slide-8
SLIDE 8

Creating ndarray

  • The easiest way to create an array is to use the

array function

8

slide-9
SLIDE 9

Creating ndarray

  • Nested sequences, like a list of equal-length lists,

will be converted into a multidimensional array

9

slide-10
SLIDE 10

Creating ndarray

  • In addition to np.array, there are a number of
  • ther functions for creating new arrays

10

slide-11
SLIDE 11

Creating ndarray

  • It is not safe to assume that np.empty will return

an array of all zeros

11

slide-12
SLIDE 12

Functions

12

slide-13
SLIDE 13

Functions (cont.)

13

slide-14
SLIDE 14

Example

14

slide-15
SLIDE 15

Data Types

  • The data type or dtype is a special object

containing the information the ndarray needs to interpret

15

slide-16
SLIDE 16

Data Types

16

slide-17
SLIDE 17

Data Types

17

slide-18
SLIDE 18

Data Types

  • You can convert or cast an array from one dtype to

another using ndarray’s astype method

18

slide-19
SLIDE 19

Array Attributes

19

slide-20
SLIDE 20

Operations on Arrays

  • Any arithmetic operations between equal-size

arrays applies the operation element-wise

20

slide-21
SLIDE 21

Operations on Arrays

  • Arithmetic operations with scalars are as you would

expect, propagating the value to each element

21

slide-22
SLIDE 22

Example

22

slide-23
SLIDE 23

Indexing & Slicing

23

slide-24
SLIDE 24

Indexing & Slicing

  • Indexing on a 2D array

24

slide-25
SLIDE 25

Indexing & Slicing

25

slide-26
SLIDE 26

Example

26

slide-27
SLIDE 27

More Example

27

slide-28
SLIDE 28

Boolean Indexing

28

slide-29
SLIDE 29

Boolean Indexing

  • To select all the rows with corresponding name

“Bob”

29

slide-30
SLIDE 30

Boolean Indexing

  • You can mix that with other indexing

30

slide-31
SLIDE 31

Fancy Indexing

  • A way of indexing using integer arrays

31

slide-32
SLIDE 32

Transposing Arrays

  • Transposing is a special form of reshaping which

similarly returns a view on the underlying data without copying anything: arr.T

32

slide-33
SLIDE 33

Transposing Arrays

  • When doing matrix computations, you will do this

very often, like for example computing the inner matrix product using np.dot

33

slide-34
SLIDE 34

Universal Functions

  • A function that performs element-wise operations
  • n data in ndarrays

34

slide-35
SLIDE 35

Universal Functions

  • Unary functions: take one argument
  • Binary functions: take 2 arrays and return a single

array as the result

35

slide-36
SLIDE 36

Unary Functions

36

slide-37
SLIDE 37

Unary Functions

37

slide-38
SLIDE 38

Binary Functions

38

slide-39
SLIDE 39

Binary Functions

39

slide-40
SLIDE 40

Data Processing

  • Expressing many kinds of data processing tasks as

concise array expressions rather than writing loops

40

slide-41
SLIDE 41

Conditional Logic

41

slide-42
SLIDE 42

Statistical Methods

42

slide-43
SLIDE 43

Array I/O

43

slide-44
SLIDE 44

Other Methods

  • Boolean arrays
  • Sorting
  • Set Operations
  • Linear Algebra
  • Matrix multiplication, decompositions,

determinants, and other square matrix math

44

slide-45
SLIDE 45

Linear Algebra

45