Skip to content

Latest commit

Β 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Machine Learning with Python

A collection of practical Machine Learning projects implemented in Python. This repository covers four major areas of machine learning:

  • Classification
  • Clustering
  • Regression
  • Recommender Systems

Each project includes its dataset and a Jupyter Notebook containing data exploration, preprocessing, model training, evaluation, visualization, and analysis.


πŸ“Œ Projects Overview

Project Task Dataset Main Techniques
πŸ«€ Classification Heart disease risk prediction heart.csv KNN, Decision Tree, Logistic Regression, SVM
πŸ‘₯ Clustering Customer segmentation Customer.csv K-Means, Hierarchical Clustering, DBSCAN
🏠 Regression Tehran house price prediction housePrice.csv Linear Regression
🎬 Recommender System Movie recommendation movies.csv, ratings.csv Collaborative Filtering, SVD, Content-Based Filtering

πŸ«€ 1. Classification Project

Overview

The goal of this project is to predict whether a patient belongs to the high-risk heart disease class based on medical information.

The dataset contains 303 patient records and 14 columns, including the target variable. The dataset was inspected for missing values and the analysis found no missing values in the available columns.

Dataset Features

Feature Description
age Age of the patient
sex Sex of the patient
cp Type of chest pain
trtbps Resting blood pressure
chol Serum cholesterol level
fbs Fasting blood sugar
restecg Resting ECG results
thalachh Maximum heart rate achieved
exng Exercise-induced angina
oldpeak ST depression
slp Slope
caa Number of major vessels
thall Thalassemia-related feature
output Target variable

The target column is separated from the input features:

X = dataframe.drop(columns=["output"])
y = dataframe["output"]

The dataset is divided into:

  • 80% training data
  • 20% testing data

using random_state=42.


Data Preprocessing

Feature standardization is applied using StandardScaler.

Standardization transforms features so that their mean is approximately 0 and their standard deviation is approximately 1. This is particularly important for distance-based algorithms such as KNN and for models such as SVM.

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

Machine Learning Models

K-Nearest Neighbors (KNN)

KNN predicts the class of a sample based on its nearest neighbors.

The project uses:

KNeighborsClassifier(n_neighbors=5)

The model is trained using the standardized features.

Decision Tree

A Decision Tree uses a sequence of feature-based decisions to classify samples.

The implementation uses:

DecisionTreeClassifier(random_state=42)

Decision Trees are easy to interpret and can model non-linear relationships, although they can overfit without appropriate controls.

Logistic Regression

Logistic Regression estimates the probability of belonging to each class.

A pipeline containing StandardScaler and LogisticRegression is used:

Pipeline([
    ("scaler", StandardScaler()),
    ("clf", LogisticRegression(max_iter=1000, random_state=42))
])

The model also produces class probabilities using predict_proba().

Support Vector Machine

The project also implements an SVM with an RBF kernel:

Pipeline([
    ("scaler", StandardScaler()),
    ("clf", SVC(
        kernel="rbf",
        probability=True,
        random_state=42
    ))
])

The model is evaluated using accuracy, confusion matrix, and classification report.


Evaluation

The classification models are compared using:

  • Accuracy
  • Precision
  • Recall
  • F1-Score
  • Confusion Matrix

A final comparison table is generated for:

  • K-Nearest Neighbors
  • Decision Tree
  • Logistic Regression
  • Support Vector Machine

πŸ‘₯ 2. Clustering Project

Overview

This project performs customer segmentation using unsupervised learning.

The dataset contains 200 customers and the following attributes:

  • Customer ID
  • Gender
  • Age
  • Annual Income
  • Spending Score

The dataset contains no missing values.

Dataset

Customer.csv

Feature Description
CustomerID Unique customer identifier
Gender Customer gender
Age Customer age
Annual Income (k$) Annual income
Spending Score (1-100) Customer spending score

Preprocessing

Gender is encoded numerically using LabelEncoder, while the customer ID is removed because it does not represent a meaningful clustering feature.

The remaining features are standardized using StandardScaler.

df["Gender"] = LabelEncoder().fit_transform(df["Gender"])

X = df.drop(columns=["CustomerID"])

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

K-Means Clustering

K-Means partitions the customers into k clusters based on their distance from cluster centroids.

The project follows these steps:

  1. Standardize the features.
  2. Use the Elbow Method to investigate the appropriate number of clusters.
  3. Train K-Means.
  4. Assign cluster labels.
  5. Evaluate the clustering using the Silhouette Score.

The resulting clusters are also visualized using PCA to project the data into two dimensions.


Hierarchical Clustering

The project also implements Agglomerative Hierarchical Clustering.

Different numbers of clusters are tested:

Number of Clusters Silhouette Score
2 0.3842
3 0.4610
4 0.4926
5 0.5538
6 0.5387
7 0.5198
8 0.4309

