Introduction to Python
Python is a general-purpose, high level programming language. It was created by Guido van
Rossum, and released in 1991.
Python Editors
There are various editors and Integrated Development Environments (IDEs) that we can use to
work with Python. Some popular options are PyCharm, Spyder, Jupyter Notebook, IDLE etc.
Jupyter Notebook is an open-source web application that allows to create and share documents
containing live code, equations, visualizations, and narrative text. It's widely used in data
science and research. It can be installed using Anaconda or with pip.
pip install notebook
Python Libraries
In the realm of data science and analytics, two powerful libraries stand out for their efficiency
and versatility: NumPy and Pandas. These libraries form the backbone of data manipulation and
analysis in Python, enabling users to handle large datasets with ease and precision.
➢ NumPy Library
NumPy, short for Numerical Python is a powerful library in Python used for numerical
computing. It is a general-purpose array-processing package.
NumPy can be installed using Python's package manager, pip.
pip install numpy
➢ Pandas Library
Pandas provides powerful data manipulation and aggregation functionalities, making it easy to
perform complex analyses and generate insightful visualizations.
Pandas can be installed using:
pip install pandas
Practical-1
AIM: Write a Python code to create a Series from Scalar Values
import pandas as pd
series1=[Link]([1,2,3])
print(series1)
Output Practical 1:
0 1
1 2
2 3
dtype: int64
Practical-2
AIM: Write a Python code to create 1 rank and 2 rank arrays.
#Creating a rank 1 Array
import numpy as np
arr=[Link]([1,2,3])
print("Array with Rank 1\n",arr)
#Creating a rank 2 Array
import numpy as np
arr=[Link]([[1,2,3],[4,5,6]])
print("Array with Rank 2\n",arr)
Output Practical 2:
Array with Rank 1
[1 2 3]
Array with Rank 2
[[1 2 3]
[4 5 6]]
Practical-3
AIM: Write a Python code to create and display Pandas DataFrames.
#Creation of a DataFrame from NumPy arrays
import numpy as np
import pandas as pd
array1=[Link]([90,100,110,120])
array2=[Link]([50,60,70])
array3=[Link]([10,20,30,40])
marksDF = [Link]([array1, array2, array3], columns=[ 'A', 'B', 'C', 'D'])
print(marksDF)
#Creation of a DataFrame from dictionary of array/lists
import pandas as pd
data = {'Name':['Varun', 'Ganesh', 'Joseph', 'Abdul','Reena'], 'Age':[37,30,38, 39,40]}
df = [Link](data)
print(df)
# Create list of dictionaries
import pandas as pd
listDict = [{'a':10, 'b':20}, {'a':5,'b':10,'c':20}]
a= [Link](listDict)
print(a)
Output Practical 3:
(1) A B C D
0 90 100 110 120.0
1 50 60 70 NaN
2 10 20 30 40.0
(2)
Name Age
0 Varun 37
1 Ganesh 30
2 Joseph 38
3 Abdul 39
4 Reena 40
(3)
a b c
0 10 20 NaN
1 5 10 20.0
Practical-4
AIM: Write a Python code to add a column and row to a Pandas DataFrame.
#Add a column and row to a DataFrame
import pandas as pd
data = {'Name':['Varun', 'Ganesh', 'Joseph', 'Abdul','Reena'], 'Age':[37,30,38, 39,40]}
df = [Link](data)
print(df)
df['Rollno']=[1,2,3,4,5]
print(df)
print("\nAfter adding a row\n")
[Link][len(df)]=['Rahul',34,6]
print(df)
Output Practical 4:
Name Age
0 Varun 37
1 Ganesh 30
2 Joseph 38
3 Abdul 39
4 Reena 40
After adding a column Rollno
Name Age Rollno
0 Varun 37 1
1 Ganesh 30 2
2 Joseph 38 3
3 Abdul 39 4
4 Reena 40 5
After adding a row
Name Age Rollno
0 Varun 37 1
1 Ganesh 30 2
2 Joseph 38 3
3 Abdul 39 4
4 Reena 40 5
5 Rahul 34 6
Practical-5
AIM: Write a Python code to delete a column and a row from a specified location from Pandas
DataFrame.
#Delete a column and row from a DataFrame
import pandas as pd
data = {'Name':['Varun', 'Ganesh', 'Joseph', 'Abdul','Reena'], 'Age':[37,30,38, 39,40]}
df = [Link](data)
print(df)
print("\nAfter deleting a column Age\n")
df=[Link]('Age',axis=1)
print(df)
print("\nAfter deleting a row\n")
df=[Link](1,axis=0)
print(df)
Output Practical 5:
Name Age
0 Varun 37
1 Ganesh 30
2 Joseph 38
3 Abdul 39
4 Reena 40
After deleting a column Age
Name
0 Varun
1 Ganesh
2 Joseph
3 Abdul
4 Reena
After deleting a row
Name
0 Varun
2 Joseph
3 Abdul
4 Reena
Practical-6
AIM: Write a Python code to display first 5 records from Pandas DataFrame.
# Display first 5 records
import pandas as pd
dict = {"Student": [Link](["Arnav","Neha","Priya","Rahul","Seema","Manav",
"Arpan"], index=[1,2,3,4,5,6,7]),"Marks": [Link]([85, 92, 78, 83, 7
8 , 90 , 89], index=[1,2,3,4,5,6,7]),"Sports": [Link](["Cricket",
"Volleyball","Hockey","Badminton","Badminton","Cricket","Volleyball"]
,index=[1,2,3,4,5,6,7])}
df = [Link](dict)
print(df)
print("\nFirst 5 records using head()")
[Link](5)
Output Practical 6:
Student Marks Sports
1 Arnav 85 Cricket
2 Neha 92 Volleyball
3 Priya 78 Hockey
4 Rahul 83 Badminton
5 Seema 78 Badminton
6 Manav 90 Cricket
7 Arpan 89 Volleyball
First 5 records using head()
Student Marks Sports
1 Arnav 85 Cricket
2 Neha 92 Volleyball
3 Priya 78 Hockey
4 Rahul 83 Badminton
5 Seema 78 Badminton
Practical-7
AIM: Write a Python code to display last 5 records from Pandas DataFrame.
# Display last 5 records
import pandas as pd
dict = {"Student": [Link](["Arnav","Neha","Priya","Rahul","Seema","Manav",
"Arpan"], index=[1,2,3,4,5,6,7]),"Marks": [Link]([85, 92, 78, 83, 7
8 , 90 , 89], index=[1,2,3,4,5,6,7]),"Sports": [Link](["Cricket",
"Volleyball","Hockey","Badminton","Badminton","Cricket","Volleyball"]
,index=[1,2,3,4,5,6,7])}
df = [Link](dict)
print(df)
print("\nLast 5 records using tail()")
[Link](5)
Output Practical 7:
Student Marks Sports
1 Arnav 85 Cricket
2 Neha 92 Volleyball
3 Priya 78 Hockey
4 Rahul 83 Badminton
5 Seema 78 Badminton
6 Manav 90 Cricket
7 Arpan 89 Volleyball
Last 5 records using tail()
Student Marks Sports
3 Priya 78 Hockey
4 Rahul 83 Badminton
5 Seema 78 Badminton
6 Manav 90 Cricket
7 Arpan 89 Volleyball
Practical-8
AIM: Write a Python code to display missing values from Pandas DataFrame.
import pandas as pandas
Result_sheet={'Maths':[Link]([94,96,67,89],index=['Sunita','Rahul','Anuj','Neera']),
'Science':[Link]([78,[Link],97,78],index=['Sunita','Rahul','Anuj','Neera']),
'English':[Link]([77,89,90,84],index=['Sunita','Rahul','Anuj','Neera']),
'Hindi':[Link]([83,90,91,[Link]],index=['Sunita','Rahul','Anuj','Neera'])}
marks=[Link](Result_sheet)
print(marks)
print("\n isnull() is used for checking missing values,True represents missing value :\n")
print([Link]())
print("\n Missing value in Science subject : ",marks['Science'].isnull().any())
print("\n Missing value in English subject : ",marks['English'].isnull().any())
print("\nTotal missing values in Dataframe marks: ",[Link]().sum().sum())
print("\nMissing values filled with zeros : ")
FillZero = [Link](0)
print(FillZero)
Output Practical 8:
Maths Science English Hindi
Sunita 94 78.0 77 83.0
Rahul 96 NaN 89 90.0
Anuj 67 97.0 90 91.0
Neera 89 78.0 84 NaN
isnull() is used for checking missing values,True represents missing value :
Maths Science English Hindi
Sunita False False False False
Rahul False True False False
Anuj False False False False
Neera False False False True
Missing value in Science subject : True
Missing value in English subject : False
Total missing values in Dataframe marks: 2
Missing values filled with zeros :
Maths Science English Hindi
Sunita 94 78.0 77 83.0
Rahul 96 0.0 89 90.0
Anuj 67 97.0 90 91.0
Neera 89 78.0 84 0.0
Practical-9
AIM: Write a Python code to read CSV File and convert it into Pandas DataFrame and perform
statistical functions on the dataset to check the data, checking missing values, filling missing
data etc.
import pandas as pd
#convert the csv to DataFrame
df=pd.read_csv("student_result.csv")
print(df)
#Save Dataframe to csv file
df.to_csv("[Link]",index=False)
#check for missing values
print("\nDataframe of missing values\n",[Link]())
#Estimate the missing value
FillZero = [Link](0)
print("\nFill missing values with zeros\n",FillZero)
Output Practical 9:
Rollno Name English Maths AI
0 1 Apoorva 67 78.0 88.0
1 2 Ronit 89 80.0 92.0
2 3 Harshita 79 NaN 93.0
3 4 Bhavik 88 91.0 98.0
4 5 Deepti 82 86.0 NaN
5 6 Akshita 89 90.0 99.0
Dataframe of missing values
Rollno Name English Maths AI
0 False False False False False
1 False False False False False
2 False False False True False
3 False False False False False
4 False False False False True
5 False False False False False
Fill missing values with zeros
Rollno Name English Maths AI
0 1 Apoorva 67 78.0 88.0
1 2 Ronit 89 80.0 92.0
2 3 Harshita 79 0.0 93.0
3 4 Bhavik 88 91.0 98.0
4 5 Deepti 82 86.0 0.0
5 6 Akshita 89 90.0 99.0
Practical-10
AIM: Write a Python code to Evaluate a Model.(Linear Regression)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
# Sample data
data = {
'X': [1, 2, 3, 4, 5],
'Y': [2, 4, 6, 8, 10]
}
# Create DataFrame
df = [Link](data)
# Split data
X = df[['X']] # Feature
y = df['Y'] # Target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create and train model
model = LinearRegression()
[Link](X_train, y_train)
print("X_train\n",X_train)
print("y_train\n",y_train)
# Predict and evaluate
y_pred = [Link](X_test)
print("X_test\n",X_test)
print("Predictions:", y_pred)
print("Actual:", y_test.values)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
Output Practical 10:
X_train
X
4 5
2 3
0 1
3 4
y_train
4 10
2 6
0 2
3 8
Name: Y, dtype: int64
X_test
X
1 2
Predictions: [4.]
Actual: [4]
Mean Squared Error: 0.0
Orange Data Mining Tool
Orange is a powerful open-source data mining and machine learning tool that combines
simplicity with functionality through its intuitive visual programming interface. Designed for
both beginners and professionals, it enables users to build complex data analysis workflows
effortlessly by connecting pre-built, interactive widgets. Orange supports a wide range of data
science tasks, including classification, regression, clustering, and insightful data visualizations,
all without the need for coding.
It comes with built-in datasets like the famous Iris dataset, allowing users to experiment with
machine learning models such as decision trees, k-NN, and support vector machines. What sets
Orange apart is its ability to provide real-time feedback through visual tools like scatter plots,
confusion matrices, and evaluation metrics such as accuracy, precision, recall, and F1 score. The
inclusion of cross-validation ensures robust model assessment, making it a reliable platform for
teaching, research, and rapid prototyping. With its engaging interface and powerful capabilities,
Orange transforms the way users learn and apply data science.
Practical-11
AIM: Write step-by-step procedure for Data Visualization of Iris Flower Dimensions
using Orange Data Mining Tool.
Steps:
1. Launch Orange Data Mining Software
➢ Open Orange and start with a blank canvas.
2. Add the Data Widget
➢ Drag the "File" widget onto the canvas to load data.
3. Load the Iris Dataset
➢ Double-click the File widget.
➢ Select the iris dataset and set the "iris" column as the target.
4. Display Data in a Table
➢ Add the "Data Table" widget.
➢ Connect the File widget to the Data Table widget to view the dataset in tabular
form.
5. Explore the Dataset
➢ Open the Data Table to view all 150 samples, including sepal/petal lengths and
widths.
6. Visualize with a Scatter Plot
➢ Add the "Scatter Plot" widget.
➢ Connect it to the File widget to visualize relationships between variables (e.g.,
sepal length vs. width).
7. Interpret and Experiment with Visualizations
➢ Analyze the scatter plot.
➢ Try other visualization widgets (e.g., histogram, box plot, parallel coordinates)
Practical-12
AIM: Perform Classification of Iris Flowers with Orange Data Mining.
1. Prepare Testing Data
• Create a spreadsheet with columns: sepal length, sepal width, petal length, petal width
(all lowercase).
• Enter the measured values for each iris flower sample, using the same units as the
training data (e.g., centimeters).
2. Set Up Classification in Orange
• Open Orange and add a "File" widget to load the training data (built-in iris dataset).
• Add a "Tree" widget and connect it to the training File widget to create a decision tree
model.
3. Use Predictions Widget
• Add the "Predictions" widget to the canvas.
• Connect the training data File and Tree widget to the Predictions widget.
• Add another "File" widget to load the testing data (spreadsheet you created).
• Connect the testing data File widget to the Predictions widget.
4. Interpret Results
• Open the Predictions widget to view the predicted iris flower types for each test sample.
• Review the output to check the accuracy and results of the classification.
Practical-13
AIM: Evaluating the Classification Model with Orange Mining Tool.
1. Set Up Model Evaluation
• Add a "File" widget and load the training dataset (iris).
• Add a "Tree" widget and connect it to the File widget to build the model.
• Add the "Test and Score" widget.
• Connect the File and Tree widgets to Test and Score to perform evaluation using 10-fold
cross-validation.
2. Analyze Evaluation Metrics
• Open the Test and Score widget to view metrics:
o Accuracy: Correct predictions out of total.
o Precision: True Positives / Predicted Positives.
o Recall: True Positives / Actual Positives.
o F1 Score: Harmonic mean of precision and recall.
• These metrics help assess model effectiveness across different classes.
3. View Confusion Matrix
• Add the "Confusion Matrix" widget.
• Connect it to the Test and Score widget.
• Open the Confusion Matrix to see:
o True positives, false positives, false negatives, and true negatives.
o Identify which classes are predicted well or where confusion occurs.
Practical-14
AIM: Perform Image analytics using the Orange data mining tool.
1. Install Required Add-On
o Open Orange, go to Options > Add-ons, and install the Image Analytics add-on.
o Restart Orange to activate the new widget panel.
2. Prepare Image Dataset
o Obtain or create a folder containing image files (e.g., pictures of objects, animals,
etc.).
o Save it on your computer for easy access.
3. Import Image Data
o Use the Import Images widget to load the folder of images into Orange.
4. Visualize the Images
o Connect the Image Viewer widget to the import widget to preview and inspect the
images.
5. Convert Images to Numeric Data (Embeddings)
o Use the Image Embedding widget to transform images into numerical vectors
using a pre-trained deep learning model.
6. Compute Image Similarities
o Add the Distance widget and choose an appropriate distance metric (e.g., cosine)
to compare image embeddings.
7. Apply Clustering Algorithm
o Use the Hierarchical Clustering widget (or any other clustering widget) to group
similar images based on computed distances.
8. Explore and Interpret Clusters
o Connect another Image Viewer to the clustering widget to visualize how images
are grouped.
o Analyze the results and observe patterns or similarities in image clusters.
Practical-15
AIM: Write down steps to visualize word frequencies with Word Cloud using the Orange
Data Mining tool.
1. Install Text Add-On
o Go to Options > Add-ons, install the Text add-on, and restart Orange.
2. Load or Create Text Data
o Use the Corpus widget to load text files, or the Create Corpus widget to manually
input your own text.
3. View Text Content
o Connect the Corpus Viewer widget to explore and search through the text data.
4. Visualize Word Frequencies
o Use the Word Cloud widget to display common words based on frequency from
the text.
5. Preprocess the Text
o Add the Preprocess Text widget to clean the text by:
▪ Lowercasing
▪ Removing punctuation and stop words
▪ Optional: Stemming or Lemmatization
6. View Cleaned Text
o Connect the Preprocess Text output to the Word Cloud to visualize the refined,
meaningful words.
Practical -16
AIM: Create a Data Story using all steps of Data Storytelling.
Objective:
Compelling a data-driven story showing how the Mid-Day Meal Scheme (MDMS), launched in
1995, influenced student enrollment, attendance, and dropout rates in a specific state over time.
Step-by-Step Solution with Simulated Data
Step 1: Collect and Structure Data
Required data (year-wise from 1990–2025 ideally):
• Student Enrollment
• Student Attendance %
• Dropout Rates %
• MDMS Implementation Year: 1995
• Any relevant external factors: policy changes, economic crises, pandemic years (e.g.,
2020 COVID-19)
Dropout
Year Enrollment Attendance % Notes
Rate %
1990 5,00,000 72% 25% Pre-MDMS
1995 6,50,000 80% 18% MDMS Launched
2000 7,50,000 85% 12% Scheme Expansion
2005 8,10,000 87% 9%
2010 8,70,000 89% 6% Nutrition Boost
2015 9,00,000 91% 5% Digital Monitoring
2020 9,50,000 74% 10% COVID-19 Impact
2023 10,00,000 88% 4% Post-pandemic rise
Step 2: Create Visualizations
1. Line Graph: Dropout Rate vs Year
Y-axis: Dropout Rate (%)
X-axis: Year
Highlights: A visible decline from 1995 to 2015, slight spike in 2020 due to COVID, and
recovery in 2023.
2. Bar Chart: Enrollment Over the Years
Y-axis: Enrollment
X-axis: Year
Highlights: Steady growth post-1995.
3. Combo Chart: Attendance % and Dropout Rate
Left Y-axis: Attendance %
Right Y-axis: Dropout Rate %
X-axis: Year
Highlights: As attendance increased, dropout decreased — clear inverse relationship.
Step 3: Analyze and Interpret Trends
Key Observations:
• Post-1995, both enrollment and attendance significantly improved.
• Dropout rates dropped from 25% in 1990 to 5% in 2015.
• COVID-19 in 2020 temporarily disrupted attendance and increased dropouts, but trends
recovered by 2023.
• There's a strong inverse correlation between attendance and dropout rate.
Step 4: Include External Factors
• 1995: MDMS launch = major positive change.
• 2010 onwards: Focus on nutrition quality.
• 2015: Technology in monitoring meals = improved outcomes.
• 2020: Pandemic disrupted school access, temporarily spiked dropout.
Step 5: Final Narrative (Data Story)
Since the launch of the Mid-Day Meal Scheme in 1995, student dropout rates have steadily
declined, indicating a positive impact of the program. From a worrying 25% dropout rate in 1990,
the numbers fell to just 5% by 2015. This trend aligns with consistent increases in enrollment and
attendance, suggesting that providing free meals incentivized regular school attendance. Though
the COVID-19 pandemic briefly reversed some progress, post-pandemic data shows a rebound
in both attendance and retention. The story clearly demonstrates how a well-implemented social
program can dramatically improve educational outcomes.