Data Science and Machine Learning Implementation in Python

Statistical Analytics with Pandas

Import library
import pandas as pd

Part 1: Descriptive Statistics

Load dataset
df = pd.read_csv("stat.csv")

print("First 5 rows:")
print(df.head())

Group by categorical column (income) and numeric column (age)
print("\nSummary Statistics (Grouped by income):")
print(df.groupby("income")["age"].describe())

Individual Statistics

print("\nMean:")
print(df.groupby("income")["age"].mean())

print("\nMedian:")
print(df.groupby("income")["age"].median())

print("\nMin:")
print(df.groupby(

Read More

Implement BERT for Sentiment Analysis in PyTorch

This implementation demonstrates how to build a BERT-based model from scratch using PyTorch and the Hugging Face Transformers library for sentiment classification.

Required Libraries and Imports

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import AutoTokenizer
from datasets import load_dataset

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")

The Self-Attention Mechanism

The core of the Transformer architecture is the Multi-Head Self-Attention mechanism,

Read More

Python Machine Learning Code Snippets Collection

Python Machine Learning Code Snippets

Hierarchical Clustering Example

This snippet demonstrates hierarchical clustering using scipy and visualizes the result as a dendrogram, followed by applying Agglomerative Clustering.

import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sch
import numpy as np
from sklearn.cluster import AgglomerativeClustering

X = np.random.rand(50, 2)
dendrogram = sch.dendrogram(sch.linkage(X, method='ward'))
plt.title('Dendrogram')
plt.show()

hc = AgglomerativeClustering(
Read More

Machine Learning Algorithms Implementation Showcase

# ============================
# 1. K-MEANS CLUSTERING
# ============================

# Create synthetic 2D data with 3 clusters
X_blobs, y_blobs = make_blobs(n_samples=300, centers=3, random_state=42)

# Fit KMeans
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
kmeans.Fit(X_blobs)

# Get cluster labels
cluster_labels = kmeans.Labels_
centers = kmeans.Cluster_centers_

print(“KMeans cluster centers:\n”, centers)

# Plot clusters
plt.
Figure(figsize=(6, 5))
plt.Scatter(X_blobs[:, 0], X_blobs[:, 1], c=cluster_

Read More

CSMA/CD Ethernet Collision Detection and LAN Access

CSMA/CD Collision Detection in Ethernet LANs

CSMA/CD (Carrier Sense Multiple Access/Collision Detection) is a media access control method that was widely used in early Ethernet technology and LANs when networks used a shared bus topology and each node (computers) was connected by coaxial cables. Today, Ethernet is typically full-duplex and the topology is either star (connected via a switch or router) or point-to-point (direct connection). Hence, CSMA/CD is not commonly used in modern switched, full‑duplex

Read More

Machine Learning Algorithms: KNN, LWR, Random Forest, SVM

K-Nearest Neighbors (KNN) Algorithm Implementation

This section demonstrates the implementation of the K-Nearest Neighbors (KNN) algorithm using the Iris dataset in Python with scikit-learn.

Python Code for KNN

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np

# Load the Iris dataset
dataset = load_iris()

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test =
Read More