SCHOOL OF COMPUTER SCIENCE AND
ENGINEERING
Python For Data Science lab
B25CS0102
First Semester
AY-2025-26
(Prepared in Sep-2025)
Python For Data Science Lab Subcode: B25CS0102
INDEX
SL. No Contents Page. no
1 Lab Objectives 3
2 Lab Outcomes 3
3 Lab Requirements 3
4 Guidelines to Students 4
5 List of Lab Exercises 5
6 Lab Exercises’ Solutions: 7
PART – A: LAB EXERCISES
7 Session – 1: Lab Exercise 7
8 Session – 2: Lab Exercise 10
9 Session – 3: Lab Exercise 14
10 Session – 4: Lab Exercise 18
11 Session – 5: Lab Exercise 22
12 Session – 6: Lab Exercise 26
13 Session – 7: Lab Exercise 29
14 Session – 8: Lab Exercise 32
15 Session – 9: Lab Exercise 37
16 Session – 10: Lab Exercise 42
School of Computer Science and Engineering Page 2
Python For Data Science Lab Subcode: B25CS0102
1. Lab Objectives:
The objectives of this course are to
1. Provide hands-on experience in writing and executing Python programs with fundamental constructs.
2. Familiarize students with core data structures and modular programming techniques in Python.
3. Enable the handling of structured datasets through file I/O, NumPy arrays, and Pandas data frames.
4. Develop proficiency in cleaning, manipulating, and preparing datasets for analysis.
5. Train students in creating effective data visualizations using Matplotlib and Seaborn.
6. Introduce the process of exploratory data analysis (EDA) and predictive modeling using scikit-learn.
2. Lab Outcomes:
On successful completion of this course; student shall be able to:
CO# Course Outcomes POs PSOs
Write and execute basic Python programs demonstrating variables, PO1, PO2, PO3,
CO1 PSO1
arithmetic operations, input/output, and control structures. PO10
Develop modular code using functions and effectively manipulate built-in PO1, PO2, PO3,
CO2 PSO1
data structures (lists, tuples, sets, dictionaries). PO10
PSO1,
Perform file handling and structured data processing from text and CSV PO1, PO2, PO3,
CO3 PSO2,
files. PO4, PO10
PSO3
PSO1,
Apply NumPy and Pandas to clean, transform, and manipulate datasets PO1, PO2, PO3,
CO4 PSO2,
efficiently. PO4, PO10
PSO3
PSO1,
PO1, PO2, PO3,
CO5 Create and customize data visualizations using Matplotlib and Seaborn. PSO2,
PO4, PO5, PO10
PSO3
PO1, PO2, PO3, PSO1,
CO6 Conduct exploratory data analysis (EDA) on given datasets. PO4, PO5, PO9, PSO2,
PO10 PSO3
BLOOM’S LEVEL OF THE COURSE OUTCOMES
Bloom’s Level
CO#
Remember (L1) Understand (L2)
Apply(L3) Analyze(L4) Evaluate(L5) Create(L6)
CO1 √
CO2 √
CO3 √
CO4 √
CO5 √
CO6 √
School of Computer Science and Engineering Page 3
Python For Data Science Lab Subcode: B25CS0102
COURSE ARTICULATION MATRIX
PSO1
PSO2
PSO3
PO10
PO11
PO12
CO#/
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
POs
CO1 3 2 1 1 3 3 3
CO2 3 3 2 1 3 3 3
CO3 2 3 2 1 1 3 2 2 2
CO4 2 3 2 2 1 3 3 2 2
CO5 2 2 2 2 1 2 3 3 2 2
CO6 2 3 3 3 2 1 2 3 3 2 2
C07 2 3 3 3 2 2 1 2 3 3 2 2
C08 2 3 3 3 3 2 1 2 3 1 3 3 2 2
3. Lab Requirements:
The following are the required hardware and software for this lab.
Following are the required hardware and software for this lab, which is available in the laboratory.
[Link] Requirements
Computer system with minimum configuration:
o Processor: Intel i3 / AMD equivalent or higher
o RAM: 4 GB or above (8 GB recommended)
o Storage: 100 GB free space
o Monitor, keyboard, and mouse
2. Software Requirements
Operating System: Windows 10 / Linux (Ubuntu) / macOS
Python Interpreter: Python 3.X or above
IDE / Editors (any one or more):
o IDLE (default with Python)
o Google Colab, / Jupyter Notebook / Anaconda
Required Packages (can be installed via pip):
o numpy
o pandas
o matplotlib
o seaborn (optional)
o scikit-learn (if ML experiments are included)
School of Computer Science and Engineering Page 4
Python For Data Science Lab Subcode: B25CS0102
.
4. Guidelines to Students:
Equipment in the lab for the use of the student community. Students need to maintain a proper decorum in
the computer lab. Students must use the equipment with care. Any damage caused is punishable.
Students are required to carry their observation / programs book with completed exercises while entering the
lab.
Students are supposed to occupy the machines allotted to them and are not supposed to talk or make noise in
the lab. The allocation is put up on the lab notice board.
The lab can be used in free time / lunch hours by the students who need to use the systems should get prior
permission from the lab in-charge.
Lab records need to be submitted on or before the date of submission.
Students are not supposed to use flash drives.
Practice
Here are some key activities involved in the developing any Python Programming:
Developing a Python program typically involves the following steps:
Problem Definition
Clearly understand the problem to be solved.
Identify inputs, processing steps, and expected outputs.
Algorithm Design
Write step-by-step instructions (algorithm) to solve the problem.
Optionally create a flowchart or pseudocode for better clarity.
Program Development (Coding)
Translate the algorithm into Python code.
Use proper syntax, variables, operators, control structures, and functions.
Testing and Debugging
Execute the program with test data.
Check if the program works correctly.
Debug errors (syntax, runtime, or logical errors).
Validation
Ensure the program works correctly for different sets of inputs (including edge cases).
Compare output with expected results.
Documentation
Add comments, docstrings, and meaningful variable names.
Prepare user documentation if required.
School of Computer Science and Engineering Page 5
Python For Data Science Lab Subcode: B25CS0102
Execution and Deployment
Run the program in the required environment.
Deploy for end-users if it’s an application.
Maintenance and Improvement
Update the program as requirements change.
Optimize for performance and readability.
Title of Exercise:
Title of the Tools and
Sl. No Expected Skills / Ability
Experiment Techniques
Writing basic Python Jupyter Understand basic syntax,
programs (Hello Notebook / variables, operators, and
1
World, arithmetic Google Colab, execution of simple
operations) Python 3.x programs.
Jupyter
Implementing Apply decision-making
Notebook /
control structures (if/else), looping constructs,
2 Google Colab,
and functions in and modular programming
Python IDE (VS
Python using functions.
Code)
Gain proficiency in using
Manipulating lists, Python built-in
and modifying Python’s core
3 tuples, sets, and data structures,
data structures for problem-
dictionaries IDE/Colab
solving.
Handle file operations,
Reading and writing
Python I/O perform basic data
4 data from text and
functions, Pandas input/output processing with
CSV files
structured files.
Performing array Develop ability to create,
NumPy library in
5 operations using reshape, and compute with
Jupyter / Colab
NumPy numerical arrays.
Data manipulation Pandas library Perform dataset cleaning,
6 and cleaning using (Series & filtering, aggregation, and
Pandas DataFrames Basic preprocessing tasks.
School of Computer Science and Engineering Page 6
Python For Data Science Lab Subcode: B25CS0102
Title of the Tools and
Sl. No Expected Skills / Ability
Experiment Techniques
Creating various Create line, bar, scatter, pie,
Matplotlib,
7 plots with Matplotlib box plots with customization
Seaborn libraries
and Seaborn for visualization.
Exploratory Data Perform descriptive
Pandas, Seaborn,
8 Analysis (EDA) on statistical analysis, visualize
Matplotlib
sample datasets patterns, detect outliers.
scikit-learn
Building a simple Develop skills to train, test
(Linear
9 predictive model and evaluate simple ML
Regression,
with scikit-learn models using Python.
Classification)
Mini project: Jupyter Notebook, Integrate skills in data
Analyzing a real- Pandas, NumPy, collection, cleaning,
10
world dataset end-to- Seaborn, scikit- analysis, visualization, and
end learn model building.
School of Computer Science and Engineering Page 7
Python For Data Science Lab Subcode: B25CS0102
LAB EXERCISES
5. Lab Exercises’ Solutions:
Program 1: Writing basic Python programs (Hello World, Arithmetic operations)
School of Computer Science and Engineering Page 8
Python For Data Science Lab Subcode: B25CS0102
1. Setting Up a Scientific Python Environment
Step 1: Install Python
• Download and install the latest stable version of Python from the official Python website.
• Alternatively, use the Anaconda distribution, which includes many scientific libraries
(Download Anaconda).
To maintain an isolated and manageable workspace, use virtual environments:
python -m venv sci-env
source sci-env/bin/activate # On Windows: sci-env\Scripts\activate
If using Conda:
conda create --name sci-env python=3.9
conda activate sci-env
Step 3: Install Essential Scientific Libraries
Run the following in Python:
# Program to print Hello World
print("Hello, World!")
# Program to perform basic arithmetic operations
# Addition
num1 = 10
num2 = 5
sum_result = num1 + num2
print(f"Addition: {num1} + {num2} = {sum_result}")
# Subtraction
difference = num1 - num2
print(f"Subtraction: {num1} - {num2} = {difference}")
# Multiplication
product = num1 * num2
print(f"Multiplication: {num1} * {num2} = {product}")
# Division
quotient = num1 / num2
print(f"Division: {num1} / {num2} = {quotient}")
School of Computer Science and Engineering Page 9
Python For Data Science Lab Subcode: B25CS0102
# Floor Division (returns the integer part of the quotient)
floor_quotient = num1 // num2
print(f"Floor Division: {num1} // {num2} = {floor_quotient}")
# Modulo (returns the remainder of the division)
remainder = num1 % num2
print(f"Modulo: {num1} % {num2} = {remainder}")
# Exponentiation (power)
power_result = num1 ** num2 # 10 to the power of 5
print(f"Power: {num1} ** {num2} = {power_result}")
Sample Output
Hello, World!
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2.0
Floor Division: 10 // 5 = 2
Modulo: 10 % 5 = 0
Power: 10 ** 5 = 100000
2. Useful References for Self-Reading
Online Courses
• Python for Data Science and Machine Learning Bootcamp – Udemy
• Scientific Computing with Python – freeCodeCamp
• MIT OpenCourseWare – Computational Thinking and Data Science
Books
• "Python Data Science Handbook" by Jake VanderPlas
• "Think Python" by Allen B. Downey
• "Automate the Boring Stuff with Python" by Al Sweigart
Viva/Interview Questions:
What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simplicity, readability, and wide range of
libraries.
2. How do you write the first Python program (Hello World)?
print("Hello, World!")
School of Computer Science and Engineering Page 10
Python For Data Science Lab Subcode: B25CS0102
It uses the built-in print() function to display output on the screen.
3. What are variables in Python?
Variables are names used to store data values. Python does not require explicit declaration of variables; a variable is created when a
value is assigned.
Example:
x = 10
name = "Alice"
4. What are arithmetic operators in Python?
+ → Addition
- → Subtraction
* → Multiplication
/ → Division (returns float)
// → Floor Division (returns integer)
% → Modulus (remainder)
** → Exponentiation (power)
5. What is the difference between / and // in Python?
/ → True division, returns result as a float (e.g., 5 / 2 = 2.5).
// → Floor division, returns result as an integer (e.g., 5 // 2 = 2).
6. What function is used to take user input in Python?
The input() function is used to accept user input as a string.
Example:
name = input("Enter your name: ")
7. How do you convert input values into integers or floats?
By using type casting functions:
int() → converts input to integer.
float() → converts input to float.
Example:
a = int(input("Enter a number: "))
b = float(input("Enter a decimal: "))
8. What is the difference between print() and input() in Python?
print() → Displays output on the screen.
input() → Accepts input from the user.
9. What is the difference between a compiler and an interpreter?
Compiler → Translates the entire code into machine language at once.
Interpreter → Translates and executes code line by line.
Python uses an interpreter.
10. What happens if you divide an integer by zero in Python?
It raises a ZeroDivisionError.
Program 2 Implementing control structures and functions in Python
Problem Statement: Implement and demonstrate the use of control structures (if, if-else, if-
School of Computer Science and Engineering Page 11
Python For Data Science Lab Subcode: B25CS0102
elif-else, for loop, while loop) and functions in Python.
Solution Overview: The problem is solved by demonstrating the use of decision-making
statements (if, if-else, if-elif-else), iterative constructs (for, while), and functions in Python.
Used if statements to check conditions and execute code accordingly.
Implemented loops (for, while) to perform repetitive tasks.
Defined user-defined functions with and without parameters/return values to organize code into
reusable modules.
Intuition:
In real-world programming, we often need to make decisions (e.g., check if a number is
positive or negative). Control structures allow us to handle such conditions.
We also need to repeat tasks (e.g., printing numbers 1 to 10), which is achieved using loops.
Functions provide a way to avoid code duplication and make the program more modular,
readable, and reusable.
By combining these concepts, we can write efficient and structured programs.
Demonstrate the following Conditional statements in Python with suitable examples.
1. Conditional Statements (if, if-else, if-elif-else)
# if statement
x = 10
if x > 5:
print("x is greater than 5")
# if-else statement
y=3
if y % 2 == 0:
print("y is even")
else:
print("y is odd")
# if-elif-else statement
grade = 85
if grade >= 90:
print("Grade: A")
elif grade >= 80:
School of Computer Science and Engineering Page 12
Python For Data Science Lab Subcode: B25CS0102
print("Grade: B")
elif grade >= 70:
print("Grade: C")
else:
print("Grade: F")
2. Loops (for loop, while loop)
# for loop
print("Numbers from 0 to 4:")
for i in range(5):
print(i)
# while loop
count = 0
print("Counting to 3:")
while count < 3:
print(count)
count += 1
Sample Output:
x is greater than 5
y is odd
Grade: B
Numbers from 0 to 4:
0
1
2
3
4
Counting to 3:
0
1
2
3. Functions
# Defining a function with no arguments and no return value
def greet():
print("Hello, world!")
School of Computer Science and Engineering Page 13
Python For Data Science Lab Subcode: B25CS0102
# Calling the function
greet()
# Defining a function with arguments
def add_numbers(a, b):
sum_result = a + b
print(f"The sum of {a} and {b} is {sum_result}")
# Calling the function with arguments
add_numbers(5, 7)
# Defining a function with a return value
def multiply_numbers(x, y):
product = x * y
return product
# Calling the function and storing the return value
result = multiply_numbers(4, 6)
print(f"The product is: {result}")
Sample Output:
Hello, world!
The sum of 5 and 7 is 12
The product is: 24
Viva/Interview Questions:
[Link] are control structures in Python?
Control structures are constructs that control the flow of execution in a program. They include:
Decision-making: if, if-else, if-elif-else
Loops: for, while
2. What is the difference between if, if-else, and if-elif-else?
if → Executes a block of code if the condition is true.
if-else → Executes one block if condition is true, another if false.
if-elif-else → Checks multiple conditions in sequence and executes the first matching block.
3. What is the difference between for loop and while loop in Python?
for loop → Iterates over a sequence (like list, tuple, string, range).
while loop → Repeats until a condition becomes false.
4. What is the use of the range() function in loops?
range() generates a sequence of numbers, often used in for loops.
Example:
for i in range(5):
print(i) # 0,1,2,3,4
School of Computer Science and Engineering Page 14
Python For Data Science Lab Subcode: B25CS0102
5. What is the difference between break, continue, and pass?
break → Exits the loop immediately.
continue → Skips the current iteration and moves to the next.
pass → Placeholder statement that does nothing (used to avoid errors in empty blocks).
6. What are functions in Python?
Functions are reusable blocks of code that perform a specific task. Defined using the def keyword.
Example:
def greet(name):
return "Hello, " + name
7. What is the difference between built-in functions and user-defined functions?
Built-in functions → Already provided by Python (e.g., len(), print(), sum()).
User-defined functions → Created by the programmer using def.
Program 3: Manipulating lists, tuples, sets, and dictionaries
Problem Statement
Write Python programs to create and manipulate lists, tuples, sets, and dictionaries, and
demonstrate common operations on each data structure.
Intuition
Lists: Demonstrated creation, indexing, slicing, appending, and removing elements.
Tuples: Showed immutability and accessing elements.
Sets: Used to eliminate duplicates and perform union, intersection, and difference.
Dictionaries: Implemented key-value pair operations like insertion, deletion, and update.
Implementation
(a) List Operations
# Creating and manipulating a list
fruits = ["apple", "banana", "cherry"]
print("Original List:", fruits)
[Link]("orange") # Add element
[Link]("banana") # Remove element
fruits[1] = "grapes" # Update element
print("Updated List:", fruits)
print("Sliced List:", fruits[0:2]) # Slicing
Sample Output
Original List: ['apple', 'banana', 'cherry']
School of Computer Science and Engineering Page 15
Python For Data Science Lab Subcode: B25CS0102
Updated List: ['apple', 'grapes', 'orange']
Sliced List: ['apple', 'grapes']
(b) Tuple Operations
# Creating and accessing a tuple
tup = (10, 20, 30, 40,’hi’,’python’)
print("Tuple:", tup)
print("First element:", tup[0])
print("Last element:", tup[-1])
# Tuple slicing
print (tup[1:])
print (tup[0:1])
# Tuple concatenation using + operator
print (tup + tup)
# Tuple repatation using * operator
print (tup * 3)
Sample Output:
Tuple: (10, 20, 30, 40, 'hi', 'python')
First element: 10
Last element: python
(20, 30, 40, 'hi', 'python')
(10,)
(10, 20, 30, 40, 'hi', 'python', 10, 20, 30, 40, 'hi', 'python')
(10, 20, 30, 40, 'hi', 'python', 10, 20, 30, 40, 'hi', 'python', 10, 20, 30, 40, 'hi', 'python')
(c) Set Operations
set1 = {'James', 2, 3,'Python'}
#Printing Set value
print(set1)
# Adding element to the set
[Link](10)
print(set1)
#Removing element from the set
School of Computer Science and Engineering Page 16
Python For Data Science Lab Subcode: B25CS0102
[Link](2)
print(set1)
Sample Output:
{'Python', 2, 3, 'James'}
{2, 3, 10, 'Python', 'James'}
{3, 10, 'Python', 'James'}
(d) Dictionary Operations:
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'} # Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1]) print("2nd name is "+ d[4])
print ([Link]())
print ([Link]())
Sample Output:
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'} # Printing dictionary
print (d)
# Accesing value using keys
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print ([Link]())
print ([Link]())
Viva Questions and Answers
What are the differences between lists, tuples, sets, and dictionaries?
List → Ordered, mutable, allows duplicates → [1, 2, 3]
Tuple → Ordered, immutable, allows duplicates → (1, 2, 3)
Set → Unordered, mutable, no duplicates → {1, 2, 3}
Dictionary → Unordered, mutable, stores key–value pairs → {"name": "Alice", "age": 25}
2. How do you create a list and access its elements?
my_list = [10, 20, 30, 40]
print(my_list[0]) # 10 (first element)
3. What is the difference between append() and extend() in lists?
append() → Adds a single element to the end of the list.
extend() → Adds multiple elements from another list (or iterable).
4. What are common list operations?
Adding → append(), insert(), extend()
Removing → remove(), pop(), clear()
Slicing → my_list[1:3]
Sorting → sort(), reverse()
School of Computer Science and Engineering Page 17
Python For Data Science Lab Subcode: B25CS0102
5. What is the key difference between a list and a tuple?
List → Mutable (can be changed).
Tuple → Immutable (cannot be changed after creation).
6. What are sets mainly used for in Python?
To store unique elements (no duplicates).
For set operations like union, intersection, and difference.
Program 4: Reading and writing data from text and CSV files.
Problem Statement
Write Python programs to read and write data from text files and CSV files using file
handling operations.
Intuition
Used Python’s built-in open() function for text file handling.
Performed read, write, and append operations on text files.
Used the csv module to read data from and write data to CSV files.
Demonstrated handling file objects with with statement for better resource management.
with open("[Link]", "w") as f:
[Link]("Hello, World!\n")
6. What is a CSV file?
CSV (Comma-Separated Values) is a simple file format used to store tabular data (like spreadsheets).
Each line represents a record, and fields Code
(a) Writing and Reading a Text File
# Writing to a text file
with open("[Link]", "w") as f:
[Link]("Hello, this is a text file.\n")
[Link]("Python makes file handling easy.\n")
# Reading from a text file
with open("[Link]", "r") as f:
content = [Link]()
print("Contents of text file:\n", content)
(b) Appending Data to a Text File
with open("[Link]", "a") as f:
[Link]("This line was appended.\n")
# Reading from a text file
with open("[Link]", "r") as f:
School of Computer Science and Engineering Page 18
Python For Data Science Lab Subcode: B25CS0102
content = [Link]()
print("Contents of text file:\n", content)
(c) Writing to a CSV File
import csv
# Writing data to a CSV file
data = [
["ID", "Name", "Marks"],
[1, "Alice", 85],
[2, "Bob", 78],
[3, "Charlie", 92]
]
with open("[Link]", "w", newline="") as f:
writer = [Link](f)
[Link](data)
(d) Reading from a CSV File
with open("[Link]", "r") as f:
reader = [Link](f)
print("CSV File Data:")
for row in reader:
print(row)
Sample Output
Text File Content:
Hello, this is a text file.
Python makes file handling easy.
This line was appended.
CSV File Data:
['ID', 'Name', 'Marks']
['1', 'Alice', '85']
['2', 'Bob', '78']
['3', 'Charlie', '92']
School of Computer Science and Engineering Page 19
Python For Data Science Lab Subcode: B25CS0102
D) Python program to count frequency of characters in a given file.
filename = input("Enter a file name : ")
f1 = open(filename,'w')
[Link]('Computer science Engineering\n')
[Link]('Elecronics and Communication Engineering\n')
[Link]('Civil Engineering\n')
[Link]()
f1 = open(filename,'r')
data = [Link]()
a =list(set(data))
sorted(a)
print(a)
[Link]()
for i in a:
print("{} as occured {} times".format(i,[Link](i)))
Sample Output:
Enter a file name : [Link]
['t', 'm', 's', ' ', 'u', 'c', '\n', 'e', 'i', 'r', 'l', 'v', 'o', 'E', 'g', 'C', 'd', 'p', 'a', 'n']
t as occured 2 times
m as occured 3 times
s as occured 2 times
as occured 6 times
u as occured 2 times
c as occured 5 times
as occured 3 times
e as occured 10 times
i as occured 12 times
r as occured 5 times
l as occured 2 times
v as occured 1 times
o as occured 4 times
E as occured 4 times
g as occured 6 times
School of Computer Science and Engineering Page 20
Python For Data Science Lab Subcode: B25CS0102
C as occured 3 times
d as occured 1 times
p as occured 1 times
a as occured 2 times
n as occured 14 times
Viva Questions and Answers
1. What is file handling in Python?
File handling allows Python programs to read from and write to files stored on the disk. It is done using the built-in open()
function.
2. What are the modes available in the open() function?
'r' → Read (default, error if file not found)
'w' → Write (creates new file/overwrites existing)
'a' → Append (adds data to the end of the file)
'b' → Binary mode
'x' → Create (fails if file already exists)
'+' → Update (read and write)
Example:
f = open("[Link]", "r")
3. What is the difference between read(), readline(), and readlines()?
read() → Reads the entire file.
readline() → Reads one line at a time.
readlines() → Reads all lines into a list.
4. Why is it recommended to use with open() in Python?
Because it automatically closes the file after reading or writing, even if an error occurs.
Example:
with open("[Link]", "r") as f:
content = [Link]()
5. How do you write data to a text file in Python?
Using write() or writelines().
are separated by commas.
School of Computer Science and Engineering Page 21
Python For Data Science Lab Subcode: B25CS0102
Program 5: Performing array operations using NumPy
Problem Statement
Write Python programs using NumPy to create arrays and perform basic array operations such as
arithmetic, slicing, reshaping, aggregation, and statistical functions.
Intuition
Created 1D and 2D arrays using [Link]() and [Link]().
Performed arithmetic operations (addition, subtraction, multiplication, division).
Demonstrated slicing and reshaping of arrays.
Used aggregation functions like sum(), mean(), min(), max().
.
Code
(a) Creating Arrays and Basic Arithmetic Operations
import numpy as np
# Creating arrays
a = [Link]([1, 2, 3, 4, 5])
b = [Link]([10, 20, 30, 40, 50])
print("Array a:", a)
print("Array b:", b)
# Arithmetic operations
print("Addition:", a + b)
print("Subtraction:", b - a)
print("Multiplication:", a * b)
print("Division:", b / a)
(b) Array Slicing and Reshaping
# Slicing
print("First three elements of a:", a[:3])
print("Last two elements of b:", b[-2:])
# Reshaping
School of Computer Science and Engineering Page 22
Python For Data Science Lab Subcode: B25CS0102
c = [Link](1, 10) # Creates array [1..9]
print("Original array:", c)
print("Reshaped to 3x3 matrix:\n", [Link](3, 3))
(c) Aggregation and Statistical Functions
d = [Link]([5, 10, 15, 20, 25])
print("Sum:", [Link](d))
print("Mean:", [Link](d))
print("Minimum:", [Link](d))
print("Maximum:", [Link](d))
print("Standard Deviation:", [Link](d))
Sample Output
Array a: [1 2 3 4 5]
Array b: [10 20 30 40 50]
Addition: [11 22 33 44 55]
Subtraction: [ 9 18 27 36 45]
Multiplication: [ 10 40 90 160 250]
Division: [10. 10. 10. 10. 10.]
First three elements of a: [1 2 3]
Last two elements of b: [40 50]
Original array: [1 2 3 4 5 6 7 8 9]
Reshaped to 3x3 matrix:
[[1 2 3]
[4 5 6]
[7 8 9]]
Sum: 75
Mean: 15.0
Minimum: 5
Maximum: 25
Standard Deviation: 7.0710678118654755
School of Computer Science and Engineering Page 23
Python For Data Science Lab Subcode: B25CS0102
Viva Questions and Answers
What is NumPy in Python?
NumPy (Numerical Python) is a Python library used for fast mathematical computations. It provides support for
multidimensional arrays and a wide range of mathematical, statistical, and linear algebra functions.
2. What is the difference between a Python list and a NumPy array?
List → General-purpose, slower, supports different data types.
NumPy Array → Homogeneous (same type), faster, supports element-wise operations and advanced mathematical
functions.
[Link] are some aggregation functions in NumPy?
[Link]() → Sum of elements
[Link]() → Average
[Link]() / [Link]() → Maximum/Minimum
[Link]() → Standard deviation
4. What is broadcasting in NumPy?
Broadcasting allows operations between arrays of different shapes by automatically expanding the smaller array.
Example:
arr = [Link]([1, 2, 3])
print(arr + 5) # [6 7 8]
5. What is the difference between [Link]() and [Link]()?
[Link](start, stop, step) → Generates values with a step size.
[Link](start, stop, num) → Generates a given number of evenly spaced values.
6. Why is NumPy faster than lists?
Because NumPy uses contiguous memory storage and is implemented in C, allowing vectorized operations without explicit
Python loops.
Program 6: Data manipulation and cleaning using Pandas
Problem Statement
Write Python programs using NumPy to create and manipulate arrays, and perform operations
Intuition
1. Create a Sample DataFrame
2. Handle Missing Values
3. Handle Duplicates
4. Data Type Conversion
5. Renaming Columns
6. String Manipulation
7. Filtering Data
8. Grouping and Aggregation
Code Implementation:
import pandas as pd
import numpy as np
School of Computer Science and Engineering Page 24
Python For Data Science Lab Subcode: B25CS0102
# 1. Create a sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Heidi'],
'Age': [24, 30, [Link], 28, 22, 35, 29, 40],
'City': ['New York', 'Los Angeles', 'Chicago', 'New York', 'Boston', 'Los Angeles', 'Chicago', ''],
'Salary': [50000, 60000, 55000, 62000, [Link], 70000, 58000, 75000],
'Department': ['IT', 'HR', 'IT', 'Sales', 'HR', 'IT', 'Sales', 'IT']
}
df = [Link](data)
print("Original DataFrame:")
print(df)
print("\n" + "="*30 + "\n")
# 2. Handle Missing Values
# Check for missing values
print("Missing values before cleaning:")
print([Link]().sum())
# Fill missing 'Age' with the mean
df['Age'].fillna(df['Age'].mean(), inplace=True)
# Fill missing 'Salary' with a specific value (e.g., 0)
df['Salary'].fillna(0, inplace=True)
# Drop rows where 'City' is an empty string
df = df[df['City'] != '']
print("\nDataFrame after handling missing values:")
print(df)
print("\n" + "="*30 + "\n")
# 3. Handle Duplicates (if any)
# Create a DataFrame with duplicates for demonstration
df_dup = [Link]({'A': [1, 2, 2, 3], 'B': ['x', 'y', 'y', 'z']})
School of Computer Science and Engineering Page 25
Python For Data Science Lab Subcode: B25CS0102
print("DataFrame with duplicates:")
print(df_dup)
df_dup.drop_duplicates(inplace=True)
print("\nDataFrame after dropping duplicates:")
print(df_dup)
print("\n" + "="*30 + "\n")
# 4. Data Type Conversion
# Convert 'Age' to integer type (after filling NaNs)
df['Age'] = df['Age'].astype(int)
print("DataFrame after converting 'Age' to int:")
print([Link])
print("\n" + "="*30 + "\n")
# 5. Renaming Columns
[Link](columns={'Department': 'Dept'}, inplace=True)
print("DataFrame after renaming 'Department' column:")
print([Link]())
print("\n" + "="*30 + "\n")
# 6. String Manipulation (e.g., cleaning 'Name')
df['Name'] = df['Name'].[Link]() # Convert names to uppercase
print("DataFrame after converting 'Name' to uppercase:")
print([Link]())
print("\n" + "="*30 + "\n")
# 7. Filtering Data
it_employees = df[df['Dept'] == 'IT']
print("IT Department Employees:")
print(it_employees)
print("\n" + "="*30 + "\n")
# 8. Grouping and Aggregation
avg_salary_by_dept = [Link]('Dept')['Salary'].mean()
School of Computer Science and Engineering Page 26
Python For Data Science Lab Subcode: B25CS0102
print("Average Salary by Department:")
print(avg_salary_by_dept)
Sample Output
Original DataFrame:
Name Age City Salary Department
0 Alice 24.0 New York 50000.0 IT
1 Bob 30.0 Los Angeles 60000.0 HR
2 Charlie NaN Chicago 55000.0 IT
3 David 28.0 New York 62000.0 Sales
4 Eve 22.0 Boston NaN HR
5 Frank 35.0 Los Angeles 70000.0 IT
6 Grace 29.0 Chicago 58000.0 Sales
7 Heidi 40.0 75000.0 IT
==============================
Missing values before cleaning:
Name 0
Age 1
City 0
Salary 1
Department 0
dtype: int64
DataFrame after handling missing values:
Name Age City Salary Department
0 Alice 24.000000 New York 50000.0 IT
1 Bob 30.000000 Los Angeles 60000.0 HR
2 Charlie 29.714286 Chicago 55000.0 IT
3 David 28.000000 New York 62000.0 Sales
4 Eve 22.000000 Boston 0.0 HR
5 Frank 35.000000 Los Angeles 70000.0 IT
6 Grace 29.000000 Chicago 58000.0 Sales
School of Computer Science and Engineering Page 27
Python For Data Science Lab Subcode: B25CS0102
==============================
DataFrame with duplicates:
A B
0 1 x
1 2 y
2 2 y
3 3 z
DataFrame after dropping duplicates:
A B
0 1 x
1 2 y
3 3 z
==============================
DataFrame after converting 'Age' to int:
Name object
Age int32
City object
Salary float64
Department object
dtype: object
==============================
DataFrame after renaming 'Department' column:
Name Age City Salary Dept
0 Alice 24 New York 50000.0 IT
1 Bob 30 Los Angeles 60000.0 HR
2 Charlie 29 Chicago 55000.0 IT
3 David 28 New York 62000.0 Sales
4 Eve 22 Boston 0.0 HR
School of Computer Science and Engineering Page 28
Python For Data Science Lab Subcode: B25CS0102
==============================
DataFrame after converting 'Name' to uppercase:
Name Age City Salary Dept
0 ALICE 24 New York 50000.0 IT
1 BOB 30 Los Angeles 60000.0 HR
2 CHARLIE 29 Chicago 55000.0 IT
3 DAVID 28 New York 62000.0 Sales
4 EVE 22 Boston 0.0 HR
==============================
IT Department Employees:
Name Age City Salary Dept
0 ALICE 24 New York 50000.0 IT
2 CHARLIE 29 Chicago 55000.0 IT
5 FRANK 35 Los Angeles 70000.0 IT
==============================
Average Salary by Department:
Dept
HR 30000.000000
IT 58333.333333
Sales 60000.000000
Name: Salary, dtype: float64
Viva Questions and Answers
[Link] is Pandas in Python?
Pandas is a Python library used for data manipulation and analysis. It provides two main data structures:
Series → 1D labeled array
DataFrame → 2D labeled data structure (rows & columns like Excel/SQL table)
2. What is the difference between NumPy arrays and Pandas DataFrames?
NumPy Array → Homogeneous (same data type), supports numerical operations.
Pandas DataFrame → Heterogeneous (different data types), supports labeled data, missing value handling, grouping, and
advanced manipulation
[Link] is data cleaning important?
Because raw datasets often contain missing values, duplicates, inconsistent types, or errors. Cleaning ensures accuracy,
reliability, and quality for analysis or machine learning models.
School of Computer Science and Engineering Page 29
Python For Data Science Lab Subcode: B25CS0102
Program 7: Creating various plots with Matplotlib and Seaborn
Problem Statement:
Write Python programs to create and visualize different types of plots using Matplotlib and
Seaborn libraries. Demonstrate basic plots such as line plots, bar charts, histograms, scatter plots,
and advanced plots like boxplots and heatmaps for better understanding of datasets.
Intuition:
Visualization is one of the most powerful ways to understand data patterns and trends.
Matplotlib provides low-level control to build a wide variety of static plots.
Seaborn is built on top of Matplotlib and makes it easier to create aesthetically pleasing,
statistical plots with fewer lines of code.
By creating different types of plots, we can:
Compare values across categories (bar charts).
Show relationships between variables (scatter plots).
Understand distribution of data (histograms, boxplots).
Identify correlations and patterns (heatmaps).
This experiment builds intuition on how visualization helps in Exploratory Data Analysis
(EDA) and decision-making.
Code Implementation:
import [Link] as plt
import seaborn as sns
import numpy as np
import pandas as pd
# Sample Data
x = [Link](0, 10, 50)
y = [Link](x)
# 1. Line Plot (Matplotlib)
[Link](x, y, label="sin(x)", color="blue", marker="o")
[Link]("Line Plot - Sine Function")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
[Link]()
School of Computer Science and Engineering Page 30
Python For Data Science Lab Subcode: B25CS0102
# 2. Bar Chart (Matplotlib)
categories = ['A', 'B', 'C', 'D']
values = [10, 24, 36, 18]
[Link](categories, values, color="orange")
[Link]("Bar Chart Example")
[Link]("Categories")
[Link]("Values")
[Link]()
# 3. Histogram (Matplotlib)
data = [Link](1000) # normal distribution
[Link](data, bins=30, color="green", edgecolor="black")
[Link]("Histogram Example")
[Link]("Value")
[Link]("Frequency")
[Link]()
# 4. Scatter Plot (Seaborn)
df = sns.load_dataset("iris")
[Link](x="sepal_length", y="sepal_width", hue="species", data=df)
[Link]("Scatter Plot - Iris Dataset")
[Link]()
# 5. Box Plot (Seaborn)
[Link](x="species", y="petal_length", data=df, palette="Set2")
[Link]("Box Plot - Petal Length by Species")
[Link]()
# 6. Pair Plot (Seaborn)
[Link](df, hue="species")
[Link]("Pair Plot - Iris Dataset", y=1.02)
[Link]()
School of Computer Science and Engineering Page 31
Python For Data Science Lab Subcode: B25CS0102
Sample Output
School of Computer Science and Engineering Page 32
Python For Data Science Lab Subcode: B25CS0102
Viva Questions and Answers
1. What is the purpose of data visualization?
Data visualization helps in representing data graphically so patterns, trends, and insights can be understood easily.
2. What is Matplotlib?
Matplotlib is a Python plotting library used to create static, interactive, and animated visualizations such as line plots, bar
charts, histograms, etc.
3. What is Seaborn?
Seaborn is a statistical data visualization library built on top of Matplotlib. It provides high-level functions for creating attractive
and informative plots (like heatmaps, pairplots, violin plots).
4. What is the difference between Matplotlib and Seaborn?
Matplotlib → Low-level, gives full control over plot design, but requires more code.
Seaborn → High-level, simpler syntax, better default styles, and built-in support for statistical plo
What is the difference between a histogram and a bar chart?
Histogram → Shows the distribution of continuous data (bins of ranges).
Bar Chart → Shows comparison among categories (discrete values).
5. What is a heatmap in Seaborn?
A heatmap is a graphical representation of data using color coding to represent values, often used to visualize
correlations.
6. Why is visualization important in data analysis?
Because it helps to:
Detect patterns and outliers
Understand data distribution
Communicate insights clearly
Support better decision-making
School of Computer Science and Engineering Page 33
Python For Data Science Lab Subcode: B25CS0102
Program 8: Exploratory Data Analysis (EDA) on sample datasets
Problem Statement
Perform Exploratory Data Analysis (EDA) on a sample dataset using Pandas, Matplotlib, and
Seaborn to understand the structure, detect missing values, visualize distributions, and discover
patterns in the data.
Intuition:
Loaded a dataset (Iris / Titanic) from Seaborn.
Performed data inspection (head(), info(), describe()).
Checked for missing values and data types.
Conducted univariate, bivariate, and multivariate analysis.
Used plots (histogram, scatter, boxplot, heatmap, pairplot) to extract insights.
Code Implementation:
import pandas as pd
import seaborn as sns
import [Link] as plt
# Load sample dataset (Iris dataset)
df = sns.load_dataset("iris")
# 1. Basic Inspection
print("First 5 rows of dataset:\n", [Link]())
print("\nDataset Info:")
print([Link]())
print("\nSummary Statistics:")
print([Link]())
# 2. Check Missing Values
print("\nMissing values in dataset:")
print([Link]().sum())
# 3. Univariate Analysis - Distribution
[Link](df["sepal_length"], kde=True, bins=20, color="blue")
[Link]("Distribution of Sepal Length")
School of Computer Science and Engineering Page 34
Python For Data Science Lab Subcode: B25CS0102
[Link]()
# 4. Bivariate Analysis - Scatter Plot
[Link](x="sepal_length", y="petal_length", hue="species", data=df)
[Link]("Sepal vs Petal Length by Species")
[Link]()
# 5. Multivariate Analysis - Pair Plot
[Link](df, hue="species")
[Link]("Pair Plot of Iris Features", y=1.02)
[Link]()
# 6. Correlation Heatmap
corr = [Link]()
[Link](corr, annot=True, cmap="coolwarm")
[Link]("Correlation Heatmap")
[Link]()
Sample Output:
First 5 rows of dataset:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
Dataset Info:
<class '[Link]'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal_length 150 non-null float64
School of Computer Science and Engineering Page 35
Python For Data Science Lab Subcode: B25CS0102
1 sepal_width 150 non-null float64
2 petal_length 150 non-null float64
3 petal_width 150 non-null float64
4 species 150 non-null object
dtypes: float64(4), object(1)
memory usage: 6.0+ KB
None
Summary Statistics:
sepal_length sepal_width petal_length petal_width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
Missing values in dataset:
sepal_length 0
sepal_width 0
petal_length 0
petal_width 0
species 0
dtype: int64
Viva Questions and Answers
1. What is Exploratory Data Analysis (EDA)?
EDA is the process of analyzing datasets to summarize their main characteristics, often using visualizations and statistical
methods, before applying machine learning or advanced analytics.
2. Why is EDA important?
Helps in understanding data distribution.
Identifies missing values, outliers, and errors.
Provides insights into relationships between variables.
School of Computer Science and Engineering Page 36
Python For Data Science Lab Subcode: B25CS0102
Guides the choice of data preprocessing and model selection.
3. What are the main steps in EDA?
1. Loading and inspecting the dataset.
2. Checking for missing values and duplicates.
3. Understanding data types and structure.
4. Statistical summary (mean, median, std).
5. Visualization of distributions (histogram, boxplot).
6. Detecting correlations and patterns (heatmaps, scatter plots).
4. Which Python libraries are commonly used for EDA?
Pandas → Data manipulation and summary.
NumPy → Numerical operations.
Matplotlib → Basic visualizations.
Seaborn → Statistical and advanced visualizations.
5. How do you detect missing values in Pandas?
[Link]().sum()
6. What is the difference between describe() and info() in Pandas?
[Link]() → Provides data types, non-null counts, and memory usage.
[Link]() → Provides statistical summary (mean, std, min, max, quartiles) of numeric columns.
7. What is the difference between univariate, bivariate, and multivariate analysis?
Univariate → Analyzing a single variable (e.g., histogram).
Bivariate → Analyzing the relationship between two variables (e.g., scatter plot).
Multivariate → Analyzing more than two variables (e.g., pairplot, heatmap).
8. What visualization is best for detecting outliers?
Boxplot is commonly used to detect outliers.
9. What does a correlation heatmap show?
It shows the degree of relationship between numeric variables using colors. Values range from -1 (negative correlation) to +1
(positive correlation).
10. What is the difference between categorical and numerical variables in EDA?
Categorical → Represent categories or labels (e.g., Gender, City).
Numerical → Represent numbers (e.g., Age, Salary).
Program 9: Building a simple predictive model with scikit-learn
Problem Statement:
Build and evaluate a simple predictive model using scikit-learn. The task involves:
Loading and preparing a dataset.
Splitting the dataset into training and testing sets.
Applying a predictive algorithm (e.g., Linear Regression for continuous target or Logistic
Regression/Decision Tree for classification).
Training the model, making predictions, and evaluating its performance using suitable metrics
(e.g., accuracy, mean squared error, R²).
Intuition:
School of Computer Science and Engineering Page 37
Python For Data Science Lab Subcode: B25CS0102
In real-world problems, data is used not only to understand patterns (EDA) but also to make
predictions about unseen cases.
Supervised learning is a core machine learning approach where the model learns from labeled
data.
Example: Predicting a student’s marks (regression) or classifying emails as spam/non-spam
(classification).
scikit-learn provides a simple and consistent API to:
Preprocess data (handle missing values, scaling).
Train models using algorithms like regression, classification, clustering.
Evaluate performance with built-in metrics.
By building a simple predictive model, we learn how raw data can be turned into a decision-making
tool through machine learning.
Code Implementation
(A) Linear Regression
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# Generate sample regression dataset
from [Link] import make_regression
X, y = make_regression(n_samples=100, n_features=1, noise=10, random_state=42)
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build model
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
# Predictions
y_pred = lin_reg.predict(X_test)
School of Computer Science and Engineering Page 38
Python For Data Science Lab Subcode: B25CS0102
# Evaluation
print("Linear Regression Results")
print("Intercept:", lin_reg.intercept_)
print("Coefficient:", lin_reg.coef_)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R² Score:", r2_score(y_test, y_pred))
Sample Output:
Linear Regression Results
Intercept: 0.09922221422587718
Coefficient: [44.24418216]
Mean Squared Error: 104.20222653187027
R² Score: 0.9374151607623286
B) Classification Example (Logistic Regression on Iris) :
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report
# Load dataset
df = sns.load_dataset("iris")
# Features and target
X = [Link]("species", axis=1)
y = df["species"]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build model
log_reg = LogisticRegression(max_iter=200)
log_reg.fit(X_train, y_train)
School of Computer Science and Engineering Page 39
Python For Data Science Lab Subcode: B25CS0102
# Predictions
y_pred = log_reg.predict(X_test)
# Evaluation
print("\nClassification Results")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
Sample Output:
Classification Results
Accuracy: 1.0
Classification Report:
setosa 1.00 1.00 1.00 10
versicolor 1.00 1.00 1.00 9
virginica 1.00 1.00 1.00 11
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
Viva Questions and Answers
[Link] is a predictive model?
A predictive model is an algorithm that learns patterns from historical data to predict outcomes for new, unseen data.
2. What is supervised learning?
Supervised learning is a type of machine learning where the model is trained on labeled data (input features + known outputs).
Regression → Predicts continuous values (e.g., house price).
Classification → Predicts categorical outcomes (e.g., spam or not spam).
3. What is the difference between regression and classification?
Regression → Predicts numerical values (e.g., predicting salary).
Classification → Predicts categories/classes (e.g., predicting whether a patient has a disease).
4. What is the purpose of splitting data into training and testing sets?
Training set → Used to train the model.
Testing set → Used to evaluate the model’s performance on unseen data.
Ensures that the model generalizes well and avoids overfitting.
5. What are common evaluation metrics for regression?
Mean Squared Error (MSE) → Average squared difference between predicted and actual values.
Root Mean Squared Error (RMSE) → Square root of MSE.
R² Score → Proportion of variance explained by the model.
6. What are common evaluation metrics for classification?
Accuracy → Proportion of correct predictions.
School of Computer Science and Engineering Page 40
Python For Data Science Lab Subcode: B25CS0102
Precision → Correct positive predictions / Total predicted positives.
Recall → Correct positive predictions / Total actual positives.
F1 Score → Harmonic mean of precision and recall.
Confusion Matrix → Table showing true positives, true negatives, false positives, and false negatives.
7. What is fit() and predict() in scikit-learn?
fit() → Trains the model on the training data.
predict() → Generates predictions on new data.
8. Why is it important to preprocess data before modeling?
Preprocessing ensures:
No missing values.
Correct data types.
Features are scaled if needed.
Improves model accuracy and reliability.
9. Can you give an example of a simple predictive model in Python?
Linear Regression for predicting house prices.
Logistic Regression for predicting whether a student passes/fails based on marks.
10. Why do we evaluate the model on test data instead of training data?
To measure generalization ability.
Evaluating on training data may overestimate performance due to overfitting.
Program 10: Mini project: Analyzing a real-world dataset end-to-end
Problem Statement
Perform end-to-end analysis on a real-world dataset using Python (Pandas, NumPy, Matplotlib, Seaborn, scikit-
learn). The tasks include:
1. Data loading and inspection
2. Data cleaning and preprocessing
3. Exploratory Data Analysis (EDA)
4. Data visualization
5. Building a predictive model (Regression or Classification)
6. Model evaluation and interpretation
Intuition
Real-world datasets are often messy and unstructured, requiring cleaning and preprocessing.
EDA helps understand patterns, detect missing values, outliers, and relationships among features.
Visualizations make it easier to communicate insights.
Predictive modeling allows us to make decisions or predictions based on historical data.
School of Computer Science and Engineering Page 41
Python For Data Science Lab Subcode: B25CS0102
This project combines all skills learned in previous experiments, providing a holistic understanding of the data
analysis workflow.
Step Involved
1. Data Loading
Load dataset using Pandas.
Inspect the first few rows with [Link]().
Check shape and column types with [Link]().
2. Data Cleaning
Handle missing values using fillna() or dropna().
Remove duplicates with drop_duplicates().
Convert data types using astype().
3. Exploratory Data Analysis (EDA)
Use describe() to get statistical summary.
Check distributions using histograms or boxplots.
Detect correlations using [Link]() and heatmaps.
Identify categorical vs numerical variables for analysis.
4. Data Visualization
Matplotlib → Line plots, bar charts, scatter plots.
Seaborn → Boxplots, violin plots, pairplots, heatmaps.
Visualize relationships between variables and key trends.
5. Predictive Modeling
Split data into training and testing sets.
Choose a model depending on the task:
o Regression → Linear Regression
o Classification → Logistic Regression, Decision Tree, etc.
Train the model using fit().
Make predictions with predict().
6. Model Evaluation
Regression metrics → MSE, RMSE, R² Score
School of Computer Science and Engineering Page 42
Python For Data Science Lab Subcode: B25CS0102
Classification metrics → Accuracy, Precision, Recall, F1-score, Confusion Matrix
Example Dataset
Titanic Dataset (Survival prediction)
Iris Dataset (Species classification)
House Prices (Price prediction)
Expected Outcome
Cleaned and well-structured dataset
Insights from EDA and visualizations
A trained predictive model with accuracy or R² score
Clear interpretation of results for decision-making
Viva Questions
1. What are the main steps in end-to-end data analysis?
2. Why is data cleaning important before modeling?
3. How do you detect and handle missing values?
4. What is the difference between regression and classification?
5. How do visualization and EDA help in modeling?
6. Why do we split data into training and testing sets?
7. Which metrics are used to evaluate model performance?
8. What are the differences between Pandas, NumPy, Matplotlib, and Seaborn?
7. Learning Resources:
Reference Books
"Learning Python" – Mark Lutz
Comprehensive book for beginners and intermediate learners.
Covers Python basics, data structures, control flow, OOP, modules, and advanced features.
"Python Crash Course" – Eric Matthes
School of Computer Science and Engineering Page 43
Python For Data Science Lab Subcode: B25CS0102
Beginner-friendly, project-based approach.
Good for students starting with Python.
"Think Python: How to Think Like a Computer Scientist" – Allen B. Downey
Focuses on problem-solving and logical thinking with Python.
Free online under Creative Commons license.
"Python Programming: An Introduction to Computer Science" – John Zelle
Widely used in academics.
Introduces computer science concepts using Python.
"Fluent Python" – Luciano Ramalho
Advanced book for experienced Python programmers.
Focus on modern features (data models, iterators, generators, concurrency).
"Automate the Boring Stuff with Python" – Al Sweigart
Practical guide to automating daily tasks.
Covers file handling, web scraping, Excel/CSV/PDF automation.
Web-Based Resources in IEEE Format
TensorFlow, “TensorFlow Documentation,” [Online]. Available:
[Link] [Accessed: Feb. 1, 2025].
Keras, “Keras Documentation,” [Online]. Available: [Link] [Accessed: Feb. 1, 2025].
PyTorch, “PyTorch Documentation,” [Online]. Available: [Link]
[Accessed: Feb. 1, 2025].
[Link], “Deep Learning Specialization on Coursera,” [Online]. Available:
[Link] [Accessed: Feb. 1, 2025].
[Link], “[Link] Deep Learning Courses,” [Online]. Available: [Link] [Accessed:
Feb. 1, 2025].
E-books
H. Kinsley, Neural Networks from Scratch in Python, [Online]. Available: [Link]
[Accessed: Feb. 1, 2025].
S. Raschka and V. Mirjalili, Python Machine Learning, 3rd ed., Birmingham, UK: Packt
Publishing, 2019. [Online]. Available: [Link] [Accessed: Feb. 1, 2025].
F. Chollet, Deep Learning with Python and Keras, [Online]. Available:
[Link] [Accessed: Feb. 1, 2025].
J. Howard and S. Gugger, Practical Deep Learning for Coders, [Online]. Available:
[Link] [Accessed: Feb. 1, 2025].
School of Computer Science and Engineering Page 44
Python For Data Science Lab Subcode: B25CS0102
F. Chollet, Deep Learning with Python (Manning), [Online]. Available:
[Link] [Accessed: Feb. 1, 2025].
School of Computer Science and Engineering Page 45