0% found this document useful (0 votes)
4 views7 pages

ML Sample Programs (1)

NumPy is a Python library for scientific computing that provides a powerful n-dimensional array object, useful for various operations including linear algebra and random number generation. It can be installed via pip and supports operations like slicing, mathematical functions, and special functions such as sine and cosine. Pandas, another library, is used for data manipulation and analysis, allowing operations on data frames such as merging, concatenation, and changing index values.

Uploaded by

viickyy0011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

ML Sample Programs (1)

NumPy is a Python library for scientific computing that provides a powerful n-dimensional array object, useful for various operations including linear algebra and random number generation. It can be installed via pip and supports operations like slicing, mathematical functions, and special functions such as sine and cosine. Pandas, another library, is used for data manipulation and analysis, allowing operations on data frames such as merging, concatenation, and changing index values.

Uploaded by

viickyy0011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What are NumPy Arrays?

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.

Where is NumPy used?


Python NumPy arrays provide tools for integrating C, C++, etc. It is also useful in linear algebra,
random number capability etc. NumPy array can also be used as an efficient multi-dimensional
container for generic data. Now, let me tell you what exactly is a Python NumPy array.

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:

How do I install NumPy?


To install Python NumPy, go to your command prompt and type “pip install numpy”. Once the
installation is completed, go to your IDE (For example: PyCharm) and simply import it by
typing: “import numpy as np”

Single-dimensional Numpy Array:


import numpy as np

a=np.array([1,2,3])

print(a)

Multi-dimensional Array:
a=np.array([(1,2,3),(4,5,6)])

print(a)

Python NumPy Operations

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())

Square Root & Standard Deviation

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

import matplotlib.pyplot as plt

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

import matplotlib.pyplot as plt

x= np.arange(0,3*np.pi,0.1)

y=np.tan(x)

plt.plot(x,y)

plt.show()

What is Python Pandas?

Pandas is used for data manipulation, analysis and cleaning. Python pandas is well suited for
different kinds of data, such as:

 Tabular data with heterogeneously-typed columns


 Ordered and unordered time series data
 Arbitrary matrix data with row & column labels
 Unlabelled data
 Any other form of observational or statistical data sets
 Python Pandas Operations
 Using Python pandas, you can perform a lot of operations with series, data frames,
missing data, group by etc.

Slicing the Data Frame


In order to perform slicing on data, you need a data frame. Don’t worry, data frame is a 2-
dimensional data structure and a most common pandas object.

Sample program by using pandas library :-

import pandas as pd

XYZ_web= {'Day':[1,2,3,4,5,6], "Visitors":[1000, 700,6000,1000,400,350], "Bounce_Rate":


[20,20, 23,15,10,34]}

df= pd.DataFrame(XYZ_web)

print(df)

Merging & Joining


In merging, you can merge two data frames to form a single data frame. You can also decide
which columns you want to make common. Let me implement that practically, first I will create
three data frames, which has some key-value pairs and then merge the data frames together.

Import pandas as pd

df1= pd.DataFrame({ "HPI":[80,90,70,60],"Int_Rate":[2,1,2,3],"IND_GDP":[50,45,45,67]},


index=[2001, 2002,2003,2004])

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.

df1 =pd.DataFrame({"HPI":[80,90,70,60],"Int_Rate":[2,1,2,3], "IND_GDP":[50,45,45,67]},


index=[2001, 2002,2003,2004])

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)

Change the index


Next in python pandas tutorial, we’ll understand how to change the index values in a dataframe.
For example, let us create a dataframe with some key value pairs in a dictionary and change the
index values.

import pandas as pd

df=pd.DataFrame({"Day":[1,2,3,4],"Visitors":[200,100,230,300],"Bounce_Rate":[20,45,60,10]})

df.set_index("Day", inplace= True)

print(df)

Change the Column Headers


Let us now change the headers of column in this python pandas tutorial. Let us take the same
example, where I will change the column header from “Visitors” to “Users”.

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)

You might also like