NUMPY Tutorial

https://docs.scipy.org/doc/numpy-dev/, https://www.python-course.eu/numpy.php/

What is NUMPY?

NumPy is an acronym for "Numeric Python" or "Numerical Python" A python package that supports the creation and manipulation of n-dimensional arrays (ndarray) of homogeneous data types. Numpy objects are fixed at creation, adjusting the size creates a new object. The objects in the array must all have the same type ( You can defeat this somewhat in that if you create numbpy array of lists, the lists themselves can contain different objects)

This means that mathematical operations can happen quickly since the size and type of the array elements are fixed. Numbpy was designed for using large arrays and matrices. SciPy (Scientific Python) extends the use of the numby ndarray's with advanced fucntions: minimization, regression, Fourier-transformation, ...

Setting up

If you already have python and pip installed the use:

pip install numpy scipy

Or follow the intstructions from http://www.numpy.org/ or https://docs.scipy.org/doc/numpy-dev/user/index.html#user

Using NUMBPY

Import it to use it


In [1]:
#import numbpy
import numpy as np
# One dimensional array
cvalues = [25.3, 24.8, 26.9, 23.9]
c=np.array(cvalues)
print (c)


[ 25.3  24.8  26.9  23.9]

In [6]:
# Assume they are celsius measurements that we want to  convert to Fahrenheit
# List expression evaluation
print "list expression result="+str([ x*9/5+32 for x in cvalues] )
print "numbpy broadcast result="+str(c*9/5+32)  # simpler syntax


list expression result=[77.54, 76.64, 80.42, 75.02]
numbpy broadcast result=[ 77.54  76.64  80.42  75.02]

Creating ranges using NUMBPY

Python ranges range([start], stop[, step]) vs xrange Renames xrange() to range() and wraps existing range() calls with list

np.arange([start,] stop[, step,], dtype=None) the stop value is not include in the numbers returned. Default step is one, the dtype is based on the start number format unless specified in dtype.


In [12]:
range(1,15,2)  # operates on integers only


Out[12]:
[1, 3, 5, 7, 9, 11, 13]

In [ ]:


In [8]:
np.arange(-1.5,15,3.1415)  # can create any sequence of numbers


Out[8]:
array([ -1.5   ,   1.6415,   4.783 ,   7.9245,  11.066 ,  14.2075])

In [25]:
np.arange((1.0-2.5j),50,(1-4j))  #Even complex number sequences


Out[25]:
array([ 1. -2.5j,  2. -6.5j,  3.-10.5j])

NUMPY Universal functions (ufunc)

a. broadcasting
b. output type
c. internal buffers
d. error handling
e. casting rules
f. overriding ufunc
g. avalable ufuncs

NUMPY Routines Routines

a. 

In [ ]: