numpy meetup 07/02/2013

7

Click here to load reader

Upload: francesco

Post on 08-Jul-2015

178 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Numpy Meetup 07/02/2013

Data manipulation with Numpy

Francesco Benincasa – 07/02/2013

Page 2: Numpy Meetup 07/02/2013

1. Basics

● N-dimensional arrays (ndarray) creation:● From another python structure (tuple, list, …)● Numpy funcions (arange, zeros, empty, …)● From text (or csv) files (loadtxt, genfromtxt, ...)

Page 3: Numpy Meetup 07/02/2013

2a. Array objects

● Indexing

>> x=np.arange(10).reshape(2,5)

>> x[1][2]=x[1,2]

>> x.repeat(2).reshape(10,2)

● Slicing

>> x[0,0:5:2]

array([0, 2, 4])

>> x[:,0]

array([0, 5])

Page 4: Numpy Meetup 07/02/2013

2b. Array objects

● Masks

>> x[x>5]

array([6, 7, 8, 9])

>> print np.ma.masked_where(x<5, x)

[[-- -- -- -- –][5 6 7 8 9]]

● Ignoring extreme values

>> np.ma.masked_outside(x,1,8)

masked_array(data =

[[-- 1 2 3 4]

[5 6 7 8 --]],

mask =

[[ True False False False False]

[False False False False True]],

fill_value = 999999)

Page 5: Numpy Meetup 07/02/2013

3. NetCDF files

● Pupynere: available into scipy library or stand-alone

● Read/Write● Handle

● Variables● Dimensions● Attributes

(example1)

Page 6: Numpy Meetup 07/02/2013

Visualization – 4. Maps + Contours

● Read NetCDF data>> sc = f.variables['SCONC_DUST'].getValue()

● Create grid:>> x, y = meshgrid(lons, lats)

● Draw contour for each timestep:>> for i in range(sc.shape[0]):

>> m.contourf(x, y, sc[i], cmap=cm.YlOrBr)

(example 2)

Page 7: Numpy Meetup 07/02/2013

References

● Numpy reference:

http://docs.scipy.org/doc/numpy/reference/

● NetCDF APIs:

https://bitbucket.org/robertodealmeida/pupynere/

http://gfesuite.noaa.gov/developer/netCDFPythonInterface.html

● Matplotlib (docs, examples …):

http://matplotlib.sourceforge.net/

Thank you!Thank you!