In [1]:
import numpy as np
In [2]:
np.arange(10)
Out[2]:
In [3]:
np.arange(1, 10, 2)
Out[3]:
In [4]:
np.arange(1, 10, 0.5, dtype=np.float64)
Out[4]:
In [5]:
m_data = np.arange(1, 10)
In [6]:
m_data.ndim
Out[6]:
In [7]:
m_data.shape
Out[7]:
In [8]:
m_data.size, m_data.itemsize, m_data.dtype
Out[8]:
In [9]:
m_data.data
Out[9]:
In [10]:
list(m_data.data)
Out[10]:
In [11]:
%%capture results
%timeit python_list = range(1, 1000)
%timeit np_array = np.arange(1, 1000)
In [12]:
print results
In [13]:
%%capture results
%timeit python_list = range(1, 10000)
%timeit np_array = np.arange(1, 10000)
In [14]:
print results
In [15]:
%%capture results
list1, list2 = range(1, 10000), range(1, 10000)
%timeit list1 + list2
In [16]:
print results
In [17]:
%%capture results
array1, array2 = np.arange(1, 10000), np.arange(1, 10000)
%timeit array1 + array2
In [18]:
print results
In [19]:
%%timeit
for i in range(100):
pass
In [20]:
%%timeit
for i in np.arange(100):
pass
In [21]:
8.33/1.57
Out[21]:
In [22]:
%%timeit
for i in range(1000000):
pass
In [23]:
%%timeit
for i in np.arange(1000000):
pass
In [24]:
71.2/25.8
Out[24]:
In [25]:
np.array([1, 2, 3, 4, 5, 6])
Out[25]:
In [26]:
# Multi dimensional array
np.array([[1, 2], [3, 4], [5, 6]])
Out[26]:
In [27]:
np.zeros((2, 4))
Out[27]:
In [28]:
np.zeros((2, 3, 4), dtype=np.int64)
Out[28]:
In [29]:
# To generate values between two numbers
np.linspace(1, 5, num=10)
Out[29]:
In [30]:
np.linspace(1, 5, num=10, endpoint=False)
Out[30]:
In [31]:
# Array with random numbers
np.random.random(10)
Out[31]:
In [32]:
np.random.random((3, 3))
Out[32]:
In [33]:
ds = np.random.random((2, 3))
ds
Out[33]:
In [34]:
np.max(ds)
Out[34]:
In [35]:
np.max(ds, axis=0)
Out[35]:
In [36]:
np.max(ds, axis=1)
Out[36]:
In [37]:
np.min(ds)
Out[37]:
In [38]:
np.mean(ds)
Out[38]:
In [39]:
np.median(ds)
Out[39]:
In [40]:
np.std(ds)
Out[40]:
In [41]:
np.sum(ds)
Out[41]:
In [42]:
# Reshaping the matrix
print ds
np.reshape(ds, (6, 1))
Out[42]:
In [43]:
# Flattening
np.ravel(ds)
Out[43]:
In [44]:
ds
Out[44]:
In [45]:
ds[0:2]
Out[45]:
In [46]:
ds[0:2, 0]
Out[46]:
In [47]:
ds[0:2, 0:2]
Out[47]:
In [48]:
%matplotlib inline
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('lena.jpg')
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.imshow(img2) # expect true color
Out[48]:
In [49]:
img3 = img2[:256]
plt.imshow(img3)
Out[49]:
In [50]:
img4 = img2[:, 0:256]
plt.imshow(img4)
Out[50]:
In [51]:
img5 = img2[256:512, 256:512]
plt.imshow(img5)
Out[51]: