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, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays 2
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
Properties • Linear algebra operations, Fourier transform, and random number generation • Tools for integrating connecting C, C++, and Fortran code to Python 4
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
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
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
Creating ndarray • The easiest way to create an array is to use the array function 8
Creating ndarray • Nested sequences, like a list of equal-length lists, will be converted into a multidimensional array 9
Creating ndarray • In addition to np.array , there are a number of other functions for creating new arrays 10
Creating ndarray • It is not safe to assume that np.empty will return an array of all zeros 11
Functions 12
Functions (cont.) 13
Example 14
Data Types • The data type or dtype is a special object containing the information the ndarray needs to interpret 15
Data Types 16
Data Types 17
Data Types • You can convert or cast an array from one dtype to another using ndarray’s astype method 18
Array Attributes 19
Operations on Arrays • Any arithmetic operations between equal-size arrays applies the operation element-wise 20
Operations on Arrays • Arithmetic operations with scalars are as you would expect, propagating the value to each element 21
Example 22
Indexing & Slicing 23
Indexing & Slicing • Indexing on a 2D array 24
Indexing & Slicing 25
Example 26
More Example 27
Boolean Indexing 28
Boolean Indexing • To select all the rows with corresponding name “Bob” 29
Boolean Indexing • You can mix that with other indexing 30
Fancy Indexing • A way of indexing using integer arrays 31
Transposing Arrays • Transposing is a special form of reshaping which similarly returns a view on the underlying data without copying anything: arr.T 32
Transposing Arrays • When doing matrix computations, you will do this very often, like for example computing the inner matrix product using np.dot 33
Universal Functions • A function that performs element-wise operations on data in ndarrays 34
Universal Functions • Unary functions: take one argument • Binary functions: take 2 arrays and return a single array as the result 35
Unary Functions 36
Unary Functions 37
Binary Functions 38
Binary Functions 39
Data Processing • Expressing many kinds of data processing tasks as concise array expressions rather than writing loops 40
Conditional Logic 41
Statistical Methods 42
Array I/O 43
Other Methods • Boolean arrays • Sorting • Set Operations • Linear Algebra • Matrix multiplication, decompositions, determinants, and other square matrix math 44
Linear Algebra 45
Recommend
More recommend