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.
| 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 |
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.
| 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.
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)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.
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 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().
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.
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
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.
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 |
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 partitions the customers into k clusters based on their distance from cluster centroids.
The project follows these steps:
- Standardize the features.
- Use the Elbow Method to investigate the appropriate number of clusters.
- Train K-Means.
- Assign cluster labels.
- Evaluate the clustering using the Silhouette Score.
The resulting clusters are also visualized using PCA to project the data into two dimensions.
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"
)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.
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.
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.
The project performs several preprocessing steps:
- Remove records without an address
- Convert the
Areacolumn to the appropriate numerical format - Remove invalid apartment areas greater than 1500 square meters
- Convert Boolean features to numerical values
- Encode the categorical
Addressfeature
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
)The dataset is divided into:
- 80% training
- 20% testing
using random_state=42.
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.
This project implements a movie recommendation system using movie metadata and user ratings.
Two datasets are used:
Contains:
movieIdtitlegenres
Contains:
userIdmovieIdratingtimestamp
The system uses both Collaborative Filtering and Content-Based Filtering.
Recommends movies based on users with similar rating patterns.
Recommends movies based on similarity between movies.
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
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
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.
| 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.
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 Projectis kept as it exists in the original repository.
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.
Clone the repository:
git clone https://github.com/<your-username>/Machine-Learning-with-python.git
cd Machine-Learning-with-pythonCreate a virtual environment:
python -m venv venvActivate it on Windows:
venv\Scripts\activateActivate it on Linux/macOS:
source venv/bin/activateInstall the main dependencies:
pip install pandas numpy scikit-learn matplotlib scipy jupyter scikit-surpriseStart Jupyter Notebook:
jupyter notebookThen open the notebook corresponding to the project you want to run.
Each project is independent.
Classification Project/heart.ipynb
Make sure heart.csv is in the same directory as the notebook.
Clustering Project/code.ipynb
Make sure Customer.csv is available in the same directory.
Reggresion Project/housePrice.ipynb
Make sure housePrice.csv is available in the same directory.
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.
This repository provides practical examples of several important machine learning concepts.
- Classification
- Regression
- Train/Test Split
- Feature Scaling
- Model Evaluation
- K-Means
- Hierarchical Clustering
- DBSCAN
- PCA
- Elbow Method
- Silhouette Score
- User-Based Collaborative Filtering
- Item-Based Collaborative Filtering
- Matrix Factorization
- SVD
- TF-IDF
- Cosine Similarity
- Top-N Recommendation
Different projects use different evaluation metrics depending on the machine learning task.
- Accuracy
- Precision
- Recall
- F1-Score
- Confusion Matrix
- MAE
- RMSE
- RΒ²
- Silhouette Score
- Elbow Method / WCSS
- RMSE
- MAE
Some of the notable results obtained during the experiments include:
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.
The optimized hierarchical clustering configuration:
Algorithm: Agglomerative Clustering
Clusters: 5
Linkage: Ward
Silhouette Score: 0.5538
The Linear Regression model achieved:
MAE: 1,472,406,083.91
RMSE: 3,881,240,707.26
RΒ²: 0.8009
The recommender system combines:
Collaborative Filtering
+
SVD
+
Content-Based Filtering
+
TF-IDF
+
Cosine Similarity
and generates personalized Top-N movie recommendations.
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.
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.
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.
Mahan Banshi
Machine Learning Projects β Python