The best result is obtained with 5 clusters.

Different linkage methods are also compared:

Linkage Silhouette Score
Ward 0.5538
Complete 0.5531
Average 0.4794

Therefore, the final hierarchical clustering configuration is:

AgglomerativeClustering(
    n_clusters=5,
    linkage="ward"
)

with a final Silhouette Score of 0.5538.

DBSCAN

DBSCAN is also explored as a density-based clustering method.

It can:

  • Detect clusters with arbitrary shapes
  • Identify outliers/noise
  • Avoid explicitly specifying the number of clusters

The project uses eps and min_samples as the main DBSCAN parameters and evaluates the resulting clusters using the Silhouette Score while excluding noise points.

The reported Silhouette Score excluding noise is:

0.5110.


🏠 3. Regression Project

Overview

This project predicts the price of apartments in Tehran using their characteristics.

The dataset contains approximately 3,500 usable records after preprocessing and includes information such as:

  • Area
  • Number of rooms
  • Parking
  • Warehouse
  • Elevator
  • Address
  • Price in Tomans
  • Price in USD

The original dataset contains 3,479 records in the analyzed version.

Dataset

housePrice.csv

Feature Description
Area Apartment area
Room Number of rooms
Parking Whether parking is available
Warehouse Whether a warehouse is available
Elevator Whether an elevator is available
Address Approximate Tehran location
Price Price in Tomans
Price(USD) Price in USD

The project specifically predicts Price in Tomans and removes Price(USD) to avoid data leakage, since the dollar price could be directly used to derive the target.


Data Cleaning

The project performs several preprocessing steps:

  • Remove records without an address
  • Convert the Area column to the appropriate numerical format
  • Remove invalid apartment areas greater than 1500 square meters
  • Convert Boolean features to numerical values
  • Encode the categorical Address feature

The Boolean features are converted to integers:

X["Parking"] = X["Parking"].astype(int)
X["Warehouse"] = X["Warehouse"].astype(int)
X["Elevator"] = X["Elevator"].astype(int)

The Address feature is transformed using one-hot encoding:

X = pd.get_dummies(
    X,
    columns=["Address"],
    drop_first=True
)

Train/Test Split

The dataset is divided into:

  • 80% training
  • 20% testing

using random_state=42.


Linear Regression

The main regression model implemented in the project is:

from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

The reported results are:

Metric Result
MAE 1,472,406,083.91
RMSE 3,881,240,707.26
RΒ² 0.8009

The model achieves an RΒ² score of approximately 0.80, meaning that the model explains a substantial portion of the variation in apartment prices in the test data.


🎬 4. Movie Recommender System

Overview

This project implements a movie recommendation system using movie metadata and user ratings.

Two datasets are used:

movies.csv

Contains:

  • movieId
  • title
  • genres

ratings.csv

Contains:

  • userId
  • movieId
  • rating
  • timestamp

The system uses both Collaborative Filtering and Content-Based Filtering.


Recommendation Approaches

1. User-Based Collaborative Filtering

Recommends movies based on users with similar rating patterns.

2. Item-Based Collaborative Filtering

Recommends movies based on similarity between movies.

3. SVD

Singular Value Decomposition is used to learn hidden relationships between users and movies and predict unknown ratings.

SVD is ultimately used to generate personalized Top-N recommendations.

The project evaluates SVD using:

  • RMSE
  • MAE

Content-Based Filtering

The content-based system uses movie genres as the movie representation.

The genre information is transformed into numerical vectors using TF-IDF.

Then Cosine Similarity is used to identify movies with similar genre profiles.

The resulting genre matrix contains:

9742 movies Γ— 22 genre features

Top-N Recommendations

The project provides a function for generating personalized recommendations for a specific user.

For example:

top_recommendations = get_top_n_recommendations(
    user_id=example_user,
    n=10
)

This produces a ranked list of recommended movies with predicted ratings.


Recommendation Method Comparison

Method Main Information Advantage Limitation
User-Based CF Similar users Easy to understand Expensive for many users
Item-Based CF Similar movies More stable for movie data Needs enough ratings
SVD User-item ratings Good rating prediction Cold-start problem
Content-Based Movie genres Works without user ratings Limited to available content

The project concludes that a hybrid approach combining SVD with content similarity could potentially improve recommendation quality in a real-world system.


πŸ—‚οΈ Project Structure

Machine-Learning-with-python-main/
β”‚
β”œβ”€β”€ Classification Project/
β”‚   β”œβ”€β”€ heart.csv
β”‚   β”œβ”€β”€ heart.ipynb
β”‚   └── heart.py
β”‚
β”œβ”€β”€ Clustering Project/
β”‚   β”œβ”€β”€ Customer.csv
β”‚   └── code.ipynb
β”‚
β”œβ”€β”€ Recommender systems Project/
β”‚   β”œβ”€β”€ movies.csv
β”‚   β”œβ”€β”€ ratings.csv
β”‚   └── code.ipynb
β”‚
└── Reggresion Project/
    β”œβ”€β”€ housePrice.csv
    └── housePrice.ipynb

