How to use numpy array in machine learning

How to use numpy array in machine learningHow to use numpy?

Numpy works as a package (A linear algebra library) to computer scientific operation using python, widely for data analysis. A person named Travis Oliphant has started to develop the NumPy library. It was the first release in 2006 with version V 1.0. Nowadays used for the calculation of multidimensional array, as well as used to perform high-level mathematical calculations.

In general words, the whole library is based on a Numpy array, mainly based on “ndarray”. ndarray stands for N-dimensional array.

NumPy has two main tastes or flavors: 1) matrices 2) vectors.

Now let’s create NumPy array using Google Colab.

import numpy as np


Let’s cast them into array.


Output: – array([11, 22,33])

Let’s create an array using NumPy built-in generation method. When we create an array using the in-built method then it will give us faster results in a simpler manner.

np.arange(0,5) //arange([start,] stop[, step,], dtype=None)

array([0, 1, 2, 3, 4])

If we want only even value then


array([0, 2, 4])

we have another function which have same kind of property but works differently, “linspace”


O/P:- array([0. , 0.75, 1.5 , 2.25, 3. ])

Here as you can see both the arrange and linspace have the same argument. But,

1) np.arange:- start, end and third argument (here in this example for even)

2) np.linspace:- start, end, and the last number (will return evenly spaced number over a specified interval) (One important point is here it returns one-dimensional array). In short last argument contains the number of points/items you want.

this library contains several methods let’s check one by one

Check the type: –



Output: – dtype(‘int64’)

1)If interested to return only 1’s in your array then


O/P:- array([1., 1.])

And for two dimensional


O/P:- array([[1., 1., 1., 1.], [1., 1., 1., 1.]])

Identity matrix, A two-dimensional square matrix, very useful when working with linear algebra. The number of rows is the same as the number of columns having a diagonal one and the rest is zero. So, it required only a single-digit as an argument.


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

To generate the array of random numbers, in NumPy, there are lots of ways, here I am going to explain to you one of the popular methods name “np.random.rand(3)”.

This method will create an array of the given shape you pass in. which means here it will create a one-dimensional array uniformly distributed from 0 to 1.

array([0.86841881, 0.52456273, 0.8753541 ])

But instead of 0 to 1, if we want to display a standard normal distribution center around zero then we have another method “randn”.


What other operation can we performed using NumPy array?

So, by creating two-dimensional array you can re-shape the it. For example,

import numpy as np




Here it will reshape it with one row and maximum number of columns. Same we you can use (-1,1) for maximum number of rows.

Above we have learned the NumPy Array now let’s discuss Python, Numpy, Pandas Matplotlib in detail

Python (Python introduction):-

You can declare the variable without using and specify the datatype. Let’s say






Now to access the value you can use starting from “0”.


And to access the last value either you can use the index number here it is “8” or “-1”



To access the second last element, we can use the above line of code.

Can we have the mixture of the element in the list?

Yes, we can as you can see in the above list all the records are in digit but you can insert the string as well check the code below.


In general, most of the code is same as we write for C, but some little syntax changes.

Like to declare the function in python we use the “def” keyword. To return the value from the function is very easy task for all of us, but using python we can return two different value from our function and assign that value to our variable.Let’s take a look how function works in python.Let’s take a look how function works in python.

def addAndSub(a,b):
return a+b,a-b

Panda (How to use pandas in python):-

It is used for data analysis. The one-dimensional array is called series and two-dimensional array is called DataFrame, also we can specify the value with the key/label. Here In down below I specify the code for one-dimensional array .

import pandas as pd

How to use matplotlib, library? or How to draw graph in matplotlib?

Using this file you can visualize the data by drawing different plot.

import matplotlib.pyplot as plt

By default,we get a line plot and to get the scatter plot

just write the codeplt.scatter(x_ax,y_ax)

Leave a Comment