python mini-course university of oklahoma department of psychology lesson 21 numpy 6/11/09 python...

16
Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Upload: ira-lambert

Post on 14-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Python Mini-CourseUniversity of Oklahoma

Department of Psychology

Lesson 21NumPy

6/11/09Python Mini-Course: Lesson 211

Page 2: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Lesson objectives

1. Use the NumPy package

6/11/09Python Mini-Course: Lesson 212

Page 3: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

What is NumPy?

NumPy is the fundamental package needed for scientific computing with Python. It contains:

a powerful N-dimensional array object basic linear algebra functions basic Fourier transforms sophisticated random number capabilities tools for integrating Fortran codetools for integrating C/C++ code

6/11/09Python Mini-Course: Lesson 213

Page 4: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

NumPy documentation

Official documentationhttp://docs.scipy.org/doc/

The NumPy bookhttp://www.tramy.us/numpybook.pdf

Example listhttp://www.scipy.org/Numpy_Example_Lis

t_With_Doc

6/11/09Python Mini-Course: Lesson 214

Page 5: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

The ndarray data structure

NumPy adds a new data structure to Python – the ndarrayAn N-dimensional array is a

homogeneous collection of “items” indexed using N integers

Defined by:1. the shape of the array, and2. the kind of item the array is composed of

6/11/09Python Mini-Course: Lesson 215

Page 6: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Array shape

ndarrays are rectangularThe shape of the array is a tuple of N integers (one for each dimension)

6/11/09Python Mini-Course: Lesson 216

Page 7: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Array item types

Every ndarray is a homogeneous collection of exactly the same data-typeevery item takes up the same size block of memory

each block of memory in the array is interpreted in exactly the same way

6/11/09Python Mini-Course: Lesson 217

Page 8: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

6/11/09Python Mini-Course: Lesson 218

Page 9: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

6/11/09Python Mini-Course: Lesson 219

Page 10: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Example: creating an array

import numpya = array([[1,2,3], [4,5,6], [7,8,9]])a.shapea.dtype

6/11/09Python Mini-Course: Lesson 2110

Page 11: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Indexing arrays

Use a tuple to index multi-dimensional arrays

Example:a[1,2]

6/11/09Python Mini-Course: Lesson 2111

Page 12: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Slicing arrays

Slicing arrays is almost the same as slicing lists, except you can specify multiple dimensions

6/11/09Python Mini-Course: Lesson 2112

Page 13: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Examples: Slicing arrays

a[1]a[1,:]a[1,1:]a[:1,1:]

6/11/09Python Mini-Course: Lesson 2113

Page 14: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Some ndarray methods

ndarray. tolist ()The contents of self as a nested list

ndarray. copy ()Return a copy of the array

ndarray. fill (scalar)Fill an array with the scalar value

6/11/09Python Mini-Course: Lesson 2114

Page 15: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Some NumPy functions

abs()add()binomial()cumprod()cumsum()floor()histogram()

min()max()multipy()polyfit()randint()shuffle()transpose()

6/11/09Python Mini-Course: Lesson 2115

Page 16: Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1

Suggested exercise

Complete the desc_stat_calc.py program

6/11/09Python Mini-Course: Lesson 2116