Note: The folder name Reggresion Project is kept as it exists in the original repository.


πŸ› οΈ Technologies Used

The projects are implemented primarily with Python and Jupyter Notebook.

Main libraries and tools used throughout the repository include:

  • Python
  • Jupyter Notebook
  • Pandas
  • NumPy
  • Scikit-learn
  • Matplotlib
  • SciPy
  • Scikit-Surprise

The notebooks use Python 3.13 in their kernel metadata.


πŸ“¦ Installation

Clone the repository:

git clone https://github.com/<your-username>/Machine-Learning-with-python.git
cd Machine-Learning-with-python

Create a virtual environment:

python -m venv venv

Activate it on Windows:

venv\Scripts\activate

Activate it on Linux/macOS:

source venv/bin/activate

Install the main dependencies:

pip install pandas numpy scikit-learn matplotlib scipy jupyter scikit-surprise

Start Jupyter Notebook:

jupyter notebook

Then open the notebook corresponding to the project you want to run.


▢️ How to Run

Each project is independent.

Classification

Classification Project/heart.ipynb

Make sure heart.csv is in the same directory as the notebook.

Clustering

Clustering Project/code.ipynb

Make sure Customer.csv is available in the same directory.

Regression

Reggresion Project/housePrice.ipynb

Make sure housePrice.csv is available in the same directory.

Recommender System

Recommender systems Project/code.ipynb

Make sure both datasets are available:

movies.csv
ratings.csv

The recommender notebook uses the surprise library for collaborative filtering, so scikit-surprise should be installed before running that notebook.


πŸ“Š Machine Learning Concepts Covered

This repository provides practical examples of several important machine learning concepts.

Supervised Learning

  • Classification
  • Regression
  • Train/Test Split
  • Feature Scaling
  • Model Evaluation

Unsupervised Learning

  • K-Means
  • Hierarchical Clustering
  • DBSCAN
  • PCA
  • Elbow Method
  • Silhouette Score

Recommendation Systems

  • User-Based Collaborative Filtering
  • Item-Based Collaborative Filtering
  • Matrix Factorization
  • SVD
  • TF-IDF
  • Cosine Similarity
  • Top-N Recommendation

πŸ“ˆ Evaluation Metrics

Different projects use different evaluation metrics depending on the machine learning task.

Classification

  • Accuracy
  • Precision
  • Recall
  • F1-Score
  • Confusion Matrix

Regression

  • MAE
  • RMSE
  • RΒ²

Clustering

  • Silhouette Score
  • Elbow Method / WCSS

Recommendation

  • RMSE
  • MAE

πŸ”¬ Key Results

Some of the notable results obtained during the experiments include:

Classification

Four different classification algorithms are trained and compared:

K-Nearest Neighbors
Decision Tree
Logistic Regression
Support Vector Machine

The models are evaluated using Accuracy, Precision, Recall and F1-Score.

Clustering

The optimized hierarchical clustering configuration:

Algorithm: Agglomerative Clustering
Clusters: 5
Linkage: Ward
Silhouette Score: 0.5538

Regression

The Linear Regression model achieved:

MAE:  1,472,406,083.91
RMSE: 3,881,240,707.26
RΒ²:   0.8009

Recommendation System

The recommender system combines:

Collaborative Filtering
        +
SVD
        +
Content-Based Filtering
        +
TF-IDF
        +
Cosine Similarity

and generates personalized Top-N movie recommendations.


🎯 Learning Objectives

The main purpose of this repository is to gain practical experience with the complete machine learning workflow:

Data Collection
      ↓
Data Exploration
      ↓
Data Cleaning
      ↓
Feature Engineering
      ↓
Feature Encoding
      ↓
Feature Scaling
      ↓
Train / Test Split
      ↓
Model Training
      ↓
Model Evaluation
      ↓
Visualization
      ↓
Model Comparison

The projects demonstrate how different machine learning techniques can be selected depending on the type of problem and available data.


⚠️ Disclaimer

The Classification Project is an educational machine learning project. Its predictions should not be considered medical advice or used as a substitute for professional medical diagnosis.

Similarly, the house price regression model is an experimental machine learning model and should not be considered a professional property valuation system.


πŸ“š Repository Contents

This repository was created as a practical collection of machine learning exercises and projects covering the major categories of machine learning:

Classification
Clustering
Regression
Recommendation Systems

Each project is implemented using Python and Jupyter Notebook and focuses on understanding the complete process of preparing data, applying machine learning algorithms, evaluating results, and interpreting the output.


πŸ‘¨β€πŸ’» Author

Mahan Banshi

Machine Learning Projects β€” Python