A complete, end-to-end Machine Learning project built in Python to predict used car prices based on features like brand, age, kilometers driven, fuel type, transmission, and owner history.
This project follows a clean production-grade directory structure, separating data preprocessing, model training, evaluation, inference, and a web-based user interface using Streamlit.
car_price_predictor/
│
├── data/
│ └── car_dataset.csv # Raw dataset
│
├── models/
│ ├── linear_regression.pkl # Saved Linear Regression model & preprocessor
│ ├── lasso.pkl # Saved Lasso model & preprocessor
│ └── random_forest.pkl # Saved Random Forest model & preprocessor
│
├── src/
│ ├── __init__.py # Makes src a package
│ ├── preprocess.py # Data loading, feature engineering, IQR outlier removal, OHE
│ ├── train.py # Pipeline fitting, scaling, and model training
│ ├── evaluate.py # Model testing, R2 score, and MAE comparison
│ └── predict.py # Command-line interactive inference script
│
├── app.py # Interactive web application (Streamlit)
└── README.md # Project documentation
-
Feature Engineering: Extracts car brands from model names, computes vehicle age dynamically relative to the current year, and applies a log transformation (np.log) to the selling price to handle skewed distributions.
-
Data Leakage Prevention: Isolates outlier removal (using the Interquartile Range / IQR method) strictly to the training set (km_driven), and fits the StandardScaler exclusively on training data before transforming the test data.
-
Column Alignment: Utilizes dynamic reindexing during inference to ensure consistency between runtime inputs and trained one-hot encoded feature spaces.
- Clone the repository / Navigate to the root directory:
cd car_price_predictor- Install the required dependencies: Ensure you have Python installed, then install the necessary packages:
pip install -r requirements.txt- Train the Models To train the models (Linear Regression, Lasso, and Random Forest), serialize them, and save them into the models/ folder, run:
python -m src.train- Evaluate Model Performance To compare all trained models against the test dataset using metrics like R-squared Score and Mean Absolute Error (MAE), run:
python -m src.evaluate- Run Command-Line Predictions To make a quick prediction via the terminal using an interactive model selection prompt, run:
python -m src.predict- Launch the Streamlit Web App To run the interactive web interface locally in your browser:
streamlit run app.py