Ad25201 Python For Data Science
Ad25201 Python For Data Science
Unit - I
Answer: A
Answer: D
A 9 B 23 C 6 D 8
Answer: D
Answer: B
Answer: A
A. ** B. * C. // D. Exp
Answer: A
A error B 5 C 7 D 6
Answer: B
A 2.0 B 3 C 2 D 2.5
Answer: C
9. Which keyword is used for conditional branching in Python?
Answer: C
A 1, 2 B [ 0, 1, 2] C {1, 2, 3} D (0, 1, 2)
Answer: B
Answer: A
A {} B [] C <> D( )
Answer: A
Answer: B
Answer: B
A. low level language B. high level language C. Machine language D. Assembly language
Answer: B
Answer: A
numbers = [1, 2, 3, 4, 5]
sliced_numbers = numbers[1:4]
print(sliced_numbers)
Answer: B
44. How do you perform element-wise addition of two NumPy arrays, arr1 and arr2?
45. What does the NumPy function [Link]((arr1, arr2), axis=0) do?
Answer: A
46. What is the output of the code [Link](2, 10, 2)?
47. Given a NumPy array arr = [Link]([[1, 2, 3], [4, 5, 6]]), what does [Link]
return?
48. How can you create a NumPy array filled with zeros of shape (4, 4)?
49. Which method is used to remove rows or columns with missing values?
A. fillna()
B. dropna()
C. remove_null()
D. clean()
Answer: B
50. Which function allows you to pivot a DataFrame into a longer format?
A. pivot_table()
B. melt()
C. stack()
D. unstack()
Answer: B
Note: In addition to this, refer Multiple choice question answers in the back of each unit.
1. What is Python?
Python is a dynamic, high-level, free open source, and interpreted programming language. It
supports object-oriented programming as well as procedural-oriented programming.
A Python Interpreter is the program that reads and executes Python code. It translates the Python
instructions into machine-readable form line by line, so the computer can understand and run
them. When we run a Python program, code passes through Python Interpreter, which is
responsible for:
Checking your code for errors.
Python language is freely available at the official website and you can download it from the
given download link below click on the Download Python keyword. Download Python Since it
is open-source, this means that source code is also available to the public. So you can download
it, use it as well as share it.
2. Easy to code
Python is a high-level programming language. Python is very easy to learn the language as
compared to other languages like C, C#, Javascript, Java, etc. It is very easy to code in the
Python language and anybody can learn Python basics in a few hours or days. It is also a
developer-friendly language.
The if statement contains a logical expression using which data is compared and a
decision is made based on the result of the comparison.
Syntax:
if expression:
statement(s)
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if
statement is executed. If boolean expression evaluates to FALSE, then the first set of
code after the end of the if statement(s) is executed.
list: A mutable, ordered collection that can hold different data types (e.g., [1,
"apple", 3.5]).
tuple: An immutable, ordered collection (e.g., (1, 2, 3)).
range: Represents a sequence of numbers, commonly used for looping.
s = "csiit"
l = len(s)
print(l)
Output: 5
Output:
5
John
List is a collection which is ordered and changeable and allows duplicate members.
(Grow and shrink as needed, sequence type, sortable). To use a list, you must declare it
first. Do this using square brackets and separate values with commas. We can construct /
create list in many ways. Ex:
>>> list1=[1,2,3,'A','B',7,8,[10,11]]
>>> print(list1)
lists and tuples both store collections of data, but differ in mutability, performance and memory
usage. Lists are mutable, allowing modifications, while tuples are immutable.
5 Lists have several built-in methods. Tuples have fewer built-in methods.
13. What is a dictionary?
Python dictionary is a data structure that stores information in key-value pairs. While keys must
be unique and immutable (like strings or numbers), values can be of any data type, whether
mutable or immutable. This makes dictionaries ideal for accessing data by a specific name rather
than a numeric position like in list.
Output:
Set is an unordered collection of unique elements. Unlike lists or tuples, sets do not allow
duplicate values i.e. each element in a set must be unique. Sets are mutable, meaning you can
add or remove items after a set has been created.
Sets are defined using curly braces {} or the built-in set() function. They are particularly
useful for membership testing, removing duplicates from a sequence, and performing
common mathematical set operations like union, intersection, and difference.
A set refers to a collection of distinct objects. It is used to group objects together and to
study their properties and relationships. The objects in a set are called elements or members
of the set.
12 Marks Questions
1. Describe the role of the python interpreter in program execution with an example.
A Python Interpreter is the program that reads and executes Python code. It translates the Python
instructions into machine-readable form line by line, so the computer can understand and run
them. When we run a Python program, code passes through Python Interpreter, which is
responsible for:
Working of Interpreter
1. Python Source Code (.py file): You write your program in a .py file.
2. Parser and AST (Syntax Check -> Abstract Syntax Tree): Python checks the code for syntax
errors and converts it into an AST (Abstract Syntax Tree), which represents the program
structure.
3. Bytecode (.pyc file in __pycache__): The AST is compiled into bytecode, a low-level instruction
set. This bytecode may also be saved in the __pycache__ folder for reuse.
4. Python Virtual Machine (Executes Bytecode): The PVM executes the bytecode line by line and
translates it into machine instructions.
5. Output (Result on Screen): Finally, the program’s result is shown on the screen (for example, via
print()).
To visualize this at a high level, here’s a simple diagram of how an interpreter processes your
code:
Working of Interpreter
Example
This program takes two inputs as a and b and prints sum in the third variable which is c. It
follows sequential as well as functional execution of programs
a=3
b=7
c=a+b
print(c)
Output:
10
2. Discuss python data types in details with example for each type.
Data types:
The data stored in memory can be of many types. For example, a student
roll number is stored as a numeric value and his or her address is stored as
alphanumeric characters. Python has various standard data types that are
used to define the operations possible on them and the storage method for
each of them.
Integer
Int, or integer, is a whole number, positive or negative, without
decimals, of unlimited length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20
>>> type(10)
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>
Float:
>>> y=2.8
>>> y
2.8
Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
String:
mrcet college
<class 'str'>
Input()
Python's input() function is used to take user input. By default, it returns the user input in form of
a string.
Output
The code prompts the user to input their name, stores it in the variable "name" and then prints a
message addressing the user by their entered name.
Print()
The print() function allows us to display text, variables and expressions on the console. In the
below example, "Hello, World!" is a string literal enclosed within double quotes. When executed,
this statement will output the text to the console.
print("Hello, World!")
Output
Hello, World!
Len()
When the object is a string, the len() function returns the number of characters in the string.
mylist = ["apple", "banana", "cherry"]
x = len(mylist)
Output: 3
Range( )
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and stops before a specified number.
x = range(6)
for n in x:
print(n)
Output:
0
1
2
3
4
5
Python operators are special symbols and keywords used to perform computations on variables
and values. They are categorized into several functional groups:
1. Arithmetic Operators
Equal (==): x == y.
Not Equal (!=): x != y.
Greater than (>), Less than (<): Standard numeric or lexicographic comparisons.
Greater than or equal to (>=), Less than or equal to (<=): Inclusive comparisons.
3. Logical Operators
It is Used to combine conditional statements:
4. Assignment Operators
It is used to assign values to variables, often combined with arithmetic for shorthand:
Simple (=): x = 5.
Compound: Performs operation then assigns result (e.g., +=, -=, *=, /=, //=, %=, **=).
Walrus (:=): Assigns a value to a variable within an expression.
5. Bitwise Operators
It is used to operate on the binary representations of integers:
AND (&), OR (|), XOR (^), NOT (~): Standard bit-level logic.
Shift (<<, >>): Shifts bits left or right.
6. Special Operators
Identity (is, is not): Checks if two variables refer to the exact same object in memory.
Membership (in, not in): Tests if a value is present in a sequence like a list, string, or
dictionary.
5. Write a python program to display all even numbers between 1 and 100 using a loops.
6. Describe flow control statements in python with example for if, elif, else and looping constructs.
Conditional Statements
Conditional statements in Python allow us to check for certain conditions and perform actions
based on the outcome of those checks. There are several types of conditional statements in
Python, including:
if statement
if else statement
if elif else statement
nested if else statement
1. if statement
The if statement is used to check if a certain condition is true, and if so, execute a specific block
of code. Here’s an example:
age = 18
In this example, the if statement checks if the value of the variable age is greater than or equal to
18. If it is, the code inside the if statement is executed, which in this case is simply printing a
message to the console.
2. if else statement
The if else statement is used to execute one block of code if a condition is true, and another block
of code if the condition is false. Here’s an example:
age = 16
In this example, the if statement checks if the value of age is greater than or equal to 18. If it is,
the message "You are old enough to vote." is printed. If it is not, the message "You are not old
enough to vote yet." is printed instead.
The if elif else statement is used to check multiple conditions, and execute a specific block of
code based on which condition is true. Here’s an example:
age = 16
In this example, the first if statement checks if the value of age is greater than or equal to 18. If it
is, the message "You are old enough to vote." is printed. If not, the elif statement checks if age is
greater than or equal to 16. If it is, the message "You can drive but cannot vote." is printed. If
neither of these conditions are true, the else statement executes, and the message "You cannot
drive or vote yet." is printed.
The nested if else statement is used when we need to check a condition inside another condition.
Here’s an example:
age = 18
gender = "female"
In this example, the first if statement checks if age is greater than or equal to 18. If it is, the
nested if statement checks if the value of gender is "male". If it is, the message "You are a male
and old enough to vote." is printed. If not, the message "You are a female and old enough.
Loops in Python
A while loop statement in Python is used to repeatedly execute a block of code as long as a
condition is true. It is typically used when you don’t know how many times the loop will run in
advance.
A for-loop statement in Python is used to iterate over a sequence (such as a list, tuple, or string)
and perform a certain action for each item in the sequence.
UNIT – II
Arguments are the values passed inside the parenthesis of the function. A function can have
any number of arguments separated by a comma.
Syntax
def function_name(parameters):
# body of the function
return expression
The return statement ends a function and sends a value back to the caller. It can return any
data type, multiple values, or None if no value is given.
Syntax:
return [expression]
Parameters: return ends the function, [expression] is the optional value to return
A class is a user-defined template for creating objects. It bundles data and functions
together, making it easier to manage and use them. When we create a new class, we
define a new type of object. We can then create multiple instances of this object type.
Creating Class
Classes are created using class keyword. Attributes are variables defined inside class and
represent properties of the class. Attributes can be accessed using dot . operator (e.g.,
MyClass.my_attribute).
# define a class
class Dog:
sound = "bark" # class attribute
5. Define object.
An object is a specific instance of a class. It holds its own set of data (instance variables)
and can invoke methods defined by its class. Multiple objects can be created from same
class, each with its own unique attributes.
class Dog:
sound = "bark"
Output
Bark
The "w" mode in file opening is used for The "a" mode in file opening is used for appending data to
writing data to a file. a file.
If the file exists, Python will truncate If the file exists, the data in the file is retained and new
existing data and over-write in the file. data being written will be appended to the end of the file.
import math
# square root of 4
print([Link](4))
Output
2.0
File handling refers to the process of performing operations on a file, such as creating,
opening, reading, writing and closing it through a programming interface. It involves
managing the data flow between the program and the file system on the storage device,
ensuring that data is handled safely and efficiently.
Opening a File
To open a file, we can use open() function, which requires file-path and mode as
arguments.
Syntax:
file = open('[Link]', 'mode')
[Link]: name (or path) of the file to be opened.
mode: mode in which you want to open the file (read, write, append, etc.).
Note: If you don’t specify the mode, Python uses 'r' (read mode) by default.
Basic Example: Opening a File
f = open("[Link]", "r")
print(f)
Closing a File
[Link]() method closes the file and releases the system resources. If the file was
opened in write or append mode, closing ensures that all changes are properly saved.
f = open("[Link]", "r")
print("Filename:", [Link])
print("Mode:", [Link])
print("Is Closed?", [Link])
[Link]()
print("Is Closed?", [Link])
Output
Filename: [Link]
Mode: r
Is Closed? False
Is Closed? True
Reading a File
Reading a file can be achieved by [Link]() which reads the entire content of the file.
After reading, it’s good practice to close the file to free up system resources.
Example: Reading a File in Read Mode (r)
Output
Hello, Python!
File handling is easy with Python.
Instead of manually opening and closing the file, you can use the with statement, which
automatically handles closing.
with open("[Link]", "r") as file:
content = [Link]()
print(content)
Output
Hello, World!
n = 10
try:
res = n / 0
except ZeroDivisionError:
print("Can't be divided by zero!")
Output
The import statement in Python is the tool used to bring code from one module (a file containing
Python code) into another. This allows to reuse functions, classes, and variables without rewriting
them. The most common way to import is using the import keyword followed by the module name.
Access its contents using dot notation.
import math
Python string methods is a collection of in-built Python functions that operates on strings.
Output
Converted String:
GEEKS FOR GEEKS
Converted String:
geeks for geeks
Converted String:
Geeks For Geeks
Converted String:
GEEkS fOR GEeKs
Original String
geeKs For geEkS
12 Marks Questions
# a user-defined function
def my_function():
print('Welcome!')
#Function creation
def my_function():
print('Welcome!')
#Function call
my_function()
The output:
This explains the phrase ‘user-defined functions only run when they
are called’ in its definition above. Calling a function is simply telling
python to execute the command in the “def code block”.
Return Values
The “return” function can be used to return and print the result
computation that is carried out when a function is called instead of the
“print” statement.
For example:
def calculateAverage(param1, param2, param3):
# Add up the numbers and divide by the count of numbers
total = param1 + param2
average = total / 2.0
return average # returns the answer to the caller
When we pass a list to a function by reference, it refers to the original list. If we make any
modifications to the list, the changes will reflect in the original list.
Example:
def fun(l):
for i in l:
print(i,end=" ") # Iterates
l = [1, 2, 3, 4]
fun(l)
Output
1 2 3 4
Explanation:
This allows us to pass a list to a function and unpack it into separate arguments. It is useful when
we don't know the exact number of arguments the function will receive.
Example:
def fun(*args):
for i in args:
print(i,end=" ")
l = [1, 2, 3, 4, 5]
fun(*l)
Output
1 2 3 4 5
Explanation:
Passing a copy
Shallow copy of a list creates a new list with the same elements, ensuring the original list
remains unchanged. This is useful when we want to work with a duplicate without modifying the
original.
Example:
def fun(l):
[Link](6)
a = [1, 2, 3, 4, 5]
b = [Link]() # shallow copy of list
fun(b)
print(a) # Original list
print(b) # Modified copy
Output
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
Explanation:
Copied list is modified by appending 6, while the original list remains the same.
Recursion is a programming technique where a function calls itself either directly or indirectly to
solve a problem by breaking it into smaller, simpler subproblems. A recursive function is just like
any other function except that it calls itself in its body. Let's see basic structure of recursive function:
def recursive_function(parameters):
if base_case_condition:
return base_result
else:
return recursive_function(modified_parameters)
Recursive function contains two key parts:
Base Case: The stopping condition that prevents infinite recursion.
Recursive Case: The part of the function where it calls itself with modified parameters.
Example 1: This code defines a recursive function to calculate factorial of a number, where function
repeatedly calls itself with smaller values until it reaches the base case.
def factorial(n):
if n == 0: # Base case
return 1
else: # Recursive case
return n * factorial(n - 1)
print(factorial(5))
Output
120
Explanation:
Base Case: When n == 0, recursion stops and returns 1.
Recursive Case: Multiplies n with the factorial of n-1 until it reaches the base case.
4. Explain try, except , else and finally blocks with suitable example
An Exception is an Unexpected Event, which occurs during the execution of the program. It
is also known as a run time error. When that error occurs, Python generates an exception
during the execution and that can be handled, which prevents your program from
interrupting.
In this code, The system can not divide the number with zero so an exception is raised.
a = 5
b = 0
print(a/b)
Output
Traceback (most recent call last):
File "/home/[Link]", line 3, in
<module>
print(a/b)
ZeroDivisionError: division by zero
Let us try to take user integer input and throw the exception in except block.
Output:
Yeah ! Your answer is : 1
Sorry ! You are dividing by zero
5. Differentiate between text files and binary files. Write a program to copy contents from one file
to another.
A text file consists of human readable A binary file is made up of non-human readable
characters, which can be opened by any text characters and symbols, which require specific
editor. programs to access its contents.
Files with extensions like .txt, .py, .csv etc Files with extensions like .jpg, .pdf etc are some
are some examples of text files. examples of binary files.
6. Write a python program to create a class ‘student’ with data members name, roll number and
marks. Include methods to display details.
class Student:
# Constructor to initialize data members
def __init__(self, name, roll_number, marks):
[Link] = name
self.roll_number = roll_number
[Link] = marks # Method to display student details
def display_details(self):
print("\n--- Student Information ---")
print(f"Name: {[Link]}")
print(f"Roll Number: {self.roll_number}") print(f"Marks: {[Link]}")
# Creating an instance (object) of the Student class
student1 = Student("Alex Smith", "A101", 85)
# Calling the method to display details
student1.display_details()
__init__ method: This is the standard constructor in Python used to assign values to the object's
properties when it is first created.
self parameter: This is a reference to the current instance of the class and is used to access variables
that belong to the class.
Data Members: name, roll_number, and marks are stored as instance variables.
Methods: The display_details function is defined within the class to print the stored data in a
readable format.
7. Write a program to count vowels , consonants, digits and write data into a text file.
def vowel():
f=open(“[Link]”,”r”)
vowels=”aeiouAEIOU”
count=0
for line in f:
for char in line:
if char in vowels:
count=count+1
print(“the number of vowels in the file is”,count)
[Link]()
vowel()
UNIT – III
NumPy is a homogeneous data structure (all elements are of the same type). It is significantly
faster than Python's built-in lists because it uses optimized C language style storage where
actual values are stored at contiguous locations (not object reference). It also supports
vectorized computations. It supports vectorized operations (no need for loops).
NumPy array’s objects allow us to work with arrays in Python. The array object is called ndarray.
NumPy arrays are created using the array() function.
import numpy as np
# Creating a 1D array
x = [Link]([1, 2, 3])
# Creating a 2D array
y = [Link]([[1, 2], [3, 4]])
# Creating a 3D array
z = [Link]([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(x)
print(y)
print(z)
Output
[1 2 3]
[[1 2]
[3 4]]
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Joining NumPy arrays means combining multiple arrays into one larger array. For
example, joining two arrays [1, 2] and [3, 4] results in a combined array [1, 2, 3, 4].
Let’s explore some common ways to join arrays using NumPy.
1. Using [Link]()
[Link]() joins two or more arrays along an existing axis without adding new
dimensions. It is fast and efficient for straightforward array joining.
import numpy as np
a = [Link]([1, 2])
b = [Link]([3, 4])
res = [Link]((a, b))
print(res)
Output
[1 2 3 4]
This code combines them into one longer list [1, 2, 3, 4] using NumPy’s concatenate
function which just sticks the arrays together end to end.
import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
res_d= [Link]((a,b))
print("[Link]: ", res_d)
Output
[Link]: [1 2 3 4 5 6]
[Link]: [[1 2 3]
[4 5 6]]
[Link]: [[[1 4]
[2 5]
[3 6]]]
Splitting arrays means dividing a single NumPy array into multiple smaller sub-arrays.
NumPy provides several functions that make this easy by allowing you to split arrays along
different directions (rows, columns, depth).
Below are some important terms to understand when splitting arrays:
Axis: The direction along which the array is split (0 for rows, 1 for columns).
Sub-arrays: Smaller arrays created after splitting the original array.
Splitting Methods: Functions like [Link](), [Link](), [Link]() and np.array_split().
Equal vs. Unequal Splits: Splits can divide data evenly, or slightly unevenly if needed
(using array_split()).
Example: This example splits a 1D array into three smaller parts using np.array_split().
import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6])
res = np.array_split(arr, 3)
print(res)
Output
Splitting Methods
NumPy provides several built-in functions to split arrays into smaller parts. These methods
help divide 1D, 2D, and even 3D arrays along different axes. Let's go through each method
one by one with simple examples, outputs, and clear explanations.
1. [Link]()
[Link]() is used to divide an array into equal-sized subarrays. The number of splits
must perfectly divide the size of the array along the chosen axis. If equal division is not
possible, NumPy will raise an error.
import numpy as np
arr = [Link](6)
res = [Link](arr, 2)
print(res)
Output
Slicing is a method for taking out an array section frequently used for subsetting and
modifying data inside arrays. In Python, Slicing gains considerably more strength when
used with multi-dimensional arrays because it may be applied along several axes.
In a 1-D NumPy array, slicing is performed using the [start:stop: step] notation.
import numpy as np
5. What is pandas?
Pandas is an open-source Python library used for data manipulation, analysis and
cleaning. It provides fast and flexible tools to work with tabular data, similar to
spreadsheets or SQL tables.
After the Pandas have been installed in the system we need to import the library. This
module is imported using:
import pandas as pd
6. Define series.
A Pandas Series is one-dimensional labeled array capable of holding data of any type
(integer, string, float, Python objects etc.). The axis labels are collectively called indexes.
Series is created by loading the datasets from existing storage which can be a SQL
database, a CSV file or an Excel file.
import pandas as pd
import numpy as np
s = [Link]()
print("Pandas Series: ", s)
data = [Link](['g', 'e', 'e', 'k', 's'])
s = [Link](data)
print("Pandas Series:\n", s)
Output
7. Define dataframe.
Indexing in pandas refers to the methods used to select specific rows and columns from a Pandas Series
or DataFrame. It serves as an address system for your data, allowing for efficient retrieval, alignment, and
manipulation.
.loc[] (Label-based Indexing): Used to select data by the labels of rows and columns.
o Inclusivity: Unlike standard Python slicing, the endpoint in .loc is included (e.g., [Link]['a':'c']
includes 'c').
o Usage: [Link][row_label, column_label].
.iloc[] (Integer-based Indexing): Used to select data by its numerical position (0-based).
o Inclusivity: Follows standard Python/NumPy conventions where the endpoint is excluded (e.g.,
[Link][0:2] gets positions 0 and 1).
o Usage: [Link][row_position, column_position].
[] (Square Bracket Operator): The most basic method, often used for quick column selection or row
slicing.
o Single/Multiple Columns: df['column_name'] or df[['col1', 'col2']].
o Row Slicing: df[0:5] selects the first five rows.
9. What is reindexing?
Reindexing in Pandas is used to change the row or column labels of a DataFrame to
match a new set of indices. This is useful when aligning data, adding missing labels, or
reshaping your DataFrame. If the new index includes values not present in the original
DataFrame, Pandas fills those with NaN by default. For example, if we try adding a new
row using reindex():
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = [Link](data)
a = [Link]([0, 1, 2, 3])
print(a)
Output
A B
0 1.0 4.0
1 2.0 5.0
2 3.0 6.0
3 NaN NaN
Index 3 wasn’t present in the original DataFrame, so it's filled with NaN.
Data alignment is a core feature of pandas that ensures operations between different DataFrames or
Series happen between matching labels, rather than just matching positions.
There are two ways "alignment" is usually discussed in pandas: Automatic Alignment (how pandas
behaves during math/logical operations) and the .align() method (explicitly syncing two objects). [1, 2]
It returns a tuple of two new objects, both reshaped to match based on the join type:
import pandas as pd
Data visualization uses charts, graphs and maps to present information clearly and simply. It turns
complex data into visuals that are easy to understand. With large amounts of data in every industry,
visualization helps spot patterns and trends quickly, leading to faster and smarter decisions.
Let's create a simple line plot using Matplotlib, showcasing the ease with which you can
visualize data.
import [Link] as plt
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 9, 16]
[Link](x, y)
[Link]()
Output
13. What is the purpose of [Link]() command?
The main purpose of [Link]() in Python's Matplotlib library is to display all currently active figures
in a visual window. [Link]() is the command that tells the backend to open a GUI window and make
the visualization visible.
Rendering: It triggers the rendering process for all figures created up to that point in your script.
In Matplotlib, Figures and Axes are the fundamental building blocks of any visualization. Think of the
Figure as the entire window or canvas, and the Axes as the actual "plot" (the area where data is drawn).
2️⃣ Axe: a container that sits within the figure and it’s a canvas for
your actual plots.
3️⃣ Plot : the shapes that make up your visualisation. The bars, points
or lines, the legends and annotations.
# Plot 1:
x1 = [Link]([1, 2, 3, 4])
y1 = [Link]([10, 20, 25, 30])
[Link](1, 2, 1)
[Link](x1, y1)
# Plot 2:
x2 = [Link]([1, 2, 3, 4])
y2 = [Link]([30, 25, 20, 10])
[Link](1, 2, 2)
[Link](x2, y2)
[Link]()
Output:
A legend is an area describing the elements of the graph. In the Matplotlib library, there’s a
function called legend() which is used to place a legend on the axes. In this article, we will
learn about the Matplotlib Legends. In this example, a simple quadratic function \( y = x^2 \)
is plotted against the x-values [1, 2, 3, 4, 5]. A legend labeled "single element" is added to
the plot, clarifying the plotted data.
import numpy as np
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
[Link](x, y)
[Link](['single element'])
[Link]()
Output :
17. Which function is used to annotate specific points in a plot.
Annotate any point in the chart with text using the annotate() function. The function
parameters used in the example below are:
12 Marks Questions
1. Explain the creation of arrays in Numpy with examples for different methods such as array(),
arange(), linspace(), and zeros().
In Python, array, arange, zeros, and linspace are core functions used primarily within the NumPy
library to create and manage numerical data structures.
[Link]()
This function converts existing Python data structures, like lists or tuples, into a NumPy ndarray.
Key Feature: Creates a clone of the original object to ensure the data is stored in a contiguous block of
memory for fast mathematical operations.
[Link]()
Key Feature: Highly efficient for pre-allocating memory when you know the size of the array but haven't
calculated the values yet.
[Link]()
Creates an array with evenly spaced values within a given range, similar to Python's built-in range().
Key Feature: You define the step size (e.g., "give me every 2nd number"). The stop value is exclusive
by default.
Generates a specific number of evenly spaced values between a start and end point. [1, 2]
2. Describe how legnds and annotations enhance the readability of a data visualization.
Annotations are used to add notes or more information about a topic. Annotations
can be titles, legends, Arrows, bands, labels etc. Adding legends to your figures
can help to properly describe and define it. Hence, giving more clarity. Legends in
Bokeh are simple to implement. They can be basic, automatically grouped,
manually mentioned, explicitly indexed and also interactive.
Example: Basic legends
The legend_label parameter is used to add a basic label to any one of the glyph.
from [Link] import figure, output_file, show
x = [val for val in range(10)]
y = [val for val in range(0, 20, 2)]
output_file("[Link]" )
p = figure()
[Link](x, y, legend_label="My Red Line", line_color="red")
[Link](y, x, legend_label="My Orange Line",
line_color="orange")
[Link](y[::-1], x, legend_label="My Green Line",
line_color="green")
show(p)
Output:
Example 2: Automatic Grouping can be used when we want to group multiple legend
items to be grouped into one.
source = ColumnDataSource(dict(
x=[1, 1, 2, 2, 1.5],
y=[1, 2, 1, 2, 1.5],
color=['red', 'red', 'red', 'red', 'blue'],
label=['corner', 'corner', 'corner', 'corner', 'center']
))
[Link](x='x', y='y', radius=0.05, color='color',
legend_group='label', source=source)
output_file("[Link]" )
show(p)
Output:
Output:
3. Illustrate the overview of data science process.
5. Data Cleaning: Based on the insights from EDA, clean the data by
addressing outliers, inconsistencies, and missing values.
6. Modeling: Use the cleaned and understood data to build and train
machine learning models.
Data mining is the extraction of useful information from large data sets, using machine learning and other
tools to discover patterns, anomalies, and insights for decision-making.
Data mining is the process of discovering useful information from an accumulation of data, often from
a data warehouse or a collection of linked data sets. Data mining can involve machine learning, statistical
analysis, and other powerful analytical tools used to sift through large sets of data to identify trends,
hidden patterns, anomalies, and relationships to support informed decision-making and planning.
The 5-step process of data mining
1. Data collection:
2. Data preprocessing:
Review the data sources you’ve gathered and make sure that you have the rights to access and
use the external data, including demographics, economic data, and market intelligence, such as
industry trends and financial benchmarks from trade associations and governments; data privacy
regulations can vary significantly depending on the region and are subject to change, so this is a
crucial step.
Engage subject matter experts to help define, categorize, and organize the data—this part of the
process is sometimes called data wrangling or data munging.
Clean the collected data, removing duplication, inconsistencies, incomplete records, or outdated
formats.
3. Model building:
Select relevant algorithms and techniques (such as decision trees, regression, or clustering—
more about data mining techniques below).
Train multiple models on your preprocessed data or fine-tune their parameters to optimize
performance.
Test model accuracy using validation techniques to ensure reliable performance on new data.
Compare different modeling approaches and identify the best option for your specific goals.
4. Evaluation:
Assess model reliability across key metrics such as accuracy, precision, and error rates.
Identify potential issues such as bias, overfitting, or data quality concerns.
5. Interpretation:
Identify which data factors have the greatest effect on predictions and outcomes—this will help
you explain key findings to the stakeholders.
Depending on team structure, you may need to translate model findings into insights and provide
reports or visualizations that would make results clear to non-technical decision-makers and other
stakeholders across the organization.
Formulate specific, actionable recommendations for business strategy, operations, and
processes based on the discovered patterns.
Select relevant metrics and establish a plan to measure the effect of implementing
recommendations derived from data mining.
Clustering
Clustering is a data mining technique aimed at discovering natural groupings based on similarities in
data rather than pre-defined assumptions (as opposed to classification), ultimately revealing hidden
patterns and relationships. In the credit card example, clustering could uncover additional flags for
suspicious activity. For instance, historic data from accounts that have suffered from fraudsters might
reveal that a statistically significant proportion of them share another similarity: perhaps, they’ve all shown
a pattern of small test purchases from a particular merchant, followed by large transactions. Then, in the
future, this pattern could be used to detect fraudulent activity in real time.
Association rules
Another key data mining technique is association rule mining: linking two seemingly unrelated events or
activities. Imagine that you’re trying to optimize product placement in a supermarket to maximize sales. It
doesn’t take data mining to speculate that, say, customers who buy diapers are also likely to buy other
baby products, such as baby wipes. But this data mining technique might discover other, less obvious,
cross-selling opportunities: perhaps, you’ll notice that customers who stock up on disposable cutlery in
the summer are also more likely to buy insect repellent and marshmallows. These products would
normally be in different product isles, but data mining might point to a seasonal shopping mission: getting
supplies for spending time outdoors. In this scenario, the association rule data mining technique would
help the retailer exploit this seasonal opportunity.
Regression
One of the mathematical data mining techniques, regression analysis predicts a number based on historic
patterns. It’s a classic tool used in many fields and contexts, including sales forecasting, stock price
predictions, and financial analysis.
5. Explain the different measures of central tendency and describe the suitable measures for the
different type of data distribution.
Central tendencies in statistics are numerical values that represent the middle or typical
value of a dataset. Also known as averages, they provide a summary of the entire data,
making it easier to understand the overall pattern or behavior. These values are useful
because they capture the essence of large datasets in a single, representative number.
Central Tendency
The three most commonly used measures of central tendency are mean, median,
and mode.
Mean
Mean in general terms is used for the arithmetic mean of the data, but other than the
arithmetic mean there are geometric mean and harmonic mean as well that are calculated
using different formulas.
The Arithmetic Mean is the most common type of average. It is obtained by adding all the
observations and then dividing by the total number of observations. It gives a simple
average value representing the entire data set.
The formula for the Arithmetic Mean is given by
xˉ=∑xiNxˉ=N∑xi
Where,
x1, x2, x3, . . ., xn are the observations, and
N is the number of observations.
Mode=3
subplots() function simplifies the creation of multiple plots within a single figure for
organized visualization of various datasets. Before diving into subplots, let's start with a
simple plot using [Link]():
import [Link] as plt
What is [Link]()
The subplots() function in [Link] creates a figure with a set of subplots
arranged in a grid. It allows you to easily plot multiple graphs in a single figure, making
your visualizations more organized and efficient.
Syntax
[Link](nrows=1, ncols=1)
This syntax creates a figure with nrows rows and ncols columns of subplots.
# Plot 1:
x1 = [Link]([1, 2, 3, 4])
y1 = [Link]([10, 20, 25, 30])
[Link](1, 2, 1)
[Link](x1, y1)
# Plot 2:
x2 = [Link]([1, 2, 3, 4])
y2 = [Link]([30, 25, 20, 10])
[Link](1, 2, 2)
[Link](x2, y2)
[Link]()
Output:
Two side-by-side plots displaying different datasets.
The subplots() function in Matplotlib allows plotting multiple plots using the same data
or axes. For example, setting nrows=1 and ncols=2 creates two subplots that share the
y-axis.
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])
fig, ax = [Link](1, 2)
ax[0].plot(x, y)
ax[0].set_title('Plot 1')
ax[1].plot(x, y, 'r')
ax[1].set_title('Plot 2')
[Link]()
Output:
Two side-by-side plots displaying different datasets.
You can stack subplots vertically and horizontally by adjusting the nrows and ncols
parameters in subplots(). This example demonstrates a 2x2 grid layout.
# Implementation of matplotlib function
import numpy as np
import [Link] as plt
Output:
A 2x2 grid of plots, each displaying different mathematical functions.
In some cases, you may want your subplots to share axes. This is useful when
comparing datasets with similar ranges. By setting the sharex or sharey parameter
to True, the subplots will share their x or y axis.
Example Code for Shared Axis:
import numpy as np
import [Link] as plt
Data Collection: Gathering raw data from various sources, such as databases, sensors
or user interactions.
Data Cleaning: Ensuring the data is accurate, complete and ready for analysis.
Data Analysis: Applying statistical and computational methods to identify patterns,
trends or relationships.
Data Visualization: Creating charts, graphs and dashboards to present findings clearly.
Decision-Making: Using insights to inform strategies, create solutions or predict
outcomes.
Handles Massive Data: Machine learning works well with large data and finds patterns
that humans might miss.
Adapts Dynamically: Systems evolve with new data, staying relevant in changing
environments.
Drives Smarter Decisions: From predicting customer behavior to detecting fraud, ML
enhances decision-making with data-driven insights.
Personalizes Experiences: Recommendation systems, like those on Netflix or Amazon,
tailor suggestions to individual preferences.
Data Mining is the process of discovering meaningful patterns and insights from large
datasets using statistical, machine learning and computational techniques. It helps
organizations analyze historical data and make data-driven decisions.
Extracts hidden patterns and relationships from large datasets
Uses techniques such as classification, clustering and regression
Widely used in marketing, finance, healthcare and business analytics
Data cleaning is the process of preparing raw data by detecting and correcting errors so it
can be effectively used for analysis. It is a foundational step in data preprocessing that
ensures datasets are suitable for analytical, statistical and machine learning tasks.
Raw data is often noisy, incomplete and inconsistent which can negatively impact the
accuracy of the model.
Clean datasets are also important in EDA (Exploratory Data Analysis), which enhances
the interpretability of data so that the right actions can be taken based on insights.
Data can be categorised in different ways depending on how it is collected, stored and
represented.
1. Quantitative Data
Quantitative data is information that can be measured, counted and expressed in numerical
form. It provides objective values that can be analyzed statistically to identify patterns,
trends and relationships.
Represents numbers and measurable values.
Can be divided into: Discrete data (Whole numbers) and Continuous data (Values on a
scale).
Widely used in research, finance, engineering and business analytics.
Example: Age of people, number of customers visiting a store, temperature readings, sales
revenue.
2. Qualitative Data
3. Structured Data
Structured data is information organized into a predefined format (rows and columns) that
makes it easily searchable and manageable by traditional databases.
Stored in relational databases or spreadsheets.
Easy to process with SQL and other tools.
Best suited for tasks requiring accuracy and consistency.
Example: Bank transactions, employee records, product inventories.
4. Unstructured Data
Unstructured data is raw information that does not follow a predefined structure or format
making it harder to organize and analyze with conventional tools.
Accounts for over 80% of data generated globally.
Requires advanced tools (AI, NLP, computer vision) to extract insights.
Common in social media, multimedia and IoT applications.
Example: Emails, images, videos, voice recordings, PDF documents.
5. Semi-Structured Data
Semi-structured data combines aspects of structured and unstructured data. It does not
reside in traditional tables but still contains tags or markers that provide a loose structure.
Provides a balance between flexibility and structure.
Easier to analyze than unstructured data, but less rigid than structured data.
Often used in web applications, IoT devices and log systems.
Example: JSON files, XML documents, NoSQL databases, sensor logs.
Data is the raw form of information, a collection of facts, figures, symbols or observations
that represent details about events, objects or phenomena. By itself, data may appear
meaningless, but when organized, processed and interpreted, it transforms into valuable
insights that support decision-making, problem-solving and innovation.
Data refers to raw facts, figures, or information that can be processed and analysed to
extract meaningful insights.
In data science and computing, data is categorised into different types based on its
structure and nature.
Understanding its type helps in selecting appropriate analysis and processing methods.
Discrete variable is a type of variable that can only take on specific or distinct values. These
values are typically whole numbers or integers. Discrete variables often represent counts or
categories.
Example of discrete variables are:
Number of students in a classroom: It is a discrete variable because it can only take
on whole number values (e.g., 25 students, 30 students).
Continuous variable is a type of variable that can take on any value within a given range.
Unlike discrete variables, which consist of distinct, separate values, continuous variables
can represent an infinite number of possible values, including fractional and decimal values.
Continuous variables often represent measurements or quantities.
Example of continuous variables are:
Height: Height is a continuous variable because it can take on any value within a range
(e.g., 150.5 cm, 162.3 cm, 175.9 cm).
A frequency distribution is a method for organizing data and determining how often each
value occurs.
It shows how many times each value or range of values occurs in a dataset.
Instead of examining raw, scattered numbers, this approach presents data in a
structured table or graph, making patterns, trends, and comparisons easy to identify.
Outliers are data points that differ significantly from the rest of the dataset and do not
follow the general pattern. They can occur due to errors, rare events or natural variability
in data.
Outliers can occur due to a variety of reasons. Identifying their source is crucial for accurate
data analysis
Data Entry Errors: Mistakes made while entering data manually can generate extreme
or inconsistent values.
Measurement Errors: Faulty instruments or incorrect experimental setups can lead to
abnormally high or low readings.
Experimental Errors: Poorly designed experiments may produce results that do not
accurately represent the underlying phenomenon.
Intentional Outliers: Sometimes outliers are introduced deliberately such as in cases of
fraud or data manipulation.
Data Processing Errors: Errors during data collection, cleaning or transformation can
introduce unusual values.
Natural Variation: Some outliers arise naturally due to inherent variability in the
population or process being studied.
Project Charter refers to a statement of objectives in a project. This statement also sets out
detailed project goals, roles and responsibilities, identifies the main stakeholders, and the level of
authority of a project manager.
It acts as a guideline for future projects as well as an important material in the organization's
knowledge management system.
The project charter is a short document that would consist of new offering request or a request
for proposal. This document is a part of the project management process, which is required by
Initiative for Policy Dialogue (IPD) and Customer Relationship Management (CRM).
Data visualization
1. What is a scatter plot? For what type of data is scatter plot usually
used for?
A scatter plot is a chart used to plot a correlation between two or more variables at the same
time. It’s usually used for numeric data.
[Link] is Matplotlib?
Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its
numerical extension NumPy. Matplotlib is a comprehensive library for creating static, animated and
interactive visualizations in Python. Matplotlib is a plotting library for the Python programming language.
It allows to make quality charts in few lines of code. Most of the other python plotting library are build on
top of Matplotlib.
[Link] is legend?
Plot legends give meaning to a visualization, assigning labels to the various plot
elements. Legends are found in maps describe the pictorial language or symbology
of the map. Legends are used in line graphs to explain the function or the values
underlying the different lines of the graph.
A tick is a short line on an axis. For category axes, ticks separate each category.
For value axes, ticks mark the major divisions and show the exact point on an axis
that the axis label defines. Ticks are always the same color and line style as the
axis.
• Ticks are the markers denoting data points on axes. Matplotlib's default tick
locators and formatters are designed to be generally sufficient in many common
situations. Position and labels of ticks can be explicitly mentioned to suit specific
requirements.
5. What is Seaborn?
• Seaborn helps you explore and understand your data. Its plotting functions
operate on dataframes and arrays containing whole datasets and internally perform
the necessary semantic mapping and statistical aggregation to produce informative
plots.
• Its dataset-oriented, declarative API. User should focus on what the different
elements of your plots mean, rather than on the details of how to draw them.
Line chart is one of the basic plots and can be created using plot() function. It is used to represent
a relationship between two data X and Y on a different axis.
Syntax:
[Link](x, y)
[Link](x, y)
[Link]("Line Chart")
[Link]('Y-Axis')
[Link]('X-Axis')
[Link]()
Output
2. Bar Chart
Bar chart displays categorical data using rectangular bars whose lengths are proportional to the
values they represent. It can be plotted vertically or horizontally to compare different categories.
Syntax:
[Link](x, height)
Parameter:
x: Categories or positions on x-axis.
height: Heights of the bars (y-axis values).
Example: This code creates a simple bar chart to show total bills for different days. X-axis
represents the days and Y-axis shows total bill amount.
import [Link] as plt
[Link](x, y)
[Link]("Bar Chart")
[Link]("Day")
[Link]("Total Bill")
[Link]()
Output
Bar Chart
Scatter plots are used to observe relationships between variables. The scatter() method in the
matplotlib library is used to draw a scatter plot.
Syntax:
[Link](x, y)
Scatter Plot
5. Pie Chart
Pie chart is a circular chart used to show data as proportions or percentages. It is created using the
pie(), where each slice (wedge) represents a part of the whole.
Syntax:
[Link](x, labels=None, autopct=None)
Parameter:
x: Data values for pie slices.
labels: Names for each slice.
autopct: Format to display percentage (e.g., '%1.1f%%').
Example: This code creates a simple pie chart to visualize distribution of different car brands. Each
slice of pie represents the proportion of cars for each brand in the dataset.
Pie Chart
6. Explain histogram and Box plot.
Histogram shows the distribution of data by grouping values into bins. The hist() function is used to
create it, with X-axis showing bins and Y-axis showing frequencies.
Syntax:
[Link](x, bins=None)
Parameter:
x: Input data.
bins: Number of bins (intervals) to group data.
Example: This code plots a histogram to show frequency distribution of total bill values from the list
x. It uses 10 bins and adds axis labels and a title for clarity.
Box plot
Box plot is a simple graph that shows how data is spread out. It displays the minimum, maximum,
median and quartiles and also helps to spot outliers easily.
Syntax:
[Link](x, notch=False, vert=True)
Parameter:
x: Data for which box plot is to be drawn (usually a list or array).
notch: If True, draws a notch to show the confidence interval around the median.
vert: If True, boxes are vertical. If False, they are horizontal.
Example: This code creates a box plot to show the data distribution and compare three groups
using matplotlib
import [Link] as plt
[Link](data)
[Link]("Groups")
[Link]("Values")
[Link]("Box Plot")
[Link]()
Output
Box Plot
7. Explain 3 dimensional plotting.
Matplotlib is the most popular choice for data visualization. While initially developed for plotting 2-D
charts like histograms, bar charts, scatter plots, line plots, etc., Matplotlib has extended its capabilities to
offer 3D plotting modules as well.
[Link] as plt
• The first one is a standard import statement for plotting using matplotlib, which
you would see for 2D plotting as well. The second import of the Axes3D class is
required for enabling 3D projections. It is, otherwise, not used anywhere else.
fig = [Link](figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
Output:
Example :
fig=[Link](figsize=(8,8))
ax=[Link](projection='3d')
[Link]()
t=[Link](0,10*[Link],[Link]/50)
x=[Link](t)
y=[Link](t)
ax.plot3D(x,y,t)
ax.set_title('3D Parametric Plot')
# Set axes label
ax.set_xlabel('x',labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('t', labelpad=20)
[Link]()
Output: