A Python-based end-to-end machine learning project that demonstrates classification pipelines using scikit-learn, XGBoost, and TensorFlow. This repository includes implementations for training Random Forest classifiers on fertilizer recommendation datasets with complete data preprocessing, model training, and evaluation workflows.
This project demonstrates best practices for building scalable ML pipelines with scikit-learn. It includes:
- Data acquisition from Google Drive
- Automated data preprocessing and cleaning
- Custom transformer implementations
- Model training with hyperparameter tuning
- Comprehensive evaluation metrics
The project focuses on a fertilizer recommendation classification task using custom transformer classes that integrate seamlessly with scikit-learn's pipeline architecture.
- Google Drive Integration: Direct data loading from Google Drive using OAuth2 or public URLs
- Custom Transformers: Reusable, modular preprocessing components (
NullChecker,LabelEncoderTransformer,StandardScalerTransformer) - Multiple ML Algorithms: RandomForest and XGBoost implementations
- Complete Pipeline: End-to-end workflow from data ingestion to model evaluation
- Flexible Authentication: Two approaches—OAuth2 flow and direct URL access
.
├── RandomForestClassifier.py # Main pipeline with Google Drive OAuth2 authentication
├── RandomForestClassifier2.py # Simplified variant using direct Google Drive URL
├── requirements.txt # Python dependencies
├── input.txt # Configuration file (stores Google Drive file_id)
├── .devcontainer/ # Dev container setup for consistent environments
└── README.md # This file
| Component | Technology |
|---|---|
| Language | Python 3.7+ |
| ML Frameworks | scikit-learn 1.3.1, XGBoost 1.7.6, TensorFlow |
| Data Processing | pandas, NumPy |
| Visualization | matplotlib, seaborn 0.12.2 |
| Cloud Integration | Google Drive API (googleapiclient 2.98.0) |
| Authentication | google-auth 2.23.0, google-auth-oauthlib 1.0.0 |
- Python 3.7 or higher
- pip package manager
- Google account (for accessing datasets on Google Drive)
- Clone the repository:
git clone https://github.com/ssampathkumar104/machinelearning.git
cd machinelearning- Install dependencies:
pip install -r requirements.txt- Set up Google Drive credentials (for
RandomForestClassifier.py):- Download
credentials.jsonfrom Google Cloud Console - Place it in the project root directory
- Create
input.txtwith your Google Drive file ID:file_id=YOUR_DRIVE_FILE_ID
- Download
python RandomForestClassifier.pyThis script:
- Authenticates with Google Drive using OAuth2
- Downloads the CSV file specified in
input.txt - Displays data overview (columns, shape, missing values, duplicates)
- Trains a Random Forest classifier with custom preprocessing steps
- Outputs accuracy and classification report
python RandomForestClassifier2.pyThis simplified version:
- Uses a hardcoded public Google Drive file URL (no authentication needed)
- Executes the same preprocessing and training pipeline
- Provides the same evaluation metrics
Both implementations follow this modular pipeline structure:
Raw Data (CSV from Google Drive)
↓
[NullChecker] → Handle missing values (fill with 0)
↓
[LabelEncoderTransformer] → Encode categorical variables
↓
[StandardScalerTransformer] → Normalize numeric features
↓
[DataSplitter] → Split into train/test sets (75/25)
↓
[ModelTrainer] → Train RandomForest/XGBoost classifier
↓
[ModelSaver] → Persist model predictions
↓
Evaluation Metrics (Accuracy, Classification Report)
- NullChecker: Fills null values with 0
- LabelEncoderTransformer: Converts categorical features to numeric labels
- StandardScalerTransformer: Standardizes numeric features to mean=0, std=1
- DataSplitter: Splits data into train/test sets (configurable ratio)
- ModelTrainer: Trains ensemble models with configurable hyperparameters
- ModelSaver: Persists model predictions to pickle files
The model outputs:
- Accuracy Score: Overall classification accuracy on the test set
- Classification Report: Precision, recall, and F1-score per class
- Saved Model: Predictions saved to
final_rf_model.pkl
Example output:
Accuracy: 0.92
Classification Report:
precision recall f1-score support
0 0.91 0.93 0.92 245
1 0.93 0.91 0.92 255
Edit pipeline parameters in the ModelTrainer initialization:
ModelTrainer(
n_estimators=50, # Number of trees in the forest
max_depth=10, # Maximum tree depth
min_samples_split=2, # Minimum samples to split a node
min_samples_leaf=1, # Minimum samples in leaf node
criterion='gini', # Split quality criterion
bootstrap=True # Use bootstrap samples
)- Both scripts target the fertilizer recommendation classification task (
target_feature='fertilizer_name') - The project is designed for educational purposes and demonstrates ML best practices
- Custom transformers are compatible with scikit-learn's
PipelineandGridSearchCV - TensorFlow is listed in requirements but not currently used; reserved for future deep learning implementations
Contributions welcome! Feel free to:
- Improve model performance
- Add additional preprocessing techniques
- Extend to other ML algorithms
- Enhance documentation
This project is open source and available for educational use.
Last Updated: November 2024