About iPython Notebooks

iPython Notebooks are interactive coding environments embedded in a webpage. After writing your code, you can run the cell by either pressing "SHIFT"+"ENTER" or by clicking on "Run Cell" (denoted by a play symbol) in the upper bar of the notebook.


In [1]:
test = "Hello World"

In [2]:
print ("test: " + test)


test: Hello World

NumPy

NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • useful linear algebra, Fourier transform, and random number capabilities

Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases.

Library documentation: http://www.numpy.org/

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank.

For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3. In the example pictured below, the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.

NumPy’s array class is called ndarray. It is also known by the alias array. Note that numpy.array is not the same as the Standard Python Library class array.array, which only handles one-dimensional arrays and offers less functionality. The more important attributes of an ndarray object are:

np.array

list as argument to array


In [ ]:
#

shape

np.zeros


In [3]:
#The function zeros creates an array full of zeros

np.ones


In [4]:
# function ones creates an array full of ones

np.empty


In [5]:
#function empty creates an array whose initial content is random and depends on the state of the memory

np.arange


In [6]:
# To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.

np.linspace

When arange is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function linspace that receives as an argument the number of elements that we want,instead of the step

a1 = np.random.random((2,3))

random() returns float values from 0 to 1

print(a1) a1+=3 print(a1)

b1 = np.random.rand(2,3) *100

same as random but random takes

only one argument ie.shape in form of a tuple

while np.random.rand() takes shape directly - rand(2,4,5)

print(b1)

randint()

c1 = np.random.randint(0,5,size=(2,3))

random integers from 0 to 4 with shape(2,3)

print(c1)

randn() Return a sample (or samples)

from the “standard normal” distribution.

r1 = np.random.randn(500)

Printing Arrays

When you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:

  • the last axis is printed from left to right,
  • the second-to-last is printed from top to bottom,
  • the rest are also printed from top to bottom, with each slice separated from the next by an empty line.

Indexing, Slicing and Iterating

One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.

The dots (...) represent as many colons as needed to produce a complete indexing tuple. For example, if x is a rank 5 array (i.e., it has 5 axes), then

  • x[1,2,...] is equivalent to x[1,2,:,:,:],
  • x[...,3] to x[:,:,:,:,3] and
  • x[4,...,5,:] to x[4,:,:,5,:].

Shape Manipulation

Changing the shape of an array

An array has a shape given by the number of elements along each axis

  • a.ravel
  • a.reshape
  • a.T

np.vstack((a,b))

np.hstack((a,b))


In [ ]: