Basic Numpy

Numerical Python Library


In [2]:
import numpy as np

In [4]:
l=[1,2,3,4]
l=np.array(l)
l


Out[4]:
array([1, 2, 3, 4])

In [6]:
a=[1,2,3]
b=[3,4,6]
c=np.array([a,b])

In [10]:
help(np.shape)


Help on function shape in module numpy.core.fromnumeric:

shape(a)
    Return the shape of an array.
    
    Parameters
    ----------
    a : array_like
        Input array.
    
    Returns
    -------
    shape : tuple of ints
        The elements of the shape tuple give the lengths of the
        corresponding array dimensions.
    
    See Also
    --------
    alen
    ndarray.shape : Equivalent array method.
    
    Examples
    --------
    >>> np.shape(np.eye(3))
    (3, 3)
    >>> np.shape([[1, 2]])
    (1, 2)
    >>> np.shape([0])
    (1,)
    >>> np.shape(0)
    ()
    
    >>> a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
    >>> np.shape(a)
    (2,)
    >>> a.shape
    (2,)


In [11]:
c.shape


Out[11]:
(2, 3)

evenly space size using arrange


In [19]:
l=np.arange(2,24,4)
print(l)


[ 2  6 10 14 18 22]

In [21]:
l.reshape(3,2)


Out[21]:
array([[ 2,  6],
       [10, 14],
       [18, 22]])

In [22]:
l.reshape(2,3)


Out[22]:
array([[ 2,  6, 10],
       [14, 18, 22]])

In [26]:
#linspace(a,b,c)
#linspace gives  a list back in range (a,b) with c intervals
o=np.linspace(0,1,9)
print(o)


[ 0.     0.125  0.25   0.375  0.5    0.625  0.75   0.875  1.   ]

In [27]:
o.reshape(3,3)


Out[27]:
array([[ 0.   ,  0.125,  0.25 ],
       [ 0.375,  0.5  ,  0.625],
       [ 0.75 ,  0.875,  1.   ]])

In [30]:
print(np.zeros(3))


[ 0.  0.  0.]

In [31]:
print(np.ones(3))


[ 1.  1.  1.]

In [34]:
print(np.eye(5))


[[ 1.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.]
 [ 0.  0.  1.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  1.]]

In [55]:
np.diag(o.reshape(3,3))


Out[55]:
array([ 0. ,  0.5,  1. ])

In [56]:
np.diag([4,5,6])


Out[56]:
array([[4, 0, 0],
       [0, 5, 0],
       [0, 0, 6]])

In [57]:
np.array([1,2,3]*3)


Out[57]:
array([1, 2, 3, 1, 2, 3, 1, 2, 3])

In [58]:
np.repeat([1,2,3],3)


Out[58]:
array([1, 1, 1, 2, 2, 2, 3, 3, 3])

In [63]:
#Uncomment the below line comment the line below it and see the difference
#a=[1,2,4]
a=np.array([1,2,4])
np.vstack([a ,2*a])


Out[63]:
array([[1, 2, 4],
       [2, 4, 8]])

In [64]:
np.hstack([a ,2*a])


Out[64]:
array([1, 2, 4, 2, 4, 8])

OPerations in numpy


In [91]:
x=np.array([1,2,3])
y=np.array([[1,2,2],[1,2,4]])
y.dot(x)


Out[91]:
array([11, 17])

In [81]:
y


Out[81]:
array([[1, 2, 2],
       [1, 2, 4]])

In [82]:
y.T


Out[82]:
array([[1, 1],
       [2, 2],
       [2, 4]])

In [85]:
##Type of array

x.dtype


Out[85]:
dtype('int64')

In [87]:
##Casting array as another type

x.astype('f')


Out[87]:
array([ 1.,  2.,  3.], dtype=float32)

In [94]:
#some math functions
a=np.array([1,3,4,-6,7,4,6,11,2])
a.sum()


Out[94]:
32

In [95]:
a.min()


Out[95]:
-6

In [96]:
a.mean()


Out[96]:
3.5555555555555554

In [97]:
a.std()


Out[97]:
4.3997755273829622

In [98]:
a.argmax()


Out[98]:
7

Indexiing and slicing


In [101]:
b=np.arange(1,18,2)**2

In [104]:
b[5]


Out[104]:
121

In [122]:
b[1:4]


Out[122]:
array([ 9, 25, 49])

In [136]:
r=np.arange(0,36)
r.resize(6,6)

In [121]:
##some operations
r[r>30]


Out[121]:
array([31, 32, 33, 34, 35])

In [127]:
r[-1,-5:-2]


Out[127]:
array([31, 32, 33])

In [128]:
r[r>30]=30

In [129]:
r


Out[129]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 30, 30, 30, 30, 30]])

In [130]:
r2=r[:,:]

In [135]:
r2


Out[135]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

In [134]:
##Changimg r2 changes r
#Uncommemt thebelow lines to observe it
#r2[:]=0
#r


Out[134]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

In [133]:
#s0 to copy use r copy


Out[133]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

In [141]:
##so to copy we should use rcopy

r_copy=r.copy()
r_copy


Out[141]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

In [143]:
##now changing r copy
r_copy[r_copy>10]=-3
r


Out[143]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

In [144]:
r_copy


Out[144]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, -3],
       [-3, -3, -3, -3, -3, -3],
       [-3, -3, -3, -3, -3, -3],
       [-3, -3, -3, -3, -3, -3],
       [-3, -3, -3, -3, -3, -3]])

In [152]:
r[0,:6]


Out[152]:
array([0, 1, 2, 3, 4, 5])

In [153]:
old = np.array([[1, 1, 1],
                [1, 1, 1]])

new = old
new[0, :2] = 0

print(old)


[[0 0 1]
 [1 1 1]]

Iterate over arrays


In [163]:
rnd=np.random.randint(1,30,(4,3))

In [164]:
rnd


Out[164]:
array([[28,  4, 13],
       [18,  9, 14],
       [16, 10, 26],
       [12, 28, 25]])

In [167]:
for row in range(len(rnd)):
    print(rnd[row])


[28  4 13]
[18  9 14]
[16 10 26]
[12 28 25]

In [168]:
for i , row in enumerate(rnd):
    print(i,"  ",row)


0    [28  4 13]
1    [18  9 14]
2    [16 10 26]
3    [12 28 25]

In [169]:
for i in zip(rnd):
    print(i)


(array([28,  4, 13]),)
(array([18,  9, 14]),)
(array([16, 10, 26]),)
(array([12, 28, 25]),)

In [170]:
for i,j in zip(rnd,rnd**2):
    print(i+j)


[812  20 182]
[342  90 210]
[272 110 702]
[156 812 650]

In [ ]: