Primer On Numpy Arrays
NumPy is short for Numerical Python
NumPy object is essentially a homogeneous n-dimensional array
Numpy stores (essentially) numbers in each dimension, all of the same type and indexed to non-negative integers
Numpy dimension has a special name
axes
For example, array[1,2,3,4]
is a 1-D array. It has one-axis and 3 elementsWhy NumPy is so popular in Data Science? Answers lies in the fact that almost all types of data (documents, sound clip, images, etc) is essentially represented by array of numbers
On appearance, Numpy array might look familiar to Python built-in
list
, however, former provides much efficient data storage and operation thanlist
1. CREATING NUMPY ARRAY
We will discuss multiple ways to create an array. But first, letβs us import
the numpy library into the program and as tradition, import
it with alias np
1.1. From the Pythonβs List
a. Upcasting
All array elements should be of same data type (dtype). Therefore, if our provided list
doesnβt have same data type, Numpy will upcast the data type to the next higher one. That is:
If list contains
int
andfloat
dtype, all elements will be up-casted tofloat
(example 1 below)If list contains
int
,float
andstr
dtype, all elements will be up-casted tostr
(example 2 below)
b. Explicitly setting the dtype
We can use the keyword argument (kwarg) dtype
to explicitly set the data type of Numpy Array
1.2. From Ranged Data
We can create Numpy array from np.arange()
function as follows:
a. Single number is passed in the argument
If only single number, n
is passed in np.arange(n)
, then array would be [0,n-1]
, which means 0
is inclusive and n
is exclusive
b. Two numbers passed in the argument
If two numbers are passed np.arange(x,y)
then the array will contain all integers from x
(inclusive) and y
(exclusive)
c. Three numbers are passed in the arguments
If three numbers np.arange(x,y,z)
are passed, array would start from x
(inclusive), ends with y
(exclusive) and z
will define the step size β number of integers to skip. The default value of step size is 1.
1.3. From Linspace function
np.linspace(a,b)
function produces equally spaced elements of array betweena
andb
Unlike
np.arange(a,b)
, innp.linspace(a,b)
, botha
andb
are inclusive in the array, unless we passendpoint=False
as kwargIf kwarg
num
is not specified, the default number of created elements would be 50
1.4. Special Functions
np.zeros(x)
will create an array ofx
zeros
np.ones(x)
will create an array ofx
ones
np.zeros_like(a)
will create new array of same shape asa
but replace all values with0
np.ones_like(a)
will create new array of same shape asa
but replace all values with1
np.full((a,b), x)
will create an array ofa
*b
dimension containing only one number,x
np.eye(x,y)
will create identity matrixnp.random.randomof
x
*y
dimension
1.5. From Random Numbers
There are several ways to create Numpy Array from random numbers
a. np.random.random
np.random.random
outputs an array of random floats of required size and shape between 0.0
and 1.0
b. np.random.randint
np.random.randint
outputs an array of random integers of required size, shape and interval
Setting the RandomState
for reproducibility of the results
c. np.random.normal
np.random.normal
allows us to produce an array of random numbers whose mean and standard deviation dictates a normal distribution
2. RESHAPING THE ARRAY
In section 1 above, we have discussed mutiple ways to create an array. Some of these methods only create a 1D array, however, we can change dimensions to any shape using np.reshape()
function
2.1. np.reshape
We have used np.arange()
to create a 1D array. We can apply .reshape()
to this 1D to reshape it to any shape we want. However, we need to be careful that total elements in 1D and reshaped array are equal. For example, we have 1D array of 10 elements, we can reshape it to (2,5) or (5,2) only:
2.2. np.reshape with -1
In the above examples, we knew the number of elements in the original 1D array, and we explicitly provided new shape in np.reshape()
However, there is special value -1
which can be used to infer the shape of the array. It infers the value of dimension, where it is provided in either (x,-1)
or (-1,y)
Examples, will make the concept clearer:
2.3. flatten method
We can reshape any nD array into 1D using .flatten
method
2.4. Transpose
We all know what transpose feature does in Excel, here we will achieve the same result using np.transpose()
function
a. Simple Transpose
In simple transpose, we are changing rows into column, and vice versa.
b. Transpose with axes kwarg
Keyword argument axes
takes in a list/tuples of integers and permute the axes according to the given values. For example, if original axes are (3,4,2) and we provide axes=(1,2,0)
, this will make:
the original first dimension, new third dimension,
original second dimension, new first dimension, and,
original third dimension, new second dimension.
Actually, the default value of axes
kwarg in transpose function is (2,1,0)
which essentially reversing the order.
3. ARRAY ATTRIBUTES
There are various methods to fetch variety of attributes of a NumPy array. Few, we have already demonstrated, others are discussed in this section
.ndim
to get the number of dimensions
.shape
to get the actual size of each dimensions of an array, returned in the form of tuple
.dtype
to get the data type associated with the array
.size
to get total size of an array, which is essentially total no of elements in array
.itemsize
to get the size of each array element in bytes
.nbytes
to get total size of the array, essentially equal tosize * itemsize
4. OTHER BASICS
To copy an array, we use
np.copy(array_to_copy)
method. This is very important, if we donβt want to modify the original array but need a copy for some other tasks
To cast the array into a specific data type, we use
.astype
method
There are two special number types,
np.nan
andnp.inf
whose presence in an array will cast the whole array asfloat
data type
Last updated
Was this helpful?