Numpy Basic (Part 1)
This session will cover numpy, this is specially useful since the images are going to be represented as numpy arrays most of the time, here the numpy documentation
The Basics¶
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 NumPy's class is call ndarray also know by its alias array, but make sure not confuse it with the Standard Python library class array.array, in this case the NumPy is numpy.array, the Standard python array only handles one-dimensional arrays and offers less functionality.
the Important objects¶
The most importation objects for the ndarray:
ndarray.ndim: the number of axis (dimensions) of the arrayndarray.shape: this is a tuple that indicate the size of the array in each direction, for example, shape of (m,n) will be a matrix with n rows and m columns, the length of the tuple is the numbers of axis,ndim.ndarray.size: total number of elements in the array, this is equal to the product of the elements in the shape.ndarray.dtype: an object describing the type of the elements in the array.ndarray.itemsize: the size in bytes of each element on the array.ndarray.data:the buffer that contain the actual elements of the array, normally is not use since we access the elements using the indexing facilities
The code Example¶
Numpy arrays¶
The Creation or a NumPy array¶
you can create a NumPy array with a simple python list
now, if you useA frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument.
np.array in a sequence of sequence, example [(1,2,3), (4,5,6)] you will get a two-dimension array
Creation Functions¶
Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation
Create array of zeros with zeros Function¶
The function zeros will create an array and will be use as placeholder 0. this means a float 0, at least you specify the dtype
Syntax
shape is optional, instead we can use just the tuple (n,m), and the dtype if it is not specify it will use the float.
Create array of ones with ones Function¶
The function ones in the same way that zeros will create and array but instead of use 0 it will use the placeholder 1.
Syntax
shape is optional, instead we can use just the tuple (n,m), and the dtype if it is not specify it will use the float.
Create array of random numbers with empty Function¶
This will create an array with random numbers, the numbers will depend of the state of the memory in that moment
Create sequence of numbers (arange,linspace)¶
To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.
Syntax
Although when we want to be completely sure of the number o elements that we want we can use linspace
Syntax
Numpy Useful methods for data manipulation¶
Here will be a couple of operation that will be useful in some cases
Random numbers¶
To start the generation of random numbers we can start by creating a seed
the 101 can be different in this case we use his to keep constant the numbers with the course followed
Get the number¶
Syntax
Find the max and min and it location¶
It is always useful to find the min and the max values of the array and where they are, their index
Find max value and its index¶
assuming the array is:
then
Syntax
Find the min and its index¶
assuming the array is:
then
Syntax
Average value¶
assuming the array is:
then
Syntax