Python Numbers

NumPy is the fundamental package for scientific computing with Python, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

It contains among other things:

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.

Example1:

Result

2-Dimension Array:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
Array shape:
(3, 5)
Array Dimension:
2
Array DataType:
int32
Item Size:
4
Array Size:
15
Array Type:
class 'numpy.ndarray
Simle numpy array
[6 7 8]
class 'numpy.ndarray'

Ex2: Aaray,reshape,zeros and ones Numpy Arrays

Result

[1 2 3]
 
[[1 2 3 4 5 6]]
 
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
 
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
 
[1 2 3 7 8]
 
[1 2 3 1 3]

Some numpy function example given below : array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, arange, linspace and sin

Result
Python Numpy
Python Numpy

Printing Arrays

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

One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.

Basic Operations

Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.

Result

Array Sum
[20 30 40 50] + [1 2 3 4] = [21 32 43 54]
Array substraction
[20 30 40 50] - [1 2 3 4] = [19 28 37 46]
Array Multification
[20 30 40 50] * [1 2 3 4] = [ 20  60 120 200]
Array Dividion
[20 30 40 50] / [1 2 3 4] = [20.    15.   13.33333333     12.5  ]
b array`s Power of 2
 [ 1  4  9 16]
[ 9.12945251 -9.88031624  7.4511316  -2.62374854]

Unlike in many matrix languages, the product operator * operates elementwise in NumPy arrays. The matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method:

Upcasting

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).

Result

float64
[1.         2.57079633 4.14159265]
[ 0.54030231+0.84147098j -0.84147098+0.54030231j -0.54030231-0.84147098j]
 
 [[0.25011412 0.93300594 0.31369793]
 [0.83932737 0.07229013 0.90281802]]
Sum: 3.3112535140008434
Min: 0.07229013472145596
Max: 0.9330059392809487
 
 [[ 0  1  2  3]
 [  5 4  6  7]
 [ 8  9 10 11]]
Sum: 66
Min: 0
Max: 11
 
sum of each column [12 15 18 21]
min of each row [0 4 8]
cumulative sum along each row [[ 0  1  3  6]
 [ 4  9 15 22]
 [ 8 17 27 38]]
cumulative sum along each colunm [[ 0  1  2  3]
 [ 4  6  8 10]
 [12 15 18 21]]

By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array:

Universal Functions

NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(ufunc). Within NumPy, these functions operate elementwise on an array, producing an array as output.

Result

[0 1 2]
[1.         2.71828183 7.3890561 ]
[0.         1.         1.41421356]

Indexing, Slicing and Iterating

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

Result

[  0   1   8  27  64 125 216 343 512 729]
 
8
 
[ 8 27 64]
 
[-1000     1 -1000    27 -1000   125   216   343   512   729]
 
8.999999999999998
7.999999999999999
6.999999999999999
5.999999999999999
4.999999999999999
nan
3.0
nan
1.0
nan

Multidimensional arrays

Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:

Result

[[ 0  1  2  3]
 [10 11 12 13]
 [20 21 22 23]
 [30 31 32 33]
 [40 41 42 43]]
 
23
 
[ 1 11 21 31 41]
 
[ 1 11 21 31 41]
 
[[10 11 12 13]
 [20 21 22 23]]

You can look at the following example to see how to use boolean indexing to generate an image of the Mandelbrot set:

Result
Python Numpy

The second way of indexing with booleans is more similar to integer indexing; for each dimension of the array we give a 1D boolean array selecting the slices we want:

Result

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
 
[[ 4 5 6 7]
[ 8 9 10 11]]
 
[[ 4 5 6 7]
[ 8 9 10 11]]
 
[[ 0 2]
[ 4 6]
[ 8 10]]
 
[ 4 10]

The ix_() function

The ix_ function can be used to combine different vectors so as to obtain the result for each n-uplet. For example, if you want to compute all the a+b*c for all the triplets taken from each of the vectors a, b and c:

Result

[[[2]]
[[3]]
[[4]]
[[5]]]
 
[[[8]
  [5]
  [4]]]
 
[[[5 4 6 8 3]]]
 
(4, 1, 1) (1, 3, 1) (1, 1, 5)
 
[[[42 34 50 66 26]
  [27 22 32 42 17]
  [22 18 26 34 14]]
 
 [[43 35 51 67 27]
  [28 23 33 43 18]
  [23 19 27 35 15]]
 
 [[44 36 52 68 28]
  [29 24 34 44 19]
  [24 20 28 36 16]]
 
 [[45 37 53 69 29]
  [30 25 35 45 20]
  [25 21 29 37 17]]]
 
17
 
17

You could also implement the reduce as follows:

Result

[[[15 14 16 18 13]
  [12 11 13 15 10]
  [11 10 12 14  9]]
 
 [[16 15 17 19 14]
  [13 12 14 16 11]
  [12 11 13 15 10]]
 
 [[17 16 18 20 15]
  [14 13 15 17 12]
  [13 12 14 16 11]]
 
 [[18 17 19 21 16]
  [15 14 16 18 13]
  [14 13 15 17 12]]]

The advantage of this version of reduce compared to the normal ufunc.reduce is that it makes use of the Broadcasting Rules in order to avoid creating an argument array the size of the output times the number of vectors.

Indexing with strings

See Structured arrays

Linear Algebra

Work in progress. Basic linear algebra to be included here.

Simple Array Operations

Result

[[1. 2.]
 [3. 4.]]
[[1. 3.]
 [2. 4.]]
 
[[-2.   1. ]
 [ 1.5 -0.5]]
 
[[1. 0.]
 [0. 1.]]
 
[[-1.  0.]
 [ 0. -1.]]
 
[[-3.]
 [ 4.]]
 
(array([0.+1.j, 0.-1.j]), array([[0.70710678+0.j        , 0.70710678-0.j        ],
       [0.        -0.70710678j, 0.        +0.70710678j]]))
Parameters:
    square matrix
Returns
    The eigenvalues, each repeated according to its multiplicity.
    The normalized (unit "length") eigenvectors, such that the
    column ``v[:,i]`` is the eigenvector corresponding to the
    eigenvalue ``w[i]`` .

Tricks and Tips

Here we give a list of short and useful tips.

"Automatic" Reshaping

To change the dimensions of an array, you can omit one of the sizes which will then be deduced automatically:

Result

(2, 5, 3)
 
[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]
  [12 13 14]]
 
 [[15 16 17]
  [18 19 20]
  [21 22 23]
  [24 25 26]
  [27 28 29]]]

Vector Stacking

How do we construct a 2D array from a list of equally-sized row vectors? In MATLAB this is quite easy: if x and y are two vectors of the same length you only need do m=[x;y]. In NumPy this works via the functions column_stack, dstack, hstack and vstack, depending on the dimension in which the stacking is to be done. For example:

Result

[0 2 4 6 8]
 
[0 1 2 3 4]
 
[[0 2 4 6 8]
 [0 1 2 3 4]]
 
[0 2 4 6 8 0 1 2 3 4]

Histograms

The NumPy histogram function applied to an array returns a pair of vectors: the histogram of the array and the vector of bins. Beware: matplotlib also has a function to build histograms (called hist, as in Matlab) that differs from the one in NumPy. The main difference is that pylab.hist plots the histogram automatically, while numpy.histogram only generates the data.

Result
Python Histogram
Result
Python Histogram