Skip to content

Latest commit

Β 

History

27 Commits

Folders and files

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

Repository files navigation

🍎 Fruit Classification using Machine Learning

A Machine Learning project that classifies different types of fruit using four classification algorithms: Logistic Regression, Decision Tree, K-Nearest Neighbors (KNN), and Support Vector Machine (SVM).

Overview

This project uses a fruit dataset containing numerical measurements of different fruits. The features include mass, width, height, and color score.

The dataset is processed and divided into training and testing sets. Feature scaling is performed using MinMaxScaler, and four different Machine Learning classification algorithms are trained and evaluated using both training and test accuracy.

The project also includes a scatter matrix visualization to explore the relationships between the input features.

Features

  • Fruit classification
  • Data preparation and feature selection
  • Scatter matrix visualization
  • Feature scaling using Min-Max normalization
  • Logistic Regression classification
  • Decision Tree classification
  • K-Nearest Neighbors classification
  • Support Vector Machine classification
  • Training and test accuracy evaluation

Technologies Used

  • Python
  • Pandas
  • Matplotlib
  • Scikit-learn

Dataset

The project uses the following dataset:

fruit.txt

The dataset contains information about different types of fruit.

The following features are used for classification:

mass
width
height
color_score

The target variable is:

fruit_label

which represents the fruit class.

The columns fruit_name and fruit_subtype are removed before training because they are not used as input features.

Data Preprocessing

The following preprocessing steps are performed:

Feature Selection

The project uses four numerical features:

feature_names = ['mass', 'width', 'height', 'color_score']
X = data[feature_names]
y = data['fruit_label']

Train/Test Split

The dataset is divided into training and testing sets using an 85/15 split.

85% β†’ Training data
15% β†’ Testing data

A fixed random_state=42 is used so that the same split can be reproduced.

Feature Scaling

MinMaxScaler is used to scale the input features before training the models.

scaler = MinMaxScaler()

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

This transforms the feature values to a common scale and is particularly useful for models such as KNN, Logistic Regression, and SVM.

Data Visualization

A scatter matrix is created to visualize the relationships between the input features.

scatter = scatter_matrix(
    X,
    c=y,
    s=40,
    figsize=(7,7),
    hist_kwds={'bins':15},
    marker='o',
    cmap=cmap
)

plt.suptitle('scatter-matrix for each input variable')
plt.savefig('fruit_scatter_matrix')
plt.show()

The scatter matrix displays pairwise relationships between the features together with the distribution of each individual feature.

Scatter Matrix

Machine Learning Workflow

The project follows the workflow below:

Fruit Dataset
      ↓
Remove Unused Columns
      ↓
Select Input Features
      ↓
Create Scatter Matrix
      ↓
Train/Test Split
      ↓
Min-Max Feature Scaling
      ↓
Train Classification Models
      ↓
Make Predictions
      ↓
Evaluate Training and Test Accuracy

Models

Four different Machine Learning classification algorithms are used in this project.

1. Logistic Regression

Logistic Regression is used to classify the fruit based on the selected numerical features.

log_model = LogisticRegression(max_iter=1000000)
log_model.fit(X_train, y_train)

The training and test accuracy are calculated using the model's score() method.

2. Decision Tree

A Decision Tree classifier is used as another approach for fruit classification.

dt_model = DecisionTreeClassifier()
dt_model.fit(X_train, y_train)

The model is evaluated on both the training and testing datasets.

3. K-Nearest Neighbors (KNN)

K-Nearest Neighbors is used to classify fruit based on the nearest training samples.

knn_model = KNeighborsClassifier()
knn_model.fit(X_train, y_train)

The model is evaluated using training and test accuracy.

4. Support Vector Machine (SVM)

Support Vector Machine is used as the fourth classification algorithm.

svm_model = SVC()
svm_model.fit(X_train, y_train)

The training and test accuracy are then calculated.

Model Training

Each classifier is trained using the same training dataset and evaluated using the same test dataset.

For each model, the project prints:

  • Training accuracy
  • Test accuracy

For example:

print(
    'Accuracy of Logistic Regression Classifier on training set: {:0.2f}'
    .format(log_model.score(X_train, y_train))
)

print(
    'Accuracy of Logistic Regression Classifier on test set: {:0.2f}'
    .format(log_model.score(X_test, y_test))
)

The same evaluation process is applied to the Decision Tree, KNN, and SVM models.

Evaluation

The models are evaluated using accuracy scores.

Two accuracy values are reported for each classifier:

  • Training Accuracy β€” measures performance on the training dataset.
  • Test Accuracy β€” measures performance on unseen test data.

Example Output

============================================================
Accuracy of Logistic Regression Classifier on training set: 0.XX
Accuracy of Logistic Regression Classifier on test set: 0.XX
============================================================
Accuracy of Decision Tree Classifier on training set: 1.00
Accuracy of Decision Tree Classifier on test set: 0.XX
============================================================
Accuracy of KNeighbors Classifier on training set: 0.XX
Accuracy of KNeighbors Classifier on test set: 0.XX
============================================================
Accuracy of Support vector machines(SVM) Classifier on training set: 0.XX
Accuracy of Support vector machines(SVM) Classifier on test set: 0.XX

The exact results depend on the dataset and the train/test split.

Project Structure

Fruit-Classification/
β”‚
β”œβ”€β”€ screenshots/
β”‚   └── fruit_scatter_matrix.png
β”‚
β”œβ”€β”€ fruit.txt
β”œβ”€β”€ fruit_classification.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ LICENSE
└── README.md

Installation

Install the required libraries using:

pip install pandas matplotlib scikit-learn

You can also install all dependencies using:

pip install -r requirements.txt

How to Run

  1. Make sure Python is installed.
  2. Place fruit.txt in the project directory.
  3. Create the screenshots folder and add the scatter matrix image.
  4. Install the required libraries.
  5. Run the Python script:
python fruit_classification.py

The program will display the scatter matrix and print the training and test accuracy for all four Machine Learning models.

Requirements

The main dependencies are:

pandas
matplotlib
scikit-learn

Future Improvements

  • Add a confusion matrix for each classifier
  • Add precision, recall, and F1-score
  • Compare the four models using a visualization
  • Experiment with different KNN values
  • Tune Decision Tree parameters
  • Tune SVM parameters and kernels
  • Add predictions for new fruit samples
  • Create a simple user interface for fruit classification

Contributing

Contributions are welcome.

You can improve the visualization, add new Machine Learning algorithms, improve the evaluation process, or add new features to the project.

License

This project is licensed under the MIT License.

Author

Mohammad Reza Bakhshandeh

Electrical Engineering (Electronics) Graduate

Interested in Python Development, Computer Vision, Machine Learning, and Artificial Intelligence.

About

A Machine Learning project for fruit classification using Logistic Regression, Decision Tree, KNN, and SVM with feature scaling and data visualization.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages