Numpy Tutorial
Numpy Tutorial
NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and
a collection of routines for processing those arrays. Using NumPy, mathematical and logical operations
on arrays can be performed. This tutorial explains the basics of NumPy such as its architecture and
environment. It also discusses the various array functions, types of indexing, etc. An introduction to
Matplotlib is also provided. All this is explained with the help of examples for better understanding.
Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray
was also developed, having some additional functionalities. In 2005, Travis Oliphant created
NumPy package by incorporating the features of Numarray into Numeric package. There are
many contributors to this open source project.
NumPy is often used along with packages like SciPy (Scientific Python) and Mat−plotlib
(plotting library). This combination is widely used as a replacement for MatLab, a popular
platform for technical computing. However, Python alternative to MatLab is now seen as a more
modern and complete programming language.
NumPy - Environment
Standard Python distribution doesn't come bundled with NumPy module. A lightweight
alternative is to install NumPy using popular Python package installer, pip.
Windows
Python (x,y): It is a free Python distribution with SciPy stack and Spyder IDE for Windows OS.
(Downloadable from https://www.python-xy.github.io/)
Linux
Package managers of respective Linux distributions are used to install one or more packages in
SciPy stack.
For Ubuntu
sudo apt-get install python-numpy
python-scipy python-matplotlibipythonipythonnotebook python-pandas
python-sympy python-nose
For Fedora
sudo yum install numpyscipy python-matplotlibipython
python-pandas sympy python-nose atlas-devel
Core Python (2.6.x, 2.7.x and 3.2.x onwards) must be installed with distutils and zlib module
should be enabled.
To test whether NumPy module is properly installed, try to import it from Python prompt.
import numpy
import numpy as np
Every item in an ndarray takes the same size of block in the memory. Each element in ndarray is
an object of data-type object (called dtype).
Any item extracted from ndarray object (by slicing) is represented by a Python object of one of
array scalar types. The following diagram shows a relationship between ndarray, data type object
(dtype) and array scalar type −
An instance of ndarray class can be constructed by different array creation routines described
later in the tutorial. The basic ndarray is created using an array function in NumPy as follows −
numpy.array
It creates an ndarray from any object exposing array interface, or from any method that returns
an array.
Example 1
Live Demo
import numpy as np
a = np.array([1,2,3])
print a
[1, 2, 3]
Example 2
Live Demo
[[1, 2]
[3, 4]]
Example 3
Live Demo
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print a
[[1, 2, 3, 4, 5]]
Example 4
Live Demo
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print a
bool_
1
Boolean (True or False) stored as a byte
int_
2
Default integer type (same as C long; normally either int64 or int32)
intc
3
Identical to C int (normally int32 or int64)
intp
4
Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8
5
Byte (-128 to 127)
int16
6
Integer (-32768 to 32767)
int32
7
Integer (-2147483648 to 2147483647)
int64
8
Integer (-9223372036854775808 to 9223372036854775807)
uint8
9
Unsigned integer (0 to 255)
uint16
10
Unsigned integer (0 to 65535)
uint32
11
Unsigned integer (0 to 4294967295)
uint64
12
Unsigned integer (0 to 18446744073709551615)
float_
13
Shorthand for float64
float16
14
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32
15
Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64
16
Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_
17
Shorthand for complex128
complex64
18
Complex number, represented by two 32-bit floats (real and imaginary components)
complex128
19
Complex number, represented by two 64-bit floats (real and imaginary components)
NumPy numerical types are instances of dtype (data-type) objects, each having unique
characteristics. The dtypes are available as np.bool_, np.float32, etc.
A data type object describes interpretation of fixed block of memory corresponding to an array,
depending on the following aspects −
The byte order is decided by prefixing '<' or '>' to data type. '<' means that encoding is little-
endian (least significant is stored in smallest address). '>' means that encoding is big-endian
(most significant byte is stored in smallest address).
Example 1
Live Demo
Example 2
Live Demo
dt = np.dtype('i4')
print dt
int32
Example 3
Live Demo
>i4
The following examples show the use of structured data type. Here, the field name and the
corresponding scalar data type is to be declared.
Example 4
Live Demo
[('age', 'i1')]
Example 5
Live Demo
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a
The output is as follows −
Example 6
Live Demo
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print a['age']
[10 20 30]
Example 7
The following examples define a structured data type called student with a string field 'name', an
integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object.
Live Demo
import numpy as np
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
print student
Example 8
Live Demo
import numpy as np
Each built-in data type has a character code that uniquely identifies it.
'b' − boolean
'i' − (signed) integer
'u' − unsigned integer
'f' − floating-point
'c' − complex-floating point
'm' − timedelta
'M' − datetime
'O' − (Python) objects
'S', 'a' − (byte-)string
'U' − Unicode
'V' − raw data (void)
ndarray.shape
This array attribute returns a tuple consisting of array dimensions. It can also be used to resize
the array.
Example 1
Live Demo
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print a.shape
(2, 3)
Example 2
Live Demo
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print a
[[1, 2]
[3, 4]
[5, 6]]
Example 3
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print b
[[1, 2]
[3, 4]
[5, 6]]
ndarray.ndim
Example 1
Live Demo
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23]
Example 2
Live Demo
# now reshape it
b = a.reshape(2,4,3)
print b
# b is having three dimensions
[[[ 0, 1, 2]
[ 3, 4, 5]
[ 6, 7, 8]
[ 9, 10, 11]]
[[12, 13, 14]
[15, 16, 17]
[18, 19, 20]
[21, 22, 23]]]
numpy.itemsize
This array attribute returns the length of each element of array in bytes.
Example 1
Live Demo
Example 2
Live Demo
numpy.flags
The ndarray object has the following attributes. Its current values are returned by this function.
C_CONTIGUOUS (C)
1
The data is in a single, C-style contiguous segment
F_CONTIGUOUS (F)
2
The data is in a single, Fortran-style contiguous segment
OWNDATA (O)
3
The array owns the memory it uses or borrows it from another object
WRITEABLE (W)
4
The data area can be written to. Setting this to False locks the data, making it read-only
ALIGNED (A)
5
The data and all elements are aligned appropriately for the hardware
UPDATEIFCOPY (U)
6
This array is a copy of some other array. When this array is deallocated, the base array will
be updated with the contents of this array
Example
Live Demo
import numpy as np
x = np.array([1,2,3,4,5])
print x.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False