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

smita ml labbbb-1-10

The document provides an overview of essential libraries in Python for data manipulation and analysis, specifically NumPy, Pandas, and Matplotlib. It includes practical examples of functions and code snippets for creating and manipulating arrays in NumPy, handling data in Pandas, and basic plotting in Matplotlib. The aim is to familiarize students with these libraries as part of their Machine Learning coursework.

Uploaded by

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

smita ml labbbb-1-10

The document provides an overview of essential libraries in Python for data manipulation and analysis, specifically NumPy, Pandas, and Matplotlib. It includes practical examples of functions and code snippets for creating and manipulating arrays in NumPy, handling data in Pandas, and basic plotting in Matplotlib. The aim is to familiarize students with these libraries as part of their Machine Learning coursework.

Uploaded by

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

COMPUTER SCIENCE AND ENGINEERING

FACULTY OF ENGINEERING AND TECHONLOGY


MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Practical - 01
Aim: - Dealing with Data using NumPy, Pandas, Statistics library.
What is NumPy?

 NumPy is a powerful, well-optimized, free open-source library for the Python


programming language, adding support for large, multi-dimensional arrays. NumPy
also comes equipped with a collection of high-level mathematical functions to work in
conjunction with these arrays.

Functions in NumPy: -

 Creating an Array:

One-Dimensional Array:

Code:
import numpy as np
a = np.array([5,10,15,20,25])
print(a)
print(type(a))

Output:

Two-Dimensional Array:

Code:
import numpy as np
a = np.array([[5,10,15,20,25],[6,11,16,21,26]])
print(a)
print(type(a))

Output:

 Array Indexing:

Code:
import numpy as np
a = np.array([5,10,15,20,25])
print(a[0],a[1])

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

 Array Slicing:

Code:
import numpy as np
a = np.array([5,10,15,20,25,30,35])
print(a[1:5])

Output:

 Data Type:

Code:
import numpy as np
a = np.array([1,2,3,4,5,6,7])
b = np.array(["Eren", "Erwin"])
print(a.dtype)
print(b.dtype)

Output:

 View:

Code:
import numpy as np
a = np.array([1,2,3,4,5,7])
print(a)
X = a.view()
a[5] = 6
print(X)

Output:

 Array Shape:

Code:
import numpy as np

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

a = np.array([[1,2,3,4],[5,6,7,8]])
print(a.shape)

Output:

 Array Reshape:

Code:
import numpy as np
a = np.array([[1,2,3,4,5,6,7,8,9,10,11,12]])
Newarr = a.reshape(3,4)
print(Newarr)

Output:

 Iterating Arrays:

Code:
import numpy as np
a = np.array([1,2,3,4])
for x in a:
print(x)

Output:

 Array Join:

Using Concatenation:

Code:
import numpy as np
a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
a = np.concatenate((a1,a2))
print(a)

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

Using Stack Function:

Code:
import numpy as np
a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
a = np.stack((a1,a2), axis = 1)
print(a)

Output:

 Array Splitting:

Code:
import numpy as np
a = np.array([1,2,3,4,5,6])
a1 = np.array_split(a,3)
print(a1)

Output:

 Array Search:

Code:
import numpy as np
a = np.array([1,2,3,4,5,4,4])
x = np.where(a == 4)
print(x)

Output:

 Array Sort:

Code:
import numpy as np
a = np.array([10,5,20,15])

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

print(np.sort(a))

Output:

 Filtering Arrays:

Code:
import numpy as np
a = np.array([41,42,43,44])
x = [True, False, True, False]
a1 = a[x]
print(a1)

Output:

 Random Number Generation:

Code:
from numpy import random
x = random.randint(100)
y = random.rand()
print(x)
print(y)

Output:

 Random Distribution Probability Density Function:

Code:
from numpy import random
x = random.choice([3,5,7,9], p = [0.1, 0.3, 0.2, 0.4], size = (100))
print(x)

Output:

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

 Random Permutation:

Code:
from numpy import random
import numpy as np
a = np.array([1,2,3,4,5])
random.shuffle(a)
print(a)

Output:

What is Pandas?

 Pandas is an open-source Python library that provides tools for data manipulation and
analysis. It is widely used in data science, machine learning, and general
programming to handle structured data effectively. Pandas is built on top of NumPy
and offers high-level data structures and functions designed to make data wrangling
intuitive and efficient.

Functions in Pandas: -

 Reading CSV File:

Code:
Import pandas as pd
Df = pd.read_csv(‘Data.csv’)
Print(df.to_string())

Output:
This is ML lab.

Deep Learning

ANN
CNN

 Pandas Series:

Code:
import pandas as pd
a = [1,7,2]
myvar = pd.Series(a)
print(myvar)

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

 Dataframe Creation:

Code:
import pandas as pd
data = {"calories" : [420, 380, 390],"duration" : [50,40,45]}
df = pd.DataFrame(data)
print(df)

Output:

 Labels:

Code:
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["x", "y", "z"])
print(myvar)

Output:

 Readings the JSON file:

Code:
import pandas as pd
Df = pd.read_json('data.json')
print(df.to_string())

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

 Shape:

Code:
df.shape

Output:
(30, 2)

 Size:

Code:
df.size

Output:
60

 Head:

Code:
df.head()

Output:

 Tail:

Code:
df.tail()

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

 Sample:

Code:
df.sample(3)

Output:

 Describe:

Code:
df.describe()

Output:

 Unique:

Code:
df.[‘Salary’].unique()

Page |
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND TECHONLOGY
MACHINE LEARNING (303105354) B. TECH 3RD YEAR
ENROLLMENT NO: - 2203031241233

Output:

What is Matplotlib?
 Matplotlib is a low-level graph plotting library in python that serves as a visualization
utility.

Functions in Matplotlib: -

 Version:

Code:
import matlplotlib
print(matplotlib. version )

Output:
3.8.0

 Liner Graph:

Code:
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([1, 8])
ypoints = np.array([3, 10])
plt.plot(xpoints, ypoints)
plt.show()

Page |

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy