Skip to main content

Numpy Arrays : A quick tutorial !

Numpy is one of the most popular Python library used in Data Science and Scientific computing. Numpy's main object is the homogeneous multidimensional array, which is a table of elements (usually numbers) of same type, indexed by a tuple of positive integers.

Creating new Arrays ( array function)
New Arrays can be created by using array function. This function takes list as an argument and create N-Dimensional array based on the arguments.

import numpy as np
# One Dimensional Array
ar1 = np.array([1,2,3])
# Two Dimensional Array
ar2 = np.array([1,2,3], [4,5,6])

Other methods to create new Arrays 
There are several other ways to create Arrays :-

arange([start], [stop], [step]
This is similar to Python range function and creates evenly spaced arrays.
np.arange(5)
# OUT : array([0, 1, 2, 3, 4])

np.arange(2,10,2) # Excludes the stop position element
# OUT : array([2, 4, 6, 8]) 

linspace([start], [stop], [num]
Creates array by number of points.
np.linspace(0,6,3)
# Creates 3 evenly spaced elements between start and stop point. 
# Note, stop point value is included. 
# OUT : array([0., 3., 6.])

np.linspace(0,6,3,endpoint=False) # Excludes the stop position element
#OUT : array([0., 2., 4.])

Important Attributes for Numpy Arrays 
Below are the commonly used attributes for Numpy Arrays


nparray.dim    : Number of axes (or dimensions) of the array.
nparray.shape : Returns size of array in each dimension as tuple. Length of shape tuple is the number of axes (dimensions).
nparray.size    : Total number of elements in array, which is also the product of elements of shape.
nparray.dtype  : Type of elements in an array, represented by object (numpy.int16, numpy.int32, numpy.float64 etc..)

Commonly used Arrrays

zeros(shape, dtype=float) 
Takes shape as an argument with optional dtype argument to define the type of elements of array and returns array of zeros.
np.zeros((2,3))
# OUT : array([[0., 0., 0.],
#              [0., 0., 0.]])

np.zeros((2,3), dtype=int)
# OUT : array([[0, 0, 0],
#              [0, 0, 0]])

np.zeros(3, dtype=int)
# OUT : array([0, 0, 0])

ones(shape, dtype=float) 
Takes shape as an argument with optional dtype argument to define the type of elements of array and returns array of ones.
np.ones((2,3))
# OUT : array([[1., 1., 1.],
#              [1., 1., 1.]])

np.ones((2,3), dtype=int)
# OUT : array([[1, 1, 1],
#              [1, 1, 1]])

np.ones(3, dtype=int)
# OUT : array([1, 1, 1])

eye() 
Takes number of rows as an argument with other optional arguments. This will return an array with diagonal elements as 1 and rest as 0.
np.eye(2)
# OUT : array([[1., 0.],
#              [0., 1.]])

diag(v,k=0) 
Creates or Extracts a diagonal array
x = np.diag(np.array(2,3,4))
# OUT : array([[2, 0, 0],
#              [0, 3, 0],
#              [0, 0, 4]])

np.diag(x)
# OUT : array([2,3,4])

random(d1,d2,d3...dn) 
Creates an array with random elements
x = np.random.rand(3,2)
# OUT : array([[0.2223, 0.1213],
#              [0.4324, 0.2232],
#              [0.6662, 0.2323]])

Shape Operations

reshape(array, newshape) 
Takes an array as an argument with newshape (integer or tuple). The newshape should be compatible with existing shape.
np.arange(6).reshape((2,3))
# OUT : array([[0, 1, 2],
#              [3, 4, 5]])

ravel(array) 
Takes an array as an argument and returns a flattened contiguous array (1-D).
x = np.arange(6).reshape((2,3))
# OUT : array([[0, 1, 2],
#              [3, 4, 5]])
np.ravel(x)
# OUT : array([0, 1, 2, 3, 4, 5])

Indexing

x = np.arange(0,30,4)
# OUT : array([0, 4, 8, 12, 16, 20, 24, 28])

x[0] # => 0
x[1] # => 4
x[[0, 3]] # => array([0, 12])
x[[0, 3]] = 2

# OUT : array([2, 4, 8, 2, 16, 20, 24, 28])

Comments