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

numList=[11,22,33]

Let’s cast them into array.

np.array(numList)

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

np.arange(0,5,2)

array([0, 2, 4])

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

np.linspace(0,3,5)

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: –

numVar=np.array(numList)

numVar.dtype

Output: – dtype(‘int64’)

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

np.ones(2)

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

And for two dimensional

np.ones((2,4))

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.

np.eye(4)

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”.

np.random.randn(3)

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

n_two=np.array(([1,2,3],[4,5,6],[7,8,9],[0,1,2],[3,4,5]))

n_reShape=n_two.reshape(1,-1)

print(n_reShape)

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

a=10;

print(a)

a=”abc”;

print(a)

p_array=[1,2,3,4,5,6,7,8,9]

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

print(p_array[0])

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

print(p_array[-1])

print(p_array[-2])

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.

p_array=[1,2,3,4,5,6,7,8,9,”Harshida”]
print(p_array[-1])

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.

return a+b,a-b
print(p_sub)

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
p_panda=pd.Series([1,2,3,4,5],[‘a’,’e’,’i’,’o’,’u’])
print(p_panda[2])
print(p_panda[‘i’])

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
x_ax=[100,200,300,400,500]
y_ax=[1000,10,3000,4000,5000]
plt.plot(x_ax,y_ax)

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

just write the codeplt.scatter(x_ax,y_ax)