This project aims to develop a machine learning model that predicts diamond prices with both high accuracy and clear interpretability. Using a rich dataset of 53,940 entries, the analysis cleans and explores the data, examining relationships between the target variable price and the diamond attribute predictors, and comparing linear and tree-based models.
We do not only focus on predictive performance but also aim to understand model behaviour. We observe how correlated size-related features affect linear model coeffcients, whether feature reduction helps. and how prediction errors behave on both log-transformed and original price scales.
The final model is an Optuna-tuned XGBoost regressor, selected using validation performance and evaluated once on a held-out test set.
price target.cut, color and clarity, fitting the encoder on the training data.Data is comprised of 53,940 observations and 10 features, with price as the target variable.
Key features include: carat, cut, color, clarity, depth, table, x, y, z.
x, y, z we renamed as length, width, height for improved readability.
Unnamed: 0 row-identifier column before checking for duplicates.The EDA examines the distributions of numerical features, including skewness and kurtosis; price relationships with categorical and numerical features; outliers; and correlations among predictors. ANOVA and Cramér’s V are used to investigate categorical feature relationships.
The analysis shows that diamond price is strongly related to carat weight and other size-related measurements. Its also highlights wha average prieces across quality grades need careful interpretation. Diamonds in different grades can have different carat weight distributions.
The dataset is split into training(64%), validation(16%) and test(20%) subsets. The validation set is used to compare models and tune hyperparameters. The test set is held back for final evaluation.
Preprocessing includes:
np.lop1pto skewed numerical features and to price.StandardScaler to predictor features.Models are trained to predict log1p(price). For metrics in dollars, predictions are transformed back to the original price scale.
The notebook compares several approaches:
The linear model experiments reveal strong correlations among size-related features can make individual(independent) coefficients difficult to interpret. Adding interaction terms improves on the baseline linear model, while tree-based models better capture non-linear relationships and feature interactions.
The notebook examines XGBoost’s built-in feature importance and SHAP values. These are used to explore how the model relies on the predictors and how feature values contribute to predictions.
Several reduced feature experiments test whether a smaller model can retain performance:
length, width and height produces a small improvement over the original XGBoost model, but does not outperform the tuned model.cut and table degrades validation performance.These experiments support retaining all eight predictors in the final model. Feature importance and SHAP describe model behaviour; they do not establish that a feature has an independent causal effect.
Optuna is used to tune XGBoost hyperparameters, with validation RMSE on the original dollar scale as the optimization objection objective. The tuned model improves validation RMSE from $552.97 for the original XGBoost model to $532.46, an improvement of approximately 3.7%.
Residuals are examined on both the log-transformed and original price scales. On the original scale, absolute errors tend to increase for more expensive diamonds, meaning predictions are less precise in dollar terms at the upper end of the price range.
The Optimized XGBoost Regressor using all eight predictors, was selected based on validation performance and evaluated on the held-out test set.
| Metric | Train | Validation | Test |
|---|---|---|---|
| RMSE | $413.29 | $532.46 | $522.62 |
| RMSE (% of mean price) | 10.53% | 13.35% | 13.39% |
| MAE | $212.60 | $264.45 | $262.04 |
| MedAE | $80.45 | $94.45 | $95.31 |
| R² (log-transformed target) | 0.995 | 0.993 | 0.992 |
Test performance is close to validation performance, which is encouraging for this data split. It is not a guarantee that the same performance will hold on other samples or real-world diamond listings.
cut, color and clarity could be compared.length-to-width ratio, could be investigated.Diamond_Price_Prediction.ipynb - the notebook containing data cleaning, EDA, preprocessing, model comparison, feature interpretation, tuning and final evaluation.requirements.txt - Python package dependencies, if included in the repository.Clone the repository and open the notebook in a Python environment with Jupyter installed:
# Clone the repository
git clone https://github.com/mcadriaans/ml_diamond-price-prediction.git
# Navigate into the project directory
cd ml_diamond-price-prediction
# Launch the notebook interface
pip install requirements.txt
jupyter notebook