Python Libraries for Machine Learning: Numpy
Introduction
Till now you configured an ML environment using Anaconda.
Python provides various functionalities to support implementing machine learning, with the help of different python libraries. From this chapter onwards, we will start exploring and studying each of them one by one.
We will start with NumPy or Numerical Python.
What is Python NumPy?
Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created the NumPy package by incorporating the features of Numarray into the Numeric package. There are many contributors to this open-source project.
NumPy or Numerical Python is a python library that provides the following
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier Transform, and random number capabilities.
Installing NumPy in Python
1. Ubuntu/ Linux
2. Anaconda
- sudo apt update -y
- sudo apt upgrade -y
- sudo apt install python3-tk python3-pip -y
- sudo pip install numpy -y
- conda install -c anaconda numpy
NumPy Array
It is a powerful N-dimensional array which is in the form of rows and columns. We can initialize NumPy arrays from the nested Python list and access its elements.
NumPy array is not the same as the Standard Python Library Class array.array, which only handles 1D arrays.
- Single Dimensional NumPy Array
- import numpy as np
- a = np.array([1,2,3])
- print(a)
the above code will result in [1 2 3]
- Multi-Dimensional arrays
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a)
NumPy Array Attributes
- ndarray.ndim
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.ndim)
- ndarray.shape
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.shape)
- ndarray.size
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.size)
- ndarray.dtype
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.dtype)
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]], dtype = float)
- print(a.dtype)
- ndarray.itemsize
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.itemsize)
- ndarray.data
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(a.data)
- ndarray.sum()
- import numpy as np
- a = np.random.random( (2,3) )
- print(a)
- print(a.sum())
[0.7115755 0.57306008 0.64267163]], - ndarray.min()
- import numpy as np
- a = np.random.random( (2,3) )
- print(a.min())
[0.7115755 0.57306008 0.64267163]], - ndarray.max()
- import numpy as np
- a = np.random.random( (2,3) )
- print(a.max())
[0.7115755 0.57306008 0.64267163]],
NumPy Functions
1. numpy.type()
Syntax
type(numpy.ndarray)
It is a python function is used to return the type of the parameter passed. In the case of numpy array, it will return numpy.ndarray
- import numpy as np
- a = np.array([[1,2,3],[4,5,6]])
- print(type(a))
2. numpy.zeros()
Syntax
numpy.zeros((rows,columns), dtype)
The above function will create a numpy array of the given the dimensions with each element being zero. If no dtype is defined, default dtype is taken
- import numpy as np
- np.zeros((3,3))
- print(a)
3. numpy.ones()
Syntax
numpy.ones((rows,columns), dtype)
The above function will create a numpy array of the given dimensions. If no dtype is defined with each element being one, default dtype is taken.
- import numpy as np
- np.ones((3,3))
- print(a)
4. numpy.empty()
Syntax
numpy.empty((rows,columns))
The above function creates an array whose initial content is random and depends on the state of the memory.
- import numpy as np
- np.empty((3,3))
- print(a)
5. numpy.arange()
Syntax
numpy.arange(start, stop, step)
The above function is used to make a numpy array with elements in the range between the start and stop value with the difference of step value.
- import numpy as np
- a=np.arange(5,25,4)
- print(a)
6. numpy.linspace()
Syntax
numpy.linspace(start, stop, num_of_elements)
The above function is used to make a numpy array with elements in the range between the start and stop value and num_of_elements as the size of the numpy array. The default dtype of numpy array is float64
- import numpy as np
- a=np.linspace(5,25,5)
- print(a)
7. numpy.logspace()
Syntax
numpy.logspace(start, stop, num_of_elements)
The above function is used to make a numpy array with elements in the range between the start and stop value and num_of_elements as the size of the numpy array. The default dtype of numpy array is float64. All the elements will be spanned over the logarithmic scale i.e the resulting elements are the log of the corresponding element.
- import numpy as np
- a=np.logspace(5,25,5)
- print(a)
8. numpy.sin()
Syntax
numpy.sin(numpy.ndarray)
The above code will return the sin of the given parameter.
- import numpy as np
- a=np.logspace(5,25,2)
- print(np.sin(a))
Similarly, there are
cos()
,tan()
, etc.9. numpy.reshape()
Syntax
numpy.resahpe(dimensions)
The above function is used to change the dimension of a numpy array. The number of arguments in the reshape decides the dimensions of the numpy array.
- import numpy as np
- a=np.arange(9).reshape(3,3)
- print(a)
10. numpy.random.random()
Syntax
numpy.random.random( (rows, column) )
The above function is used to return a numpy ndarray with the given dimensions and each element of ndarray being randomly generated.
- a = np.random.random((2,2))
11. numpy.exp()
Syntax
numpy.exp(numpy.ndarray)
The above function returns a ndarray with exponential of every element
- b = np.exp([10])
12. numpy.sqrt()
Syntax
numpy.sqrt(numpy.ndarray)
The above function returns a ndarray with ex of every element
- b = np.sqrt([16])
The above code returns the value 4
NumPy Basic Operations
- a = np.array( [ 5, 10, 15, 20, 25] )
- b = np.array( [ 0, 1, 2, 3 ] )
- c = a - b
- b**2
- 10* np.sin(a)
- a<15
NumPy Array Basic Operations
- a = np.array( [[1,1], [0,1]])
- b = np.array( [[2,0],[3,4]])
- a * b
- a @ b
or
- a.dot(b)
Conclusion
In this chapter, we studied Python NumPy. In the next chapter, we will learn about Python Pandas.
Python Pandas is an excellent library used majority for data manipulation and analysis.
Author
Rohit Gupta
64
28.1k
3.1m