4.1 - NumPy Basics: Part 1

Ha Khanh Nguyen (hknguyen)


1. What is NumPy?


2. NumPy ndarray

2.1 Creating ndarray

Function Description
array Convert input data (list, tuple, array, or other sequence type) to an ndarray either by inferring a dtype or explicitly specifying a dtype; copies the input data by default
asarray Convert input to ndarray, but do not copy if the input is already an ndarray
arange Like the built-in range but returns an ndarray instead of a list
ones Produce an array of all 1s with the given shape and dtype
ones_like Take another array and produces a 1s array of the same shape and dtype
zeros Like ones producing arrays of 0s instead
zeros_like Like ones_like producing arrays of 0s instead
empty Create new arrays by allocating new memory, but do not populate with any values like ones and zeros
empty_like Take another array and produces a new array of the same shape and dtype, but do not populate it with values
full Produce an array of the given shape and dtype with all values set to the indicated “fill value”
full_like Take another array and produces a filled array of the same shape and dtype
eye, identity Create a square NxN identify matrix (1s on the diagonal and 0s elsewhere)

2.2 Arithmetic with NumPy Arrays


This lecture note is modified from Chapter 4 of Wes McKinney's Python for Data Analysis 2nd Ed.