In [1]:
import numpy as np

Arrays

In [2]:
# numpy array
arr = np.array([0,1,2,3])
arr
Out[2]:
array([0, 1, 2, 3])

numpy calc is faster than native python

In [3]:
# testing the same

L = range(100000)
%timeit [i**2 for i in L]
10 loops, best of 3: 55.7 ms per loop
In [4]:
L = np.arange(100000)
%timeit L**2
The slowest run took 4.17 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 98.8 ┬Ás per loop

there are so many ways to create array in numpy, we will see them one by one

starting with 1-D array

In [5]:
a = np.array([1,2,5,3,5,9])
a
Out[5]:
array([1, 2, 5, 3, 5, 9])
In [6]:
a = np.arange(1, 20)    # start, end
a
Out[6]:
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19])
In [7]:
a = np.arange(1, 20, 3)   # start, end, step
a
Out[7]:
array([ 1,  4,  7, 10, 13, 16, 19])
In [8]:
# check dimension and size
print("dimension - ", a.ndim)
print("size - ", a.shape)
print("length - ", len(a))
dimension -  1
size -  (7,)
length -  7

2-D array

In [9]:
b = np.array([[1,2,3], [2,3,4], [3,4,5]])
b
Out[9]:
array([[1, 2, 3],
       [2, 3, 4],
       [3, 4, 5]])
In [10]:
# check dimension and size
print("dimension - ", b.ndim)
print("size - ", b.shape)
print("length - ", len(b))
dimension -  2
size -  (3, 3)
length -  3

3-D array

In [11]:
c = np.array([[[5,7], [8,9], [0,7]], [[1,2], [2,3], [6,7]], [[5,7], [8,9], [0,7]]])
c
Out[11]:
array([[[5, 7],
        [8, 9],
        [0, 7]],

       [[1, 2],
        [2, 3],
        [6, 7]],

       [[5, 7],
        [8, 9],
        [0, 7]]])
In [12]:
# check dimension and size
print("dimension - ", c.ndim)
print("size - ", c.shape)
print("length - ", len(c))
dimension -  3
size -  (3, 3, 2)
length -  3

Simplest way to create test array

In [13]:
# by no
a = np.arange(1, 10, 2)
print(a)
print(a.shape)
[1 3 5 7 9]
(5,)
In [14]:
# by number of points
a = np.linspace(1, 10, 4)  # pick any 4 no from 1 to 10
print(a)
print(a.shape)
[  1.   4.   7.  10.]
(4,)
In [15]:
# by number of points
a = np.linspace(1, 10, 6, endpoint=False)  # pick any 4 no from 1 to 10
print(a)
print(a.shape)
[ 1.   2.5  4.   5.5  7.   8.5]
(6,)

Common arrays to use :

In [16]:
np.ones(4)
Out[16]:
array([ 1.,  1.,  1.,  1.])
In [17]:
np.ones((2,4))
Out[17]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
In [18]:
np.zeros((2,3))
Out[18]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [19]:
np.eye((3))
Out[19]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
In [20]:
np.diag([1,2,3,4])
Out[20]:
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

Random no generation

with help of numpy random library

In [21]:
np.random.rand(5)   # pick any 5 value between 0 and 1
Out[21]:
array([ 0.77109549,  0.92437055,  0.13376104,  0.00209447,  0.99861705])
In [22]:
np.random.randn(5)  # includes negative no as well (Gaussian)
Out[22]:
array([-0.26465512,  0.76965644,  0.61422423,  2.54456552, -0.99713591])
In [23]:
np.random.randint(9)  # this will pick any random no from 0 to 8 (integer)
Out[23]:
1
In [24]:
np.random.randint(10, 13)  # start, end    end exclusive
Out[24]:
11
In [25]:
np.random.randint(10, 13, 2)  # start, end, no of element    end exclusive
Out[25]:
array([11, 11])
In [26]:
np.random.randint(10, 13, 4)  # start, end, no of element    end exclusive
Out[26]:
array([11, 10, 10, 10])
In [27]:
np.random.randint(1, 20, (4, 3))
Out[27]:
array([[19, 11, 11],
       [18,  5, 17],
       [ 5, 13, 13],
       [15, 19, 16]])

If you set the np.random.seed(a_fixed_number) every time you call the numpy's other random function, the result will be the same:

In [28]:
np.random.seed(123)
np.random.randint(1, 10, 2)
Out[28]:
array([3, 3])
In [29]:
np.random.seed(123)
np.random.randint(1, 10, 2)
Out[29]:
array([3, 3])

Data Type

In [30]:
a = np.random.randint(3,9, 3)
print(a)
print(a.dtype)
[4 6 5]
int32
In [31]:
a = np.random.rand(3)
print(a)
print(a.dtype)
[ 0.42310646  0.9807642   0.68482974]
float64

You can explicitly specify which data-type you want:

In [32]:
c = np.array([1, 2, 3], dtype=float)
c.dtype
Out[32]:
dtype('float64')
In [33]:
c = np.array([1, 2, 3])
c.dtype
Out[33]:
dtype('int32')
In [34]:
a = np.ones((3, 3))
a.dtype
Out[34]:
dtype('float64')
In [35]:
# complex data type
d = np.array([1+2j, 3+4j, 5+6*1j])
d.dtype
Out[35]:
dtype('complex128')
In [36]:
# Boolean
e = np.array([True, False, False, True])
e.dtype
Out[36]:
dtype('bool')
In [37]:
# String
f = np.array(['Bonjour', 'Hello', 'Hallo'])
f.dtype  # unicode string with max 7 letters
Out[37]:
dtype('<U7')

Indexing and Slicing

In [38]:
a = np.arange(10)
print(a)

a[2], a[0], a[8], a[-1]
[0 1 2 3 4 5 6 7 8 9]
Out[38]:
(2, 0, 8, 9)
In [39]:
a[2:7]
Out[39]:
array([2, 3, 4, 5, 6])
In [40]:
# reversing the array
a[::-1]
Out[40]:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
In [41]:
a[1:8:3]  #start, end, step
Out[41]:
array([1, 4, 7])
In [42]:
a[:4]  #start, 4
Out[42]:
array([0, 1, 2, 3])
In [43]:
a[6:]  #6, end
Out[43]:
array([6, 7, 8, 9])
In [44]:
a[:]  #start, end
Out[44]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

numpy array supports all python Indexing and Slicing operation

Numpy indexing and Slicing

In [45]:
a = np.arange(10)
a
Out[45]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [46]:
a[5:] = 11
a
Out[46]:
array([ 0,  1,  2,  3,  4, 11, 11, 11, 11, 11])
In [47]:
a[6:] = a[::-1][6:]
a
Out[47]:
array([ 0,  1,  2,  3,  4, 11,  3,  2,  1,  0])

Array Copies and Views

In [48]:
a = np.arange(10)
a
Out[48]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [49]:
b = a[6:]
b
Out[49]:
array([6, 7, 8, 9])
In [50]:
a[7] =  13
print(a)
print(b)
[ 0  1  2  3  4  5  6 13  8  9]
[ 6 13  8  9]
In [51]:
print(np.may_share_memory(a,b))
True

Copy

In [52]:
b = a[6:].copy()
b
Out[52]:
array([ 6, 13,  8,  9])
In [53]:
a[8] =  16
print(a)
print(b)
[ 0  1  2  3  4  5  6 13 16  9]
[ 6 13  8  9]
In [54]:
print(np.may_share_memory(a,b))
False

Boolean indexing or Fancy indexing

In [55]:
a = np.arange(10)
a
Out[55]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [56]:
b = a%3==0
b   # boolean array
Out[56]:
array([ True, False, False,  True, False, False,  True, False, False,  True], dtype=bool)
In [57]:
# picking only those for which a%3==0 is TRUE
a[b]   # boolean indexing
Out[57]:
array([0, 3, 6, 9])
In [58]:
a[b] = a[b]+200
a
Out[58]:
array([200,   1,   2, 203,   4,   5, 206,   7,   8, 209])
In [59]:
b = np.random.randint(0, 6, 3)
b
Out[59]:
array([1, 0, 0])
In [60]:
a[b]
Out[60]:
array([  1, 200, 200])