Using Numpy in Python

Hello all,

This is the third article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles

  1. Python for Data Science Series - Part 1
  2. Python for Data Science Series - Part 2


To install numpy in your python environment, please use the below command

pip install numpy


Please refer the video below for detailed explanation on Numpy


After you have installed numpy, please refer the following notebook to understand on how to use Numpy functionalities.




In [1]:
import numpy as np

Defining a Numpy Array

In [2]:
a = np.array([10,20,30])
In [3]:
a
Out[3]:
array([10, 20, 30])
In [4]:
type(a)
Out[4]:
numpy.ndarray
In [ ]:
dir(a)
In [6]:
a.shape
Out[6]:
(3,)
In [7]:
a = np.array([[1,2,4],[5,6,7]])
In [8]:
a.shape
Out[8]:
(2, 3)
In [9]:
a
Out[9]:
array([[1, 2, 4],
       [5, 6, 7]])
In [10]:
a = np.array([[[1,2,4],[5,6,7]],[[1,2,10],[8,4,2]]])
In [11]:
a.shape
Out[11]:
(2, 2, 3)
In [12]:
a
Out[12]:
array([[[ 1,  2,  4],
        [ 5,  6,  7]],

       [[ 1,  2, 10],
        [ 8,  4,  2]]])
In [13]:
type(a)
Out[13]:
numpy.ndarray
In [14]:
a.dtype
Out[14]:
dtype('int32')
In [15]:
b = a.T
b
Out[15]:
array([[[ 1,  1],
        [ 5,  8]],

       [[ 2,  2],
        [ 6,  4]],

       [[ 4, 10],
        [ 7,  2]]])
In [16]:
b.shape
Out[16]:
(3, 2, 2)
In [17]:
a = np.array([[1,2],[3,4]])
In [18]:
a
Out[18]:
array([[1, 2],
       [3, 4]])
In [19]:
a.shape
Out[19]:
(2, 2)
In [20]:
b = a.T
b
Out[20]:
array([[1, 3],
       [2, 4]])
In [21]:
a = np.array([1,103.34,34])
In [22]:
a.dtype
Out[22]:
dtype('float64')

MIN MAX ARGMIN ARGMAX

In [23]:
max(a)
Out[23]:
103.34
In [24]:
b
Out[24]:
array([[1, 3],
       [2, 4]])
In [25]:
np.max(b)
Out[25]:
4
In [26]:
np.argmax(b)
Out[26]:
3
In [27]:
a = np.array([1,2,3,4])
In [28]:
np.argmax(a)
Out[28]:
3

Generating Arrays

In [31]:
np.ones(shape=(2,3))
Out[31]:
array([[1., 1., 1.],
       [1., 1., 1.]])
In [32]:
np.zeros(shape=(3,3))
Out[32]:
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
In [40]:
np.eye(3)
Out[40]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
In [44]:
np.random.sample(4)
Out[44]:
array([0.07908825, 1.83392063, 5.07344989, 1.90522311])
In [1]:
a = list(range(0,100))
In [58]:
np.random.choice
Out[58]:
<function RandomState.choice>
In [61]:
np.random.sample((5,5))
Out[61]:
array([[0.67240121, 0.03435943, 0.0911954 , 0.22860258, 0.72933351],
       [0.20351709, 0.76852415, 0.47024114, 0.97237468, 0.18860797],
       [0.94120166, 0.12416461, 0.04580034, 0.15012586, 0.67994007],
       [0.82487477, 0.08379525, 0.10066431, 0.96580346, 0.62604015],
       [0.51248987, 0.81711833, 0.67205623, 0.42366449, 0.59213291]])

Matirx Operations

In [62]:
a = np.array([[1,2],[3,4]])
a
Out[62]:
array([[1, 2],
       [3, 4]])
In [63]:
b = np.array([[5,6],[7,8]])
b
Out[63]:
array([[5, 6],
       [7, 8]])
In [64]:
a + b
Out[64]:
array([[ 6,  8],
       [10, 12]])
In [65]:
a * b
Out[65]:
array([[ 5, 12],
       [21, 32]])
In [66]:
np.matmul(a,b)
Out[66]:
array([[19, 22],
       [43, 50]])
In [67]:
np.add(a,b)
Out[67]:
array([[ 6,  8],
       [10, 12]])
In [71]:
np.subtract(a,b)
Out[71]:
array([[-4, -4],
       [-4, -4]])
In [72]:
a
Out[72]:
array([[1, 2],
       [3, 4]])
In [73]:
a.T
Out[73]:
array([[1, 3],
       [2, 4]])
In [83]:
a = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,3],[14,15,16],[17,18,19]]])
In [84]:
a
Out[84]:
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[11, 12,  3],
        [14, 15, 16],
        [17, 18, 19]]])
In [85]:
a.shape
Out[85]:
(2, 3, 3)

Slicing Arrays

In [89]:
a[1][0][0]
Out[89]:
11
In [103]:
a[1,0,:]
Out[103]:
array([11, 12,  3])

Transpose Arrays

In [107]:
b = np.transpose(a)
b
Out[107]:
array([[[ 1, 11],
        [ 4, 14],
        [ 7, 17]],

       [[ 2, 12],
        [ 5, 15],
        [ 8, 18]],

       [[ 3,  3],
        [ 6, 16],
        [ 9, 19]]])
In [108]:
b.shape
Out[108]:
(3, 3, 2)
In [115]:
b[2,:,1]
Out[115]:
array([ 3, 16, 19])
In [118]:
a = np.array([[[1,2,3,100],[4,5,6,200],[7,8,9,300]],[[11,12,3,400],[14,15,16,500],[17,18,19,600]]])
a
Out[118]:
array([[[  1,   2,   3, 100],
        [  4,   5,   6, 200],
        [  7,   8,   9, 300]],

       [[ 11,  12,   3, 400],
        [ 14,  15,  16, 500],
        [ 17,  18,  19, 600]]])
In [119]:
a.shape
Out[119]:
(2, 3, 4)
In [122]:
b = np.transpose(a)
b
Out[122]:
array([[[  1,  11],
        [  4,  14],
        [  7,  17]],

       [[  2,  12],
        [  5,  15],
        [  8,  18]],

       [[  3,   3],
        [  6,  16],
        [  9,  19]],

       [[100, 400],
        [200, 500],
        [300, 600]]])
In [123]:
b.shape
Out[123]:
(4, 3, 2)
In [126]:
b = np.transpose(a,axes=(0,2,1))
b
Out[126]:
array([[[  1,   4,   7],
        [  2,   5,   8],
        [  3,   6,   9],
        [100, 200, 300]],

       [[ 11,  14,  17],
        [ 12,  15,  18],
        [  3,  16,  19],
        [400, 500, 600]]])
In [127]:
b.shape
Out[127]:
(2, 4, 3)

Concatenation of Arrays

In [128]:
a = [1,2,3,4]
In [131]:
np.asarray(a)
Out[131]:
array([1, 2, 3, 4])
In [136]:
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
In [137]:
np.append(a,b)
Out[137]:
array([1, 2, 3, 4, 5, 6, 7, 8])
In [140]:
c = np.hstack((a,b))
c
Out[140]:
array([1, 2, 3, 4, 5, 6, 7, 8])
In [141]:
d = np.vstack((a,b))
d
Out[141]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
In [142]:
c.shape
Out[142]:
(8,)
In [143]:
d.shape
Out[143]:
(2, 4)
In [148]:
a = np.random.random((2,3))
print(a.shape)
a
(2, 3)
Out[148]:
array([[0.89779757, 0.70373201, 0.55522516],
       [0.76259986, 0.15352072, 0.28630461]])
In [149]:
b = np.random.random((2,3))
print(b.shape)
b
(2, 3)
Out[149]:
array([[0.94099365, 0.11215319, 0.0237094 ],
       [0.82909191, 0.00599765, 0.683048  ]])
In [150]:
c = np.hstack((a,b))
print(c.shape)
c
(2, 6)
Out[150]:
array([[0.89779757, 0.70373201, 0.55522516, 0.94099365, 0.11215319,
        0.0237094 ],
       [0.76259986, 0.15352072, 0.28630461, 0.82909191, 0.00599765,
        0.683048  ]])
In [151]:
d = np.vstack((a,b))
print(d.shape)
d
(4, 3)
Out[151]:
array([[0.89779757, 0.70373201, 0.55522516],
       [0.76259986, 0.15352072, 0.28630461],
       [0.94099365, 0.11215319, 0.0237094 ],
       [0.82909191, 0.00599765, 0.683048  ]])
In [ ]:
 
In [ ]:
 
In [152]:
a = np.random.random((2,3,4))
print(a.shape)
a
(2, 3, 4)
Out[152]:
array([[[0.05348472, 0.38735588, 0.07242871, 0.37705306],
        [0.10991526, 0.68119907, 0.700878  , 0.81107797],
        [0.0624925 , 0.99382657, 0.82772526, 0.61071547]],

       [[0.9282276 , 0.42039079, 0.94785463, 0.69754789],
        [0.41982185, 0.0625692 , 0.4286561 , 0.69704102],
        [0.92536563, 0.51611501, 0.23019467, 0.7254887 ]]])
