NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
In this notebook we'll try various numpy methods and in the process learn more about NumPy.
Please follow this link
In [1]:
import numpy as np
In NumPy, strictly 1-D arrays are known as vectors and 2-D arrays are known as matrices (a matrix can also have only one row or one column). With NumPy arrays we can get access to various pre-written functions from NumPy.
There are various ways to create NumPy Arrays. Some of them are listed below.
In [2]:
list = ['a', 'b', 'c', 'd']
list
Out[2]:
In [3]:
np.array(list)
Out[3]:
In [4]:
list_matrix = [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
list_matrix
Out[4]:
In [5]:
np.array(list_matrix)
Out[5]:
arange method
arange ( starting_number, ending_number_plus_one, step? )
Returns evenly spaced values within a given interval. step is optional
In [6]:
np.arange(1, 11)
Out[6]:
In [7]:
np.arange(5, 60, 5)
Out[7]:
zeros method
zeros ( shape )
Returns a new array of given shape and type, filled with zeros.
In [8]:
np.zeros(10)
Out[8]:
In [9]:
np.zeros((5, 5))
Out[9]:
ones method
ones ( shape )
Returns a new array of given shape and type, filled with ones.
In [10]:
np.ones(10)
Out[10]:
In [11]:
np.ones((5,5))
Out[11]:
linspace
linspace ( starting_number, ending_number, number_of_elements_in_array )
Returns evenly spaced numbers over a specified interval.
In [12]:
np.linspace(10, 20, 5)
Out[12]:
In [13]:
np.linspace(100, 101, 10)
Out[13]:
eye
eye ( number_of_rows )
Returns a 2-D array with ones on the diagonal and zeros elsewhere. (Returns and identity matrix)
In [14]:
np.eye(4)
Out[14]:
In [15]:
np.random.rand(5)
Out[15]:
In [16]:
np.random.rand(3,3)
Out[16]:
In [17]:
np.random.rand(2,3,4)
Out[17]:
randn
randn ( shape )
Returns a sample (or samples) from the "standard normal" distribution.
In [18]:
np.random.randn(3)
Out[18]:
In [19]:
np.random.randn(4,4)
Out[19]:
randint
randint( low, high?, size? )
Returns random integers from low
(inclusive) to high
(exclusive).
In [20]:
np.random.randint(5)
Out[20]:
In [21]:
np.random.randint(1,11)
Out[21]:
In [22]:
np.random.randint(1, 100, 10)
Out[22]:
In [23]:
np.random.randint(1, 100, (4, 4))
Out[23]:
In [24]:
list = np.arange(1,10)
In [25]:
list.max()
Out[25]:
In [26]:
list.min()
Out[26]:
In [27]:
list.argmax()
Out[27]:
In [28]:
list.argmin()
Out[28]:
In [29]:
reshape_list = np.arange(1,10)
reshape_list
Out[29]:
In [30]:
reshape_list.reshape(3,3)
Out[30]:
Shape
Its an attribute which returns the shape (in a tuple) of the array.
In [31]:
shape_list = np.arange(1,10)
shape_list
Out[31]:
In [32]:
shape_list.shape
Out[32]:
In [33]:
shape_list.reshape(3,3)
Out[33]:
In [34]:
shape_list.reshape(3,3).shape
Out[34]:
ravel
ravel ( )
Returns a flattened array. (does not return a new copy)
In [35]:
ravel_list = np.arange(1,26).reshape(5,5)
ravel_list
Out[35]:
In [36]:
ravel_list = ravel_list.ravel()
ravel_list
Out[36]:
flatten
flatten ( )
Returns a copy of the array collapsed into one dimension.
In [37]:
flatten_list = np.arange(1,26).reshape(5,5)
flatten_list
Out[37]:
In [38]:
flatten_list = flatten_list.flatten()
flatten_list
Out[38]:
In [39]:
list = np.arange(1,11)
list.dtype
Out[39]:
In [40]:
list = np.array(['a', 'b', 'c'])
list.dtype
Out[40]:
In [41]:
selection_list = np.arange(1,26)
selection_list
Out[41]:
In [42]:
selection_list[9]
Out[42]:
In [43]:
selection_list[24]
Out[43]:
For multi dimensional lists
In [44]:
selection_list = selection_list.reshape(5, 5)
selection_list
Out[44]:
In [45]:
selection_list[2:,1:]
Out[45]:
In [46]:
selection_list[1:4,2:4]
Out[46]:
In [47]:
selection_list = selection_list.ravel()
selection_list
Out[47]:
In [48]:
selection_list[selection_list>10]
Out[48]:
NumPy comes with many universal functions. Some of it are in the next cells.
In [49]:
uni_list = np.arange(1,11)
uni_list
Out[49]:
In [50]:
np.square(uni_list)
Out[50]:
In [51]:
np.sin(uni_list)
Out[51]:
In [52]:
np.log(uni_list)
Out[52]:
In [53]:
np.log10(uni_list)
Out[53]:
In [54]:
np.isfinite(uni_list)
Out[54]: