import numpy as np
# numpy array
arr = np.array([0,1,2,3])
arr
array([0, 1, 2, 3])
numpy calc is faster than native python
# testing the same
L = range(100000)
%timeit [i**2 for i in L]
10 loops, best of 3: 55.7 ms per loop
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
a = np.array([1,2,5,3,5,9])
a
array([1, 2, 5, 3, 5, 9])
a = np.arange(1, 20) # start, end
a
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
a = np.arange(1, 20, 3) # start, end, step
a
array([ 1, 4, 7, 10, 13, 16, 19])
# check dimension and size
print("dimension - ", a.ndim)
print("size - ", a.shape)
print("length - ", len(a))
dimension - 1 size - (7,) length - 7
b = np.array([[1,2,3], [2,3,4], [3,4,5]])
b
array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
# 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
c = np.array([[[5,7], [8,9], [0,7]], [[1,2], [2,3], [6,7]], [[5,7], [8,9], [0,7]]])
c
array([[[5, 7], [8, 9], [0, 7]], [[1, 2], [2, 3], [6, 7]], [[5, 7], [8, 9], [0, 7]]])
# 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
# by no
a = np.arange(1, 10, 2)
print(a)
print(a.shape)
[1 3 5 7 9] (5,)
# 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,)
# 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 :
np.ones(4)
array([ 1., 1., 1., 1.])
np.ones((2,4))
array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
np.zeros((2,3))
array([[ 0., 0., 0.], [ 0., 0., 0.]])
np.eye((3))
array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
np.diag([1,2,3,4])
array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
with help of numpy random library
np.random.rand(5) # pick any 5 value between 0 and 1
array([ 0.77109549, 0.92437055, 0.13376104, 0.00209447, 0.99861705])
np.random.randn(5) # includes negative no as well (Gaussian)
array([-0.26465512, 0.76965644, 0.61422423, 2.54456552, -0.99713591])
np.random.randint(9) # this will pick any random no from 0 to 8 (integer)
1
np.random.randint(10, 13) # start, end end exclusive
11
np.random.randint(10, 13, 2) # start, end, no of element end exclusive
array([11, 11])
np.random.randint(10, 13, 4) # start, end, no of element end exclusive
array([11, 10, 10, 10])
np.random.randint(1, 20, (4, 3))
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:
np.random.seed(123)
np.random.randint(1, 10, 2)
array([3, 3])
np.random.seed(123)
np.random.randint(1, 10, 2)
array([3, 3])
a = np.random.randint(3,9, 3)
print(a)
print(a.dtype)
[4 6 5] int32
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:
c = np.array([1, 2, 3], dtype=float)
c.dtype
dtype('float64')
c = np.array([1, 2, 3])
c.dtype
dtype('int32')
a = np.ones((3, 3))
a.dtype
dtype('float64')
# complex data type
d = np.array([1+2j, 3+4j, 5+6*1j])
d.dtype
dtype('complex128')
# Boolean
e = np.array([True, False, False, True])
e.dtype
dtype('bool')
# String
f = np.array(['Bonjour', 'Hello', 'Hallo'])
f.dtype # unicode string with max 7 letters
dtype('<U7')
a = np.arange(10)
print(a)
a[2], a[0], a[8], a[-1]
[0 1 2 3 4 5 6 7 8 9]
(2, 0, 8, 9)
a[2:7]
array([2, 3, 4, 5, 6])
# reversing the array
a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
a[1:8:3] #start, end, step
array([1, 4, 7])
a[:4] #start, 4
array([0, 1, 2, 3])
a[6:] #6, end
array([6, 7, 8, 9])
a[:] #start, end
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy array supports all python Indexing and Slicing operation
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a[5:] = 11
a
array([ 0, 1, 2, 3, 4, 11, 11, 11, 11, 11])
a[6:] = a[::-1][6:]
a
array([ 0, 1, 2, 3, 4, 11, 3, 2, 1, 0])
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a[6:]
b
array([6, 7, 8, 9])
a[7] = 13
print(a)
print(b)
[ 0 1 2 3 4 5 6 13 8 9] [ 6 13 8 9]
print(np.may_share_memory(a,b))
True
b = a[6:].copy()
b
array([ 6, 13, 8, 9])
a[8] = 16
print(a)
print(b)
[ 0 1 2 3 4 5 6 13 16 9] [ 6 13 8 9]
print(np.may_share_memory(a,b))
False
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a%3==0
b # boolean array
array([ True, False, False, True, False, False, True, False, False, True], dtype=bool)
# picking only those for which a%3==0 is TRUE
a[b] # boolean indexing
array([0, 3, 6, 9])
a[b] = a[b]+200
a
array([200, 1, 2, 203, 4, 5, 206, 7, 8, 209])
b = np.random.randint(0, 6, 3)
b
array([1, 0, 0])
a[b]
array([ 1, 200, 200])