Program - 1
Aim: Introduction to Jupyter IDE and its libraries Pandas and Numpy.
Theory: Jupyter IDE (Integrated Development Environment) refers to the environment where
users interact with Jupyter Notebooks, which is part of Project Jupyter. It is widely used for data
science, machine learning, numerical computation, and scientific research.
Key Features of Jupyter IDE:
1. Interactive Coding: Write and execute code in cells, making testing and iteration easy.
2. Multi-language Support: Mainly used for Python but supports over 40 languages via
"kernels" (e.g., R, Julia).
3. Data Visualization: Easily integrate with libraries like Matplotlib, Seaborn, and Plotly.
4. Documentation Friendly: Use Markdown to create readable documents mixing code,
output, and explanation.
5. Notebook Format (.ipynb): Stores code, outputs, and formatting in JSON format.
Common Use Cases:
Data exploration and visualization
Machine learning modeling
Academic research and education
Reproducible workflows
Collaborative projects
Installation:
Pandas - Python Data Analysis Library:
Pandas is a powerful open-source library used for data manipulation, analysis, and cleaning. It
provides easy-to-use data structures and functions.
Key Features :
Data Structures:
Series: One-dimensional labeled array
DataFrame: Two-dimensional labeled table (rows & columns)
Handling Missing Data
Data Filtering & Slicing
GroupBy Operations
Merging and Joining Datasets
Reading/Writing Data from CSV, Excel, SQL, etc.
Example demonstrating Pandas:
NumPy - Numerical Python:
NumPy is a fundamental library for numerical computations in Python. It is known for its high-
performance multi-dimensional array object.
Key Features :
ndarray: Efficient n-dimensional array
Mathematical Functions: Linear algebra, statistics, Fourier transforms
Broadcasting: Efficient operations on arrays of different shapes
Vectorized Operations: Faster computation without Python loops
Integration with other libraries (Pandas, SciPy, etc.)
Example demonstrating Numpy:
Program - 2
Aim: Program to demonstrate Simple Linear regression and Logistic Regression
Theory:
Simple Linear Regression:
Simple Linear Regression is a statistical technique used to study the relationship between two
continuous variables - one independent variable (X) and one dependent variable (Y). It assumes
that the relationship between X and Y can be represented with a straight line:
𝑌 = 𝛽0 + 𝛽1𝑋 + 𝜖
Where:
Y = Dependent variable (what we want to predict)
X = Independent variable (predictor)
𝛽0 = Intercept
𝛽1 = Slope (effect of X on Y)
ϵ = Error term
The goal is to minimize the error (difference between actual and predicted values) using the
method of least squares. It is commonly used in predicting continuous values like house prices,
marks scored, or sales based on given inputs.
Key Features:
Establishes a linear relationship between one independent variable and one dependent
variable.
Uses the least squares method to minimize prediction errors.
Provides coefficients (intercept and slope) that explain how much the dependent
variable changes with the independent variable.
Easy to interpret and visualize with a straight-line graph.
Advantages:
Simple to understand and implement.
Computationally efficient and works well with small datasets.
Provides clear interpretability of results.
Useful for predicting continuous outcomes (e.g., sales, temperature, prices).
Logistic Regression:
Logistic Regression is used when the dependent variable is categorical (e.g., 0/1, Yes/No,
Pass/Fail). Unlike linear regression, it predicts the probability of belonging to a class rather than
a continuous value.
It uses the sigmoid (logistic) function to map predicted values into a probability between 0 and
1:
P(Y = 1 ∣ X) = ( )
Where:
P(Y = 1∣X) = Probability that outcome is 1 given input X
β0, β1 = Model coefficients
Based on a threshold (commonly 0.5), the probability is converted into class labels. Logistic
regression is widely applied in problems like spam detection, disease diagnosis, loan default
prediction, etc.
Key Features:
Used for binary or categorical dependent variables (e.g., 0/1, Yes/No).
Employs the sigmoid function to map outputs into probabilities (0–1 range).
Can be extended to multi-class classification (Multinomial Logistic Regression).
Provides probability estimates along with classification results.
Advantages:
Simple and widely used in classification tasks.
Provides interpretable coefficients (odds ratios).
Handles both continuous and categorical predictor variables.
Less prone to overfitting for smaller datasets compared to complex models.
Forms the basis for understanding more advanced classification algorithms.
Program 5
Aim: Program to Demonstrate decision tree - ID3 Algorithm
Theory:
Decision Tree is a supervised machine learning algorithm used for classification and
regression. The ID3 (Iterative Dichotomiser 3) algorithm builds the tree using the
Information Gain metric to select the best attribute at each step.
Key Terms:
Entropy: Measures the impurity in the dataset.
Information Gain: Reduction in entropy after splitting the dataset on an attribute.
Code:-
import pandas as pd
from [Link] import LabelEncoder
from [Link] import DecisionTreeClassifier
from sklearn import tree
import [Link] as plt
data = {
'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rain', 'Rain', 'Rain', 'Overcast',
'Sunny', 'Sunny', 'Rain', 'Sunny', 'Overcast', 'Overcast', 'Rain'],
'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool',
'Mild', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'],
'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal',
'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'],
'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong',
'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'],
'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes',
'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}
df = [Link](data)
le = LabelEncoder()
for column in [Link]:
df[column] = le.fit_transform(df[column])
features = ['Outlook', 'Temperature', 'Humidity', 'Wind']
X = df[features]
y = df['Play']
model = DecisionTreeClassifier(criterion='entropy') # ID3 uses entropy
[Link](X, y)
[Link](figsize=(12, 8))
tree.plot_tree(model, feature_names=features, class_names=['No', 'Yes'],
filled=True)
[Link]("Decision Tree using ID3 Algorithm")
[Link]()
Output:
Program 6
Aim: Program to Demonstrate DBSCAN clustering algorithm.
Theory:
DBSCAN (Density-Based Spatial Clustering of Applications with Noise) is an unsupervised
clustering algorithm that groups together points that are closely packed and marks points in
low-density regions as outliers.
eps: Maximum distance between two samples to be considered as neighbors.
min_samples: Minimum number of points to form a dense region (cluster).
Code:-
from [Link] import load_iris
from [Link] import StandardScaler
from [Link] import DBSCAN
import [Link] as plt
import pandas as pd
iris = load_iris()
X = [Link]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
dbscan = DBSCAN(eps=0.5, min_samples=5)
clusters = dbscan.fit_predict(X_scaled)
df = [Link](X, columns=iris.feature_names)
df['Cluster'] = clusters
[Link](figsize=(8, 5))
[Link]([Link][:, 0], [Link][:, 1], c=df['Cluster'], cmap='rainbow',
edgecolors='k')
[Link]('Sepal Length')
[Link]('Sepal Width')
[Link]('DBSCAN Clustering on Iris Dataset')
[Link](True)
[Link]()
Output:
Program 7
Aim: Program to Demonstrate k nearest neighbor flowers classification.
Theory:
k-Nearest Neighbour (k-NN) is a simple, supervised machine learning algorithm used for
classification and regression.
In classification, it classifies a data point based on how its neighbors are classified.
Code:-
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, classification_report
import pandas as pd
iris = load_iris()
X = [Link] # Features
y = [Link] # Labels (0, 1, 2)
df = [Link](X, columns=iris.feature_names)
df['Species'] = y
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5,
random_state=40)
k = 3 # Number of neighbors
model = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred,
target_names=iris.target_names))
Output:
Program 8
Aim: Program to Demonstrate K means clustering algorithm on handwritten dataset.
Theory:
K-Means is an unsupervised machine learning algorithm used for clustering. It partitions
data into K clusters based on feature similarity.
How It Works:
1. Choose the number of clusters (K).
2. Randomly initialize K centroids.
3. Assign each point to the nearest centroid.
4. Recalculate centroids as the mean of assigned points.
5. Repeat until convergence.
Code:-
import numpy as np
import [Link] as plt
import seaborn as sns
from [Link] import load_digits
from [Link] import KMeans
from [Link] import confusion_matrix, adjusted_rand_score
from [Link] import StandardScaler
digits = load_digits()
X = [Link] # 1797 samples, 64 features (8x8 pixels)
y = [Link] # True labels (0-9)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(n_clusters=10, random_state=42)
clusters = kmeans.fit_predict(X_scaled)
conf_mat = confusion_matrix(y, clusters)
label_map = [Link](conf_mat, axis=1)
mapped_clusters = [Link]([label_map[cluster] for cluster in clusters])
ari = adjusted_rand_score(y, mapped_clusters)
print("Confusion Matrix:")
print(conf_mat)
print("\nLabel Mapping (Cluster -> Digit):", label_map)
print("\nAdjusted Rand Index:", ari)
[Link](figsize=(10, 6))
[Link](conf_mat, annot=True, fmt='d', cmap='viridis',
xticklabels=range(10), yticklabels=range(10))
[Link]("Cluster Label")
[Link]("True Digit Label")
[Link]("Confusion Matrix of K-Means on Handwritten Digits")
[Link]()
fig, axes = [Link](2, 5, figsize=(10, 4))
for i in range(10):
ax = axes[i // 5, i % 5]
centroid = kmeans.cluster_centers_[i].reshape(8, 8)
centroid =
scaler.inverse_transform([kmeans.cluster_centers_[i]]).reshape(8, 8) #
Inverse scaling
[Link](centroid, cmap='gray')
ax.set_title(f'Cluster {i} (Digit {label_map[i]})')
[Link]('off')
plt.tight_layout()
[Link]()
Output: