ML Sample Programs (1)
ML Sample Programs (1)
NumPy is a Python package that stands for ‘Numerical Python’. It is the core library for
scientific computing, which contains a powerful n-dimensional array object.
Python NumPy Array: Numpy array is a powerful N-dimensional array object which is in the
form of rows and columns. We can initialize NumPy arrays from nested Python lists and access
it elements. In order to perform these NumPy operations, the next question which will come in
your mind is:
a=np.array([1,2,3])
print(a)
Multi-dimensional Array:
a=np.array([(1,2,3),(4,5,6)])
print(a)
ndim:
You can find the dimension of the array, whether it is a two-dimensional array or a single
dimensional array. So, let us see this practically how we can find the dimensions. In the below
code, with the help of ‘ndim’ function, I can find whether the array is of single dimension or
multi dimension.
import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
itemsize:
You can calculate the byte size of each element. In the below code, I have defined a single
dimensional array and with the help of ‘itemsize’ function, we can find the size of each element.
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
dtype:
You can find the data type of the elements that are stored in an array. So, if you want to know the
data type of a particular element, you can use ‘dtype’ function which will print the datatype
along with the size. In the below code, I have defined an array where I have used the same
function.
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
slicing:
As you can see the ‘reshape’ function has showed its magic. Now, let’s take another operation i.e
Slicing. Slicing is basically extracting particular set of elements from an array. This slicing
operation is pretty much similar to the one which is there in the list as well.
import numpy as np
a=np.array([(1,2,3,4),(3,4,5,6)])
print(a[0,2])
linspace
This is another operation in python numpy which returns evenly spaced numbers over a specified
interval.
import numpy as np
a=np.linspace(1,3,10)
print(a)
max/min:- min
Next, we have some more operations in numpy such as to find the minimum, maximum as well
the sum of the numpy array.
import numpy as np
a= np.array([1,2,3])
print(a.min())
print(a.max())
print(a.sum())
There are various mathematical functions that can be performed using python numpy. You can
find the square root, standard deviation of the array.
import numpy as np
a=np.array([(1,2,3),(3,4,5,)])
print(np.sqrt(a))
print(np.std(a))
Additio/Operation
You can perform more operations on numpy array i.e addition, subtraction,multiplication and
division of the two matrices. Let me go ahead in python numpy tutorial, and show it to you
practically:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(x+y)
print(x-y)
print(x/y)
Python Numpy Special Functions
There are various special functions available in numpy such as sine, cosine, tan, log etc. First,
let’s begin with sine function where we will learn to plot its graph. For that, we need to import a
module called matplotlib. To understand the basics and practical implementations of this module,
import numpy as np
x= np.arange(0,3*np.pi,0.1)
y=np.sin(x)
plt.plot(x,y)
plt.show()
Similarly, you can plot a graph for any trigonometric function such as cos, tan etc. Let me show
you one more example where you can plot a graph of another function, let’s say tan.
import numpy as np
x= np.arange(0,3*np.pi,0.1)
y=np.tan(x)
plt.plot(x,y)
plt.show()
Pandas is used for data manipulation, analysis and cleaning. Python pandas is well suited for
different kinds of data, such as:
import pandas as pd
df= pd.DataFrame(XYZ_web)
print(df)
Import pandas as pd
df2=pd.DataFrame({ "HPI":[80,90,70,60],"Int_Rate":[2,1,2,3],"IND_GDP":[50,45,45,67]},
index=[2005, 2006,2007,2008])
merged= pd.merge(df1,df2)
print(merged)
Concatenation
Concatenation basically glues the data frames together. You can select the dimension on which
you want to concatenate. For that, just use “pd. concat” and pass in the list of dataframes to
concatenate together.
df2 = pd.DataFrame({"HPI":[80,90,70,60],"Int_Rate":[2,1,2,3],"IND_GDP":[50,45,45,67]},
index=[2005, 2006,2007,2008])
concat= pd.concat([df1,df2])
print(concat)
import pandas as pd
df=pd.DataFrame({"Day":[1,2,3,4],"Visitors":[200,100,230,300],"Bounce_Rate":[20,45,60,10]})
print(df)
import pandas as pd
df=pd.DataFrame({"Day":[1,2,3,4],"Visitors":[200,100,230,300],"Bounce_Rate":[20,45,60,10]})
df = df.rename(columns={"Visitors":"Users"})
print(df)