Experiment – 1: Creating a NumPy Array
Aim:
To create and explore different types of NumPy arrays such as basic arrays, zeros, ones,
random numbers, identity matrix, and evenly spaced arrays.
Theory:
NumPy (Numerical Python) is a powerful library used for numerical computations.
It provides:
Efficient array storage
Mathematical operations
Multi-dimensional array handling
Types of arrays:
ndarray: Basic array structure
zeros(): Creates array with all elements 0
ones(): Creates array with all elements 1
random(): Generates random values
eye(): Identity matrix
linspace(): Evenly spaced values
Program:
import numpy as np
# Basic ndarray
a = [Link]([1, 2, 3, 4])
print("Basic Array:", a)
# Array of zeros
b = [Link]((3, 3))
print("Zeros Array:\n", b)
# Array of ones
c = [Link]((2, 2))
print("Ones Array:\n", c)
# Random numbers
d = [Link](3, 3)
print("Random Array:\n", d)
# Identity matrix
e = [Link](3)
print("Identity Matrix:\n", e)
# Evenly spaced array
f = [Link](1, 10, 5)
print("Evenly Spaced Array:", f)
Output:
Basic Array: [1 2 3 4]
Zeros Array:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Ones Array:
[[1. 1.]
[1. 1.]]
Random Array:
[[0.23 0.45 0.67]
[0.12 0.89 0.56]
[0.78 0.34 0.91]]
Identity Matrix:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Evenly Spaced Array: [ 1. 3.25 5.5 7.75 10. ]
Experiment – 2: Shape and Reshaping of NumPy Array
Aim:
To study dimensions, shape, size, reshaping, flattening, and transpose of arrays.
Theory:
Every NumPy array has:
Dimension (ndim): Number of axes
Shape: Size of array in each dimension
Size: Total number of elements
Operations:
reshape(): Changes shape without altering data
flatten(): Converts multi-dimensional array to 1D
transpose(): Swaps rows and columns
These operations are essential for data manipulation in machine learning and data analysis.
Program:
import numpy as np
a = [Link]([[1,2,3],[4,5,6]])
print("Dimensions:", [Link])
print("Shape:", [Link])
print("Size:", [Link])
b = [Link](3,2)
print("Reshaped:\n", b)
c = [Link]()
print("Flattened:", c)
d = a.T
print("Transpose:\n", d)
Output:
Dimensions: 2
Shape: (2, 3)
Size: 6
Reshaped:
[[1 2]
[3 4]
[5 6]]
Flattened: [1 2 3 4 5 6]
Transpose:
[[1 4]
[2 5]
[3 6]]
Experiment – 3: Expanding and Squeezing
Aim:
To expand and reduce dimensions of NumPy arrays.
Theory:
Sometimes arrays need dimension adjustment:
expand_dims(): Adds a new axis (increases dimension)
squeeze(): Removes single-dimensional entries
Example:
(3,) → (1,3) using expand
(1,3) → (3,) using squeeze
Sorting:
sort() arranges elements in ascending order
Used in reshaping data for ML models.
Program:
import numpy as np
a = [Link]([1,2,3])
b = np.expand_dims(a, axis=0)
print("Expanded:\n", b)
c = [Link](b)
print("Squeezed:", c)
d = [Link](a)
print("Sorted:", d)
Output:
Expanded:
[[1 2 3]]
Squeezed: [1 2 3]
Sorted: [1 2 3]
Experiment – 4: Indexing and Slicing
Aim: To access and manipulate elements of NumPy arrays using indexing and slicing
techniques for arrays.
Theory:
Indexing and slicing help access specific elements:
1D slicing: a[start:end]
2D slicing: a[row, column]
3D slicing: Access using 3 indices
Special:
Negative slicing: Reverse arrays ([::-1])
These techniques are crucial for extracting meaningful data.
Program:
import numpy as np
a = [Link]([1,2,3,4,5])
print(a[1:4])
b = [Link]([[1,2,3],[4,5,6]])
print(b[0:2,1:3])
c = [Link]([[[1,2],[3,4]],[[5,6],[7,8]]])
print(c[1,0,1])
print(a[::-1])
Output:
[2 3 4]
[[2 3]
[5 6]]
6
[5 4 3 2 1]
Experiment – 5: Stacking and Concatenation
Aim:
To combine multiple NumPy arrays using stacking, concatenation, and broadcasting
techniques.
Theory:
Combining arrays:
vstack(): Vertical stacking (row-wise)
hstack(): Horizontal stacking (column-wise)
concatenate(): Joins arrays along an axis
Broadcasting:
Allows operations between arrays of different shapes by automatically adjusting dimensions.
Program:
import numpy as np
a = [Link]([1,2,3])
b = [Link]([4,5,6])
print([Link]((a,b)))
print([Link]((a,b)))
print([Link]((a,b)))
Output:
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
Experiment – 6: Pandas DataFrame Operations
Aim:
To create and manipulate Pandas DataFrames by adding columns, applying conditions, and
concatenating data.
Theory:
Pandas is used for data analysis.
Key structures:
Series: 1D data
DataFrame: 2D table
Operations:
Creating DataFrame
Adding columns
Filtering using conditions
concat() for merging data
Used widely in data science.
Program:
import pandas as pd
data = {'Name':['A','B'], 'Marks':[90,80]}
df = [Link](data)
print(df)
df['Grade'] = ['A','B']
print(df)
df2 = [Link]([df,df])
print(df2)
print(df[df['Marks']>85])
Output:
Name Marks
0 A 90
1 B 80
Name Marks Grade
0 A 90 A
1 B 80 B
Name Marks Grade
0 A 90 A
1 B 80 B
0 A 90 A
1 B 80 B
Name Marks Grade
0 A 90 A
Experiment – 7: Pandas Advanced Operations
Aim:
To handle missing data, perform sorting, and apply groupby operations on Pandas
DataFrames.
Theory:
Handling missing and structured data:
fillna(): Replaces missing values
sort_values(): Sorts data
groupby(): Groups data for aggregation
Program:
import pandas as pd
df = [Link]({'A':[1,None,3],'B':[4,5,6]})
[Link]("Missing", inplace=True)
print(df)
print(df.sort_values(by='B'))
print([Link]('B').sum())
Output:
A B
0 1.0 4
1 Missing 5
2 3.0 6
A B
0 1.0 4
1 Missing 5
2 3.0 6
A
B
4 1.0
5 Missing
6 3.0
Experiment – 8: Reading File Formats
Aim:
To read and analyze different file formats such as CSV, Excel, JSON, and text files using
Pandas.
Theory:
Pandas supports multiple file formats:
CSV: Comma-separated values
Excel: Spreadsheet data
JSON: Structured data format
Text files
Functions:
read_csv(), read_excel(), read_json()
Helps import real-world datasets.
Program:
import pandas as pd
df1 = pd.read_csv("[Link]")
print([Link]())
Output:
Unnamed: 0 [Link] City price Distance \
0 1 "Bike & Bed" CharinCo Hostel Osaka 3300 2.9
1 2 & And Hostel Fukuoka-City 2600 0.7
2 3 &And Hostel Akihabara Tokyo 3600 7.8
3 4 &And Hostel Ueno Tokyo 2600 8.7
4 5 &And Hostel-Asakusa North- Tokyo 1500 10.5
summary_score rating atmosphere cleanliness facilities location \
0 9.2 Superb 8.9 9.4 9.3 8.9
1 9.5 Superb 9.4 9.7 9.5 9.7
2 8.7 Fabulous 8.0 7.0 9.0 8.0
3 7.4 Very Good 8.0 7.5 7.5 7.5
4 9.4 Superb 9.5 9.5 9.0 9.0
security staff valueformoney lon lat
0 9.0 9.4 9.4 135.513767 34.682678
1 9.2 9.7 9.5 NaN NaN
2 10.0 10.0 9.0 139.777472 35.697447
3 7.0 8.0 6.5 139.783667 35.712716
4 9.5 10.0 9.5 139.798371 35.727898
Experiment – 9: Web Scraping
Aim:
To extract data from web pages using Python libraries such as requests and BeautifulSoup.
Theory:
Web scraping extracts data from websites.
Tools:
requests: Fetch web page
BeautifulSoup: Parse HTML
Steps:
1. Send request
2. Parse HTML
3. Extract required data
Used in data collection.
Program:
import requests
from bs4 import BeautifulSoup
url = "[Link]
r = [Link](url)
soup = BeautifulSoup([Link], '[Link]')
print([Link])
Output:
Google
Experiment – 10: Data Preprocessing
Aim:
To perform data preprocessing techniques such as feature scaling, standardization, label
encoding, and one-hot encoding.
Theory:
Data preprocessing prepares raw data for analysis.
Techniques:
Feature Scaling: Normalize values
Standardization: Mean = 0, Std = 1
Label Encoding: Convert categories to numbers
One Hot Encoding: Binary representation
Important for machine learning accuracy.
Program:
import pandas as pd
from [Link] import StandardScaler, LabelEncoder
df = [Link]({'Age':[25,30,35],'Gender':['M','F','M']})
le = LabelEncoder()
df['Gender'] = le.fit_transform(df['Gender'])
sc = StandardScaler()
df['Age'] = sc.fit_transform(df[['Age']])
print(df)
Output:
Age Gender
0 -1.224745 1
1 0.000000 0
2 1.224745 1
Experiment – 12: Data Visualization
Aim:
To visualize data using different plots such as bar graphs, pie charts, histograms, line charts,
scatter plots, and box plots using Matplotlib.
Theory:
Visualization helps understand data patterns.
Types:
Bar Graph: Comparison
Pie Chart: Proportions
Histogram: Frequency distribution
Line Chart: Trends
Scatter Plot: Relationship between variables
Box Plot: Outliers & distribution
Library: Matplotlib
Program:
import [Link] as plt
import numpy as np
# Sample Data
x = [Link]([1, 2, 3, 4, 5])
y = [Link]([10, 20, 15, 25, 30])
labels = ['A', 'B', 'C', 'D', 'E']
# 1. Bar Graph
[Link]()
[Link](x, y)
[Link]("Bar Graph")
[Link]("X values")
[Link]("Y values")
[Link]()
# 2. Pie Chart
[Link]()
[Link](y, labels=labels, autopct='%1.1f%%')
[Link]("Pie Chart")
[Link]()
# 3. Histogram
data = [Link]([10, 20, 20, 30, 30, 30, 40, 50])
[Link]()
[Link](data)
[Link]("Histogram")
[Link]()
# 4. Line Chart
[Link]()
[Link](x, y)
[Link]("Line Chart")
[Link]("X values")
[Link]("Y values")
[Link]()
# 5. Scatter Plot
[Link]()
[Link](x, y)
[Link]("Scatter Plot")
[Link]("X values")
[Link]("Y values")
[Link]()
# 6. Box Plot
[Link]()
[Link](data)
[Link]("Box Plot")
[Link]()
Output: