In [1]:
import numpy as np
In [2]:
# numpy array
arr = np.array([0,1,2,3])
arr
Out[2]:
numpy calc is faster than native python
In [3]:
# testing the same
L = range(100000)
%timeit [i**2 for i in L]
In [4]:
L = np.arange(100000)
%timeit L**2
In [5]:
a = np.array([1,2,5,3,5,9])
a
Out[5]:
In [6]:
a = np.arange(1, 20) # start, end
a
Out[6]:
In [7]:
a = np.arange(1, 20, 3) # start, end, step
a
Out[7]:
In [8]:
# check dimension and size
print("dimension - ", a.ndim)
print("size - ", a.shape)
print("length - ", len(a))
In [9]:
b = np.array([[1,2,3], [2,3,4], [3,4,5]])
b
Out[9]:
In [10]:
# check dimension and size
print("dimension - ", b.ndim)
print("size - ", b.shape)
print("length - ", len(b))
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]:
In [12]:
# check dimension and size
print("dimension - ", c.ndim)
print("size - ", c.shape)
print("length - ", len(c))
Simplest way to create test array
In [13]:
# by no
a = np.arange(1, 10, 2)
print(a)
print(a.shape)
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)
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)
Common arrays to use :
In [16]:
np.ones(4)
Out[16]:
In [17]:
np.ones((2,4))
Out[17]:
In [18]:
np.zeros((2,3))
Out[18]:
In [19]:
np.eye((3))
Out[19]:
In [20]:
np.diag([1,2,3,4])
Out[20]:
In [21]:
np.random.rand(5) # pick any 5 value between 0 and 1
Out[21]:
In [22]:
np.random.randn(5) # includes negative no as well (Gaussian)
Out[22]:
In [23]:
np.random.randint(9) # this will pick any random no from 0 to 8 (integer)
Out[23]:
In [24]:
np.random.randint(10, 13) # start, end end exclusive
Out[24]:
In [25]:
np.random.randint(10, 13, 2) # start, end, no of element end exclusive
Out[25]:
In [26]:
np.random.randint(10, 13, 4) # start, end, no of element end exclusive
Out[26]:
In [27]:
np.random.randint(1, 20, (4, 3))
Out[27]:
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]:
In [29]:
np.random.seed(123)
np.random.randint(1, 10, 2)
Out[29]:
In [30]:
a = np.random.randint(3,9, 3)
print(a)
print(a.dtype)
In [31]:
a = np.random.rand(3)
print(a)
print(a.dtype)
You can explicitly specify which data-type you want:
In [32]:
c = np.array([1, 2, 3], dtype=float)
c.dtype
Out[32]:
In [33]:
c = np.array([1, 2, 3])
c.dtype
Out[33]:
In [34]:
a = np.ones((3, 3))
a.dtype
Out[34]:
In [35]:
# complex data type
d = np.array([1+2j, 3+4j, 5+6*1j])
d.dtype
Out[35]:
In [36]:
# Boolean
e = np.array([True, False, False, True])
e.dtype
Out[36]:
In [37]:
# String
f = np.array(['Bonjour', 'Hello', 'Hallo'])
f.dtype # unicode string with max 7 letters
Out[37]:
In [38]:
a = np.arange(10)
print(a)
a[2], a[0], a[8], a[-1]
Out[38]:
In [39]:
a[2:7]
Out[39]:
In [40]:
# reversing the array
a[::-1]
Out[40]:
In [41]:
a[1:8:3] #start, end, step
Out[41]:
In [42]:
a[:4] #start, 4
Out[42]:
In [43]:
a[6:] #6, end
Out[43]:
In [44]:
a[:] #start, end
Out[44]:
In [45]:
a = np.arange(10)
a
Out[45]:
In [46]:
a[5:] = 11
a
Out[46]:
In [47]:
a[6:] = a[::-1][6:]
a
Out[47]:
In [48]:
a = np.arange(10)
a
Out[48]:
In [49]:
b = a[6:]
b
Out[49]:
In [50]:
a[7] = 13
print(a)
print(b)
In [51]:
print(np.may_share_memory(a,b))
In [52]:
b = a[6:].copy()
b
Out[52]:
In [53]:
a[8] = 16
print(a)
print(b)
In [54]:
print(np.may_share_memory(a,b))
In [55]:
a = np.arange(10)
a
Out[55]:
In [56]:
b = a%3==0
b # boolean array
Out[56]:
In [57]:
# picking only those for which a%3==0 is TRUE
a[b] # boolean indexing
Out[57]:
In [58]:
a[b] = a[b]+200
a
Out[58]:
In [59]:
b = np.random.randint(0, 6, 3)
b
Out[59]:
In [60]:
a[b]
Out[60]:
In [ ]: