Python Day 7
Python Day 7
What is NumPy
NumPy is an extension to the Python programming language, adding support for large,
multi-dimensional arrays and matrix's, along with a large library of high-level
mathematical functions to operate on these arrays.
NumPy is the fundamental library needed for scientific computing with Python.
This contains:
N-dimensional array object
Array slicing methods
Array reshaping methods
Numerical routines in numpy:
Linear algebra functions
Fourier transform
Random number capabilities
Why NumPy
Hence we need efficient arrays with arithmetic and better multidimensional tools
NumPy package provide arrays which are similar to lists, but much more
capable, except fixed size
NumPy - ndarray
[9, 1, -1] An array of rank 1 i.e. A matrix with 1 row and columns
[ [ 10, 0.21, -30], An array of rank 2 ( A matrix with 2 rows and 3 columns)
[ 1.9, 7.4, 1.9] ]
Numpy – ndarray Attributes
ndarray.ndim
Gives dimensiona (ran of axes) of the array
ndarray.shape
For a matrix with n rows and m columns, shape will be tuple (n,m). The length of the
shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements
of shape.
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify
dtype’s using standard Python types. Additionally NumPy provides types of its own.
numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of
type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4
(=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use
this attribute because we will access the elements in an array using indexing facilities.
Numpy - Array Creation
Using functions that are dedicated to generating numpy arrays, such as arange,
ones, zeros linspace, etc.
In general, any numerical data that is stored in an array-like container can be converted
to an ndarray through use of the array() function. The most obvious examples are
sequence types like lists and tuples.
There are a couple of built-in NumPy functions which will create arrays from scratch.
• zeros(shape) -- creates an array filled with 0 values with the specified shape. The
default dtype is float64.
• linspace() -- creates arrays with a specified number of elements, and spaced equally
between the specified beginning and end values.
• eye(): Return a 2-D array with ones on the diagonal and zeros elsewhere.
Numpy Methods
sort()
argsort()
transpose()
invert()
dot()
var()
std()
THANK YOU!!