Search

# First Step Towards Machine Learning: NumPy by Hrithik Sharma

Hello, I am back with another article after completion of the module of python. In my previous article, I told you about the objects and classes in python. As you know I am learning Python for Machine Learning and Artificial Intelligence. Python is considered to be an important and very useful programming language in the development of Machine Learning and Artificial Intelligence due to the inbuilt libraries and functions used for data analysis and data preprocessing, data plotting, data filtration, and data visualization. The examples of such libraries are NumPy, Pandas, Matplotlib and Seaborn. These libraries are considered to be the basic libraries for data analysis, data analytics, data plotting, data visualization and applying the mathematical concepts and advanced analytics on data.

In my previous article, I talked about my university project idea on "Object Detection Using Computer Vision" and about the field of Computer Vision which is achieved by implementing the conventional neural networks which is a subset of Machine Learning. By the use of Machine Learning, we can solve the real-world problems and make algorithms that can adapt things on its own and learn to solve real-world problems. A Machine Learning Algorithm can be used against a game such as the game of chess which can mess up with the brain of players sometimes for beating their opponent & if an algorithm can beat a game then that Machine Learning algorithm can be used for solving the challenging real-world problems so it can be concluded that games are the best environment for testing of any Machine Learning algorithm that is going to be used for solving the problem existing in the real-world related to business, healthcare, weather predictions and in many other fields or just learning Machine Learning for fun there are endless possibilities.

In today's article, I am going to talk about the first data analysis library which is NumPy which stands for numerical python. NumPy is generally used for creating the n-dimensional arrays in the python environment and apply the mathematical functions on n-dimensional arrays generated using NumPy. For using NumPy in python environment first of all we have to install the library by typing the command "pip install numpy" in command prompt. In the case of Anaconda IDE, all packages related to Machine Learning & Data Science comes inbuilt inside the IDE. For using NumPy firstly we import Numpy as shown:

```>>> import numpy as np

```

Let's discuss some functions of NumPy library

## array() function

Let us consider a list l having 10 elements and a function array in called on list l using NumPy and stored in x and we printed x.

```>>> l=[1,2,3,4,5,6,7,8,9,10]

>>> x=np.array(l)

>>> x

```

The output of following program is:

```array([1, 2, 3, 4, 5, 6, 7, 8])

```

If we check the type of x:

```>>> type(x)

numpy.ndarray

```

The type of x is ndarray which is a n-dimensional NumPy array

Let us create a matrix using array() function

```>>> m=[[1,2,3],[4,5,6],[7,8,9]]

>>> z=np.array(m)

>>> z

array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

```

## arange() function

```>>> g=np.arange(1,11)

>>> g

```

The output of following program is:

```array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]

```

This is also a ndarray of NumPy which started form 1 and ends on 10 and 11 is exclusive.

Let us look at another example:

```>>> g=np.arange(1,21,2)

>>> g

```

The output of following program is:

```array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])

```

Here, in this example the first argument in arange function is starting index, second argument is stopping index and third index is step.

## zeros() function

zeros() function is used to create the array and matrices of zeros.

```>>> np.zeros(7)

array([0., 0., 0., 0., 0., 0., 0.])

```

Let's create matrices of zeros using zeros() function

```>>> np.zeros((4,4))

array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

>>> np.zeros((3,4))

array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])

```

## ones() function

ones() function is used to create the array and matrices of ones.

```>>> np.ones(10)

array([1., 1., 1., 1., 1., 1., 1., 1., 1.])

```

Let's create a matrices of ones using ones() function

```>>> np.ones((5,6))

array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])

>>> np.ones((2,2))

array([[1., 1.],
[1., 1.]])

```

## eye() function

eye() function is used to create identity matrix.

```>>> np.eye(5)

array([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])

```

eye() function takes 1 argument in order to create the identity square matrix of following number of rows and columns

## reshape() function

```>>> x=np.arange(1,21)

>>> x

array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

>>> x.reshape(4,5)

array([[ 1,  2,  3,  4,  5],
[ 6,  7,  8,  9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])

>>> x.reshape(5,4)

array([[ 1,  2,  3,  4],
[ 5,  6,  7,  8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]])

```

reshape() function is used to create a matrix of a desired array but the arguments in reshape function must be factors of number of elements in array. Here in this example in x array the number of elemnts are 20 and in reshape() function the (5,4) and (4,5) are the factors of 20.

Let' consider a list k:

```>>> k=[37, 36, 41, 97, 47, 59, 81, 70, 79, 74, 43, 39, 89, 94, 7, 53, 89, 11]

>>> l= np.array(k)

>>> l

array([37, 36, 41, 97, 47, 59, 81, 70, 79, 74, 43, 39, 89, 94, 7, 53, 89, 11]

>>> l.max()

97

>>> l.min()

7

>>> l.argmax()

3

>>> l.argmin()

14

```

## Some Mathematical Operations On NumPy Arrays

```>>> import numpy as np

>>> arr1 = np.arange(0,11)

>>> arr2 = np.arange(0,21,2)

>>> arr1

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

>>> arr2

array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20])

```

```>>> arr1 + arr2

array([ 0,  3,  6,  9, 12, 15, 18, 21, 24, 27, 30])

```

## Subtraction Of NumPy Arrays

```>>> arr2 - arr1

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

```

## Multiplication Of NumPy Arrays

```>>> arr1 * arr2

array([  0,   2,   8,  18,  32,  50,  72,  98, 128, 162, 200])

```

## Adding & Subtracting Of Integers On NumPy Arrays

```>>> arr1 + 100

array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110])

>>> arr2 - 1

array([-1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])

```

## NumPy Arrays Raised To Power

```>>> arr1 ** 3

array([   0,    1,    8,   27,   64,  125,  216,  343,  512,  729, 1000], dtype=int32])

>>> arr2 ** 2

array([  0,   4,  16,  36,  64, 100, 144, 196, 256, 324, 400], dtype=int32)

```

## Some More Mathematical Functions

```>>> np.sqrt(arr2)

array([0.        , 1.41421356, 2.        , 2.44948974, 2.82842712,
3.16227766, 3.46410162, 3.74165739, 4.        , 4.24264069,
4.47213595])

>>> np.exp(arr2)

array([1.00000000e+00, 7.38905610e+00, 5.45981500e+01, 4.03428793e+02,
2.98095799e+03, 2.20264658e+04, 1.62754791e+05, 1.20260428e+06,
8.88611052e+06, 6.56599691e+07, 4.85165195e+08])

>>> np.sin(arr2)

array([ 0.        ,  0.90929743, -0.7568025 , -0.2794155 ,  0.98935825,
-0.54402111, -0.53657292,  0.99060736, -0.28790332, -0.75098725,
0.91294525])

>>> np.log(arr2)

array([      -inf, 0.69314718, 1.38629436, 1.79175947, 2.07944154,
2.30258509, 2.48490665, 2.63905733, 2.77258872, 2.89037176,

2.99573227])

```

Python is set of about 1500+ libraries and until now I just used numpy for creating arrays in python environment and it is said that NumPy library contains about 620 functions each for different purposes. In this article I just illustrated some introductory functions of NumPy all functions of NumPy is about to explore by me.

This is all about my this article of learning python for Machine Learning and Artificial Intelligence as soon I learn different concepts of python I'll keep posting. So, there are more to go as I am learning.

Bye-Bye, See You in my next article. Until then enjoy Python and PEACE OUT.

See All