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).
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.
- 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
- Python
- Pandas
- Matplotlib
- Scikit-learn
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.
The following preprocessing steps are performed:
The project uses four numerical features:
feature_names = ['mass', 'width', 'height', 'color_score']
X = data[feature_names]
y = data['fruit_label']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.
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.
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.
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
Four different Machine Learning classification algorithms are used in this project.
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.
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.
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.
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.
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.
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.
============================================================
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.
Fruit-Classification/
β
βββ screenshots/
β βββ fruit_scatter_matrix.png
β
βββ fruit.txt
βββ fruit_classification.py
βββ requirements.txt
βββ LICENSE
βββ README.md
Install the required libraries using:
pip install pandas matplotlib scikit-learnYou can also install all dependencies using:
pip install -r requirements.txt- Make sure Python is installed.
- Place
fruit.txtin the project directory. - Create the
screenshotsfolder and add the scatter matrix image. - Install the required libraries.
- Run the Python script:
python fruit_classification.pyThe program will display the scatter matrix and print the training and test accuracy for all four Machine Learning models.
The main dependencies are:
pandas
matplotlib
scikit-learn
- 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
Contributions are welcome.
You can improve the visualization, add new Machine Learning algorithms, improve the evaluation process, or add new features to the project.
This project is licensed under the MIT License.
Mohammad Reza Bakhshandeh
Electrical Engineering (Electronics) Graduate
Interested in Python Development, Computer Vision, Machine Learning, and Artificial Intelligence.
