CS Term – 2 Nineteenth Feb Twenty-Six
Chapter 3:
Data Frames in Python
Benefits are:
▫ They are efficient for using and storing large amounts of Data
▫ They are easy to use and manipulate
▫ They provide a powerful set of tools for analysing Data
What are Data Frames?
•
DataFrames are a two-dimensional Data structure in python
•
•
•
They are similar to a spreadsheet, w rows and columns
The Data in a DataFrame can be of different Data types
The columns of a DataFrame represent the attributes of the Data, and the rows
represent the actual Data
Implementation of DataFrame in python:
•
You must import library pandas
Syntax is ( import pandas as pd )
•
Creating DataFrame in Python
Syntax is ( df1 = pd.DataFrame() )
•
Add Columns to the DataFrame
DataFrame_Name[‘Column_name’]=[ ]
•
Add Values to the DataFrame
DataFrame_Name[‘Column_name’]=[ values ]
•
Creating pandas DataFrame with custom Index
List = [100,200,300,400]
Df = pd.DataFrame(list, index = list, columns = [‘Teams’])
•
•
•
Note that is the index is more than the actual data itself a value error will come
DataFrames are essential in machine learning and data science for efficiently
handling large datasets.
Pandas allows easy creation of DataFrames from Excel files using the
read_excel() function.
•
Head function returns first 5 rows by default
Name.head( )
•
Tail function return last 6 rows by default
Name.tail( )
•
Info function used to get concise summary of the DataFrame
Name.info( )
•
Dtypes function will return the DataType of each attribute
Name.dtypes no brackets
•
Describe function is used to get the summary statistics
Name.describe( )
•
Shape function used to get the total rows and columns
Name.shape- no bracket
•
Size function used to get the product of rows and columns
Name.size- no bracket
•
Values_counts returns the unique counts for different values for a particular
column.
Name.value_counts( )
Visulizations in Python
Chapter 4
Why is visualizations important?
Real-world data is often large and complex, making traditional tables hard to analyze.
Data visualization helps present large datasets in a clearer and more understandable
way.
What is Visualization?
Visualization means showing data in a graphical form like charts and graphs so it is
easier to understand. It helps identify trends, patterns, and relationships for better
decision-making.
▫ Bar Chart:
Uses rectangular bars to compare diffrent categories based on their values.
▫ Pie Chart:
Shows data as slices of a circle to represent proportions of a whole.
▫ Line Graph:
Uses connected points to show trends over time or relationships between
variables.
Advantages of Visualization
▫ • Enhanced Understanding: Visualization helps users grasp complex concepts
and relationships more easily than raw data.
▫ • Insight Discovery: Visual representations of data can reveal patterns, trends,
and outliers that may not be apparent in tabular data.
▫ • Effective Communication: Visualizations facilitate clear and concise
communication of insights and findings to a diverse audience.
▫ • Decision Making: Visualizations aid in informed decision-making by presenting
data in a format that is easy to interpret and analyse.
o Library used for visualization in python is matplotlib.pyplot
Import matplotlib.pyplot as plt
o To create a graph plot function is used
Plt.plot(x_value,y_value)
o To create bar grpah bar( ) is used
Plt.bar( x_values,y_values) * It is important for x_values to be categorical and y_values
to be numirical
•
To create pie chart pie( ) is used
plt.pie(pie values)
•
To Create a barchart using values from a excel spreadsheet
import pandas as pd
import matplotlib.pyplot as plt
student_data = pd.read_excel('Student_dataN.xlsx')
student_data['Activity'].value_counts().plot(kind='bar')
plt.xlabel('Clubs')
plt.ylabel('Number of students')
plt.title('Students participation in Activites')
Plt.show( )
•
Creating an Pie chart using array
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 10, 25, 15])
plt.pie(y)
plt.show()
•
Making a pie chart with percentage
import matplotlib.pyplot as plt
Assignments = [5,3,1,4]
labels = ['English', 'Maths', 'Computers', 'Science']
plt.pie(Assignments, labels=labels, autopct='%1.1f%%')
plt.title("Assignments Pending")
plt.show()
•
This pie chart is clearer because it includes labels and percentages for each
slice. The title is set using the title() function. Two lists are used — one for
actual values and one for labels. The percentage of each slice is calculated
automatically based on the total. The pie() function takes arguments like
values, labels, and autopct to format how percentages are displayed (e.g., one
decimal place using %1.1f%%).
•
Making a line graph with 3 line using legend and labels and markers
import matplotlib.pyplot as plt
Month = [1,2,3,4,5]
avg_temp_Hyd = [25.88, 27.19, 30.89, 33.88, 35.99]
avg_temp_Bng = [24.81, 26.12, 28.97, 30.77, 31.91]
avg_temp_Mum = [18.81, 24.12, 28.12, 31.88, 32.90
plt.plot(Month, avg_temp_Hyd, label = "Hyd Avg Temp")
plt.plot(Month, avg_temp_Bng, ':', label = "Bng Avg Temp")
plt.plot(Month, avg_temp_Mum, '--', label = "Mum Avg Temp")
plt.xlabel("Months")
plt.ylabel("Average Temperature")
plt.title('Avg Temp Comparison of Hyd, Bng and Mum')
plt.legend()
plt.show()
Chapter 5 Machine Learning Machine Learning (ML) is a field where computers learn from data and improve over
time without being directly programmed. Instead of giving exact instructions, we train
computers using examples.
Examples of ML:
•
•
•
•
Spam Detection: Identifies spam emails.
Recommendation Systems: Suggests movies or products (like
Netflix/Amazon).
Predictive Analytics: Predicts stock prices or customer behavior.
Medical Diagnosis: Helps detect diseases from medical data.
Why Python for ML?
Python is popular because it is simple, easy to read, and has powerful libraries like
Scikit-learn, TensorFlow, and PyTorch that help build ML models.
Types of ML Problems:
1. Prediction: Predicts numerical values (e.g., house prices).
2. Classification: Classifies data into categories (e.g., spam or not spam).
In short, Machine Learning helps computers learn from past data to make smart
decisions or predictions.
Chapter 6 7 Simple / Multiple Linear Regression
Intro
SLR is a type of regression algorithms that computes the relationship between a
dependent and independent variable and a single independent variable. The
relationship shown is linear or a sloped straight line, hence it is called SLR
In SLR the dependent variable should be countinous value, while the independent
variable can be either continuous or categorical
▫
▫
▫
▫
▫
The dataset has only 2 columns.
The first column (x) is the independent variable (input).
The second column (y) is the dependent variable (output).
y depends on x — we use x to predict y.
Usually, x comes first and y comes second, but sometimes other datasets may
switch the order.
•
Importing libraries
•
•
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
Why?
•
•
•
pandas → to read Excel data
train_test_split → to divide data into training & testing
LinearRegression → to build SLR/MLR model
• Loading the Excel dataset
•
data = pd.read_excel("data.xlsx")
•
•
Why?
Reads the Excel test dataset into Python as a DataFrame.
• Selecting X and y using iloc
• For SLR (Single Linear Regression)
•
X = data.iloc[:, 0:1]
y = data.iloc[:, 1]
# Independent variable (must be 2D)
# Dependent variable
• For MLR (Multiple Linear Regression)
•
X = data.iloc[:, 0:-1] # All columns except last
y = data.iloc[:, -1]
# Last column is output
•
•
•
Why iloc?
Selects data using row and column numbers
0:1 keeps X in 2D format, which sklearn needs
• Splitting data into Train & Test
•
•
•
•
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=1/3, random_state=123)
Why?
80% training data → model learns
20% testing data → model performance check
•
random_state → gives same result every time
• Building the model
•
•
•
•
model = LinearRegression()
Why?
Creates a linear regression model object
Same code for SLR and MLR
• Fitting (Training) the model
•
model.fit(X_train, y_train)
•
•
•
Why?
Model learns the relationship between X and y
Calculates slope(s) and intercept
• Predicting values
•
•
•
•
y_pred = model.predict(X_test)
Why?
Predicts output values for test inputs
Used to compare with actual values
• Checking model score (Accuracy)
•
•
•
•
•
score = model.score(X_test, y_test)
print(score)
Why?
Gives R² score
Tells how well the model fits the data
Value closer to 1 = better model
• Predicting for UNKNOWN / USER INPUT VALUES
• SLR – User input
•
x_value = float(input("Enter value for X: "))
result = model.predict([[x_value]])
print("Predicted Y:", result[0])
•
•
•
Why?
Takes one input
Must be inside [[ ]] (2D format)
•
MLR – User input
•
x1 = float(input("Enter X1: "))
x2 = float(input("Enter X2: "))
x3 = float(input("Enter X3: "))
result = model.predict([[x1, x2, x3]])
print("Predicted Y:", result[0])
•
•
•
Why?
Takes multiple independent variables
Order must match dataset columns
• Predicting using a NEW TEST EXCEL FILE
•
•
•
•
new_data = pd.read_excel("test_data.xlsx")
predictions = model.predict(new_data)
print(predictions)
Why?
Used when external test dataset is given
Very important for exams & projects
• One-line difference
Type
SLR
MLR
X
One column
Multiple column
Logistic Regression (Classification)
Even though name says "Regression", it is used for classification.
1. Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
Why?
•
pandas → read Excel file
•
•
train_test_split → divide train & test
LogisticRegression → build classification model
•
metrics → check accuracy
2. Load Excel dataset
data = pd.read_excel("data.xlsx")
3. Select X and y (Same as MLR)
X = data.iloc[:, 0:-1]
y = data.iloc[:, -1]
# Independent variables
# Target (0 or 1)
4. Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=1/3, random_state=123)
5. Build model
logreg = LogisticRegression()
Why?
Creates logistic regression model object.
6. Train model
logreg.fit(X_train, y_train)
Model learns probability of class (0 or 1).
7. Predict
y_pred = logreg.predict(X_test)
Predicts class labels (0 or 1).
8. Check Accuracy
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
Why?
•
•
Checks how many predictions are correct.
Value closer to 1 = better model
9. Predict Unknown Value (User Input)
Step 1: Load Test Excel
test_data = pd.read_excel("test.xlsx")
Step 2: Predict
predictions = dtc.predict(test_data)
print(predictions)
Must match number of features.
Decision Tree Classifier
Works like MLR structure, but model type changes.
1. Import libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import metrics
2. Load dataset
data = pd.read_excel("data.xlsx")
3. Select X and y
X = data.iloc[:, 0:-1]
y = data.iloc[:, -1]
4. Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size= 1/3, random_state=123)
5. Build model
dtc = DecisionTreeClassifier()
Creates decision tree model.
6. Train model
dtc.fit(X_train, y_train)
Tree learns rules like:
•
•
If X1 > 10 → Class 1
Else → Class 0
7. Predict
y_pred = dtc.predict(X_test)
8. Check Accuracy
print("Accuracy:", metrics.accuracy_score(y_test, y_pred))
9. Predict Unknown Values
Step 1: Load Test Excel
test_data = pd.read_excel("test.xlsx")
Step 2: Predict
predictions = dtc.predict(test_data)
print(predictions)
VERY IMPORTANT EXAM DIFFERENCE
Logistic Regression
Uses probability
(sigmoid)
Linear boundary
Good for simple data
Less overfitting
Decision Tree
Uses tree rules
Non-linear boundary
Good for complex
data
Can overfit
Simple Structure Comparison
All models follow SAME steps:
1. Import
2. Load data
3. Select X, y
4. Split
5. Create model
6. Fit
7. Predict
8. Check accuracy
9. Predict unknown values
Only model class changes.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )