Numpy:
Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.
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.
For example, the coordinates of a point in 3D space
[1, 2, 1]
has one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.
Example:
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:
1. ndarray.ndim: The number of axes (dimensions) of the array.
2. ndarray.shape : The dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
For a matrix with n rows and m columns,
shape
will be (n,m)
.
The length of the
shape
tuple is therefore the number of axes, ndim
.
3. ndarray.size: The total number of elements of the array. This is equal to the product of the elements of
shape
.
4. 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.
5. 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
.
6. 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.
Example:
ARRAY CREATION:
There are several ways to create arrays.
For example, you can create an array from a regular Python list or tuple using the
array
function. The type of the resulting array is deduced from the type of the elements in the sequences.
Example:
No comments:
Post a Comment