introduction to numpy
play

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,


  1. Introduction to NumPy Maryam Tavakol Machine Learning Group Winter semester 2016/17 1

  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 operate on these arrays 2

  3. Properties • A fast and efficient multidimensional array object ndarray • Functions for performing element-wise computations with arrays or mathematical operations between arrays • Tools for reading and writing array-based data sets to disk 3

  4. Properties • Linear algebra operations, Fourier transform, and random number generation • Tools for integrating connecting C, C++, and Fortran code to Python 4

  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

  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

  7. ndarrays • At the core of the NumPy package, is the ndarray object 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

  8. Creating ndarray • The easiest way to create an array is to use the array function 8

  9. Creating ndarray • Nested sequences, like a list of equal-length lists, will be converted into a multidimensional array 9

  10. Creating ndarray • In addition to np.array , there are a number of other functions for creating new arrays 10

  11. Creating ndarray • It is not safe to assume that np.empty will return an array of all zeros 11

  12. Functions 12

  13. Functions (cont.) 13

  14. Example 14

  15. Data Types • The data type or dtype is a special object containing the information the ndarray needs to interpret 15

  16. Data Types 16

  17. Data Types 17

  18. Data Types • You can convert or cast an array from one dtype to another using ndarray’s astype method 18

  19. Array Attributes 19

  20. Operations on Arrays • Any arithmetic operations between equal-size arrays applies the operation element-wise 20

  21. Operations on Arrays • Arithmetic operations with scalars are as you would expect, propagating the value to each element 21

  22. Example 22

  23. Indexing & Slicing 23

  24. Indexing & Slicing • Indexing on a 2D array 24

  25. Indexing & Slicing 25

  26. Example 26

  27. More Example 27

  28. Boolean Indexing 28

  29. Boolean Indexing • To select all the rows with corresponding name “Bob” 29

  30. Boolean Indexing • You can mix that with other indexing 30

  31. Fancy Indexing • A way of indexing using integer arrays 31

  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

  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

  34. Universal Functions • A function that performs element-wise operations on data in ndarrays 34

  35. Universal Functions • Unary functions: take one argument • Binary functions: take 2 arrays and return a single array as the result 35

  36. Unary Functions 36

  37. Unary Functions 37

  38. Binary Functions 38

  39. Binary Functions 39

  40. Data Processing • Expressing many kinds of data processing tasks as concise array expressions rather than writing loops 40

  41. Conditional Logic 41

  42. Statistical Methods 42

  43. Array I/O 43

  44. Other Methods • Boolean arrays • Sorting • Set Operations • Linear Algebra • Matrix multiplication, decompositions, determinants, and other square matrix math 44

  45. Linear Algebra 45

Recommend


More recommend