In [153]:
b = np.random.random((2,3,4))
print(b.shape)
b
(2, 3, 4)
Out[153]:
array([[[0.51753157, 0.12818528, 0.37229487, 0.01687935],
        [0.00573693, 0.80945614, 0.5601282 , 0.88981313],
        [0.49319538, 0.92671174, 0.96977514, 0.41044506]],

       [[0.553852  , 0.08528831, 0.63869774, 0.13570784],
        [0.40396164, 0.24848851, 0.67898245, 0.86057556],
        [0.28326273, 0.59721677, 0.45035408, 0.05016073]]])
In [154]:
c = np.hstack((a,b))
print(c.shape)
c
(2, 6, 4)
Out[154]:
array([[[0.05348472, 0.38735588, 0.07242871, 0.37705306],
        [0.10991526, 0.68119907, 0.700878  , 0.81107797],
        [0.0624925 , 0.99382657, 0.82772526, 0.61071547],
        [0.51753157, 0.12818528, 0.37229487, 0.01687935],
        [0.00573693, 0.80945614, 0.5601282 , 0.88981313],
        [0.49319538, 0.92671174, 0.96977514, 0.41044506]],

       [[0.9282276 , 0.42039079, 0.94785463, 0.69754789],
        [0.41982185, 0.0625692 , 0.4286561 , 0.69704102],
        [0.92536563, 0.51611501, 0.23019467, 0.7254887 ],
        [0.553852  , 0.08528831, 0.63869774, 0.13570784],
        [0.40396164, 0.24848851, 0.67898245, 0.86057556],
        [0.28326273, 0.59721677, 0.45035408, 0.05016073]]])
In [155]:
d = np.vstack((a,b))
print(d.shape)
d
(4, 3, 4)
Out[155]:
array([[[0.05348472, 0.38735588, 0.07242871, 0.37705306],
        [0.10991526, 0.68119907, 0.700878  , 0.81107797],
        [0.0624925 , 0.99382657, 0.82772526, 0.61071547]],

       [[0.9282276 , 0.42039079, 0.94785463, 0.69754789],
        [0.41982185, 0.0625692 , 0.4286561 , 0.69704102],
        [0.92536563, 0.51611501, 0.23019467, 0.7254887 ]],

       [[0.51753157, 0.12818528, 0.37229487, 0.01687935],
        [0.00573693, 0.80945614, 0.5601282 , 0.88981313],
        [0.49319538, 0.92671174, 0.96977514, 0.41044506]],

       [[0.553852  , 0.08528831, 0.63869774, 0.13570784],
        [0.40396164, 0.24848851, 0.67898245, 0.86057556],
        [0.28326273, 0.59721677, 0.45035408, 0.05016073]]])
In [162]:
e = np.random.random((1,3,4))
print(e.shape)
e
(1, 3, 4)
Out[162]:
array([[[0.4394013 , 0.72769784, 0.92034209, 0.92738419],
        [0.94154974, 0.7619238 , 0.29673614, 0.67078156],
        [0.25698888, 0.44332544, 0.99029697, 0.01074971]]])
In [163]:
f = np.vstack((d,e))
print(f.shape)
f
(5, 3, 4)
Out[163]:
array([[[0.05348472, 0.38735588, 0.07242871, 0.37705306],
        [0.10991526, 0.68119907, 0.700878  , 0.81107797],
        [0.0624925 , 0.99382657, 0.82772526, 0.61071547]],

       [[0.9282276 , 0.42039079, 0.94785463, 0.69754789],
        [0.41982185, 0.0625692 , 0.4286561 , 0.69704102],
        [0.92536563, 0.51611501, 0.23019467, 0.7254887 ]],

       [[0.51753157, 0.12818528, 0.37229487, 0.01687935],
        [0.00573693, 0.80945614, 0.5601282 , 0.88981313],
        [0.49319538, 0.92671174, 0.96977514, 0.41044506]],

       [[0.553852  , 0.08528831, 0.63869774, 0.13570784],
        [0.40396164, 0.24848851, 0.67898245, 0.86057556],
        [0.28326273, 0.59721677, 0.45035408, 0.05016073]],

       [[0.4394013 , 0.72769784, 0.92034209, 0.92738419],
        [0.94154974, 0.7619238 , 0.29673614, 0.67078156],
        [0.25698888, 0.44332544, 0.99029697, 0.01074971]]])
In [ ]:
 

Comments

Popular posts from this blog

Project: Implementation of Label Editor

Using Pandas in Python