diamonds / README.md
lipaz's picture
Update README.md
c4ea897 verified
|
Raw
History Blame Contribute Delete
12.6 kB
Project Walkthrough Video [Click here to watch the video](https://youtu.be/EJxIwzKkG3g)
# 0. Project Overview & Initial Workflow
## **0.1 Project Goal**
The main objective of this project was to analyze the **diamonds dataset** and understand:
- What drives the **price** of a diamond,
- How **physical attributes** (carat, x/y/z dimensions) behave,
- How **categorical qualities** (cut, color, clarity) influence pricing,
- And which features are the most important for predictive modeling.
This includes **EDA, feature engineering, dimensionality reduction, clustering, and model interpretation**.
---
## **0.2 Initial Setup & Data Loading**
Before analysis, we:
1. Loaded the dataset (`diamonds.csv`)
2. Inspected its structure
3. Checked for missing values
4. Validated data types
5. Identified potential data issues such as:
- Zero or unrealistic values (especially in x, y, z)
- Extreme outliers in price and carat
---
## **0.3 Cleaning & Preprocessing Steps**
We performed the following:
- Removed or corrected invalid dimension values (e.g., x/y/z = 0)
- Detected extreme outliers using boxplots
- Standardized numeric distributions when needed
- Encoded categorical variables
- Created engineered features:
- `volume = x * y * z`
- `price_per_carat = price / carat`
- PCA components (`pca1`, `pca2`)
- Ratios (xy_ratio, yz_ratio, xz_ratio)
---
## **0.4 Core Analytical Questions**
Throughout the project, we focused on several central questions:
### **1. Distribution & Quality of the Data**
- What do the numeric features look like?
- Are there extreme outliers or incorrect values?
### **2. Relationship Between Size & Price**
- How strongly does carat determine price?
- Do heavier diamonds increase in price linearly or exponentially?
### **3. Influence of Categorical Features**
- How do cut, color, and clarity affect pricing?
- Are some categories overpriced or underpriced?
### **4. Multicollinearity & Feature Dependencies**
- Are x, y, z essentially duplicating carat?
- What is the correlation structure?
### **5. Machine Learning Insights**
- Which features matter most for predict
# Diamonds Dataset — Full EDA, Visualizations & Feature Analysis
This project explores the **diamonds dataset** using a full workflow:
- Exploratory Data Analysis (EDA)
- Outlier detection
- Distribution analysis
- Relationship analysis (carat–price, dimensions–price)
- Correlation heatmaps
- Clustering (PCA + KMeans)
- Feature importance (Linear Regression + Random Forest)
All generated graphics are stored and displayed inside this repository.
---
# 1. Exploratory Data Analysis (EDA)
## 1.1 Boxplots — Outlier Detection
![Boxplots Outliers](boxplots_outliers.png.png)
### **What we looked for**
- Detect extreme outliers in: **carat, depth, table, x, y, z, price**
- Identify values that may distort models
### **What we found**
- Very strong outliers in **price** (long right tail)
- Outliers in **carat** and the 3D dimensions **x/y/z**
- depth & table are stable with minimal extreme values
---
## 1.2 Boxplots — Numeric Feature Comparison
![Boxplots Numeric Features](boxplots_numeric_features.png.png)
### **What we looked for**
- Check consistency between numeric fields
- Identify skewness and unusual patterns
### **What we found**
- Most numeric columns are **right-skewed**
- carat, price, x/y/z show wide variance
- Indicates the need for normalization before modeling
---
## 1.3 Distribution Plots — Numeric Features
![Distributions Numeric](distributions_numeric_features.png.png)
### **What we looked for**
- Understand the shape of each numeric feature
- Detect multimodality or measurement clusters
### **What we found**
- price = heavily right-skewed
- carat = concentrated around 0.3–1.5
- x/y/z = show clear clustered peaks → indicates standard diamond sizes in the market
---
## 1.4 Distribution (Histograms + KDE)
![Distribution Histograms](distribution_histograms.png.png)
### **What we looked for**
- Compare distribution curves to understand typical ranges
- Check for measurement errors
### **What we found**
- x/y/z show repeating peaks → indicates standard diamond cut proportions
- price again shows extreme right-tail
- No obvious errors except a few odd large values
---
# 2. Relationship Exploration
## 2.1 Carat vs Price — Colored by Cut
![Carat by Cut](carat_vs_price_colored_by_cut.png.png)
### **What we looked for**
- Understand how **carat weight** drives pricing
- Check if **cut quality** influences pricing
### **What we found**
- Strong positive correlation: **higher carat → higher price**
- High-cut diamonds (Ideal, Premium) form slightly higher price clusters
- Clear size bands (0.5, 1.0, 1.5 carat) visible
---
## 2.2 Carat vs Price — Hexbin Density
![Hexbin Carat Price](carat_vs_price_hexbin.png.png)
### **What we looked for**
- Density concentration of the dataset
- Identify the most common market prices
### **What we found**
- Dense cluster around **0.2–1.2 carat** and **$500–$6000**
- Very few large stones (3–5 carat)
---
## 2.3 ### Average Price by Cut, Color, and Clarity
![Overlay Density](
![avg_price_by_cut_color_clarity](https://cdn-uploads.huggingface.co/production/uploads/6911ea1a4cdaf097dd0456d9/pzu7dPTsyqI1oBl6XoDbs.png)
Key insights:
- **Cut:** Premium diamonds have the highest average price, while Ideal tends to be lower.
- **Color:** Colors I and J show higher average prices compared to others.
- **Clarity:** SI2 appears with the highest average price, likely influenced by larger carat values within this group.
---
## 2.4 Diamonds Dimensions vs Price
![Diamonds Size vs Price](diamonds_size_vs_price.png.png)
### **What we looked for**
- Relationship between x, y, z dimensions and price
### **What we found**
- Strong increasing trend for all dimensions
- x and y show the clearest linear relationship
- Some unrealistic dimension values detected → outliers
---
# 3. Statistical Relationships
## 3.1 Correlation Matrix (Numeric)
![Correlation Matrix](correlation_matrix_numeric.png.png)
### **What we looked for**
- Identify multicollinearity
- See which features are most correlated with price
### **What we found**
- carat has the strongest correlation with price (0.92)
- x, y, z strongly correlated with each other (>0.98)
- depth & table weakly correlated
---
## 3.2 Heatmap — Key Numeric Features
![Heatmap](heatmap_numeric_features.png.png)
### **What we found**
- Confirms carat is the #1 price driver
- Dimensions act as proxies for carat
- Confirms multicollinearity → PCA or feature reduction required
---
# 4. Advanced Visualizations
## 4.1 KMeans Clusters (PCA Visualization)
![KMeans PCA](kmeans_clusters_pca_visualization.png.png)
### **What we looked for**
- Whether diamonds naturally form clusters
- If PCA can separate quality groups
### **What we found**
- The dataset forms clear clusters after PCA
- Cluster 0 is dominant (majority of market)
- Smaller clusters show unique structural patterns
---
## 4.2 Pairplot — Key Features
![Pairplot](pairplot_key_features.png.png)
### **What we looked for**
- Examine all pairwise relationships visually
### **What we found**
- Strong linear relationships among x, y, z
- Clear nonlinear trend for carat–price
- Strong clustering by typical diamond sizes
---
# 5. Feature Importance
## 5.1 Linear Regression — Coefficients
![LR Feature Importance](feature_importance_linear_regression.png.png)
### **What we looked for**
- Identify which features increase or decrease price
### **What we found**
- carat has the highest positive effect
- Higher clarity grades reduce price impact (negative coefficients for lower clarity)
- color J decreases price significantly
---
## 5.2 Random Forest — Top 20 Features
![RF Importance](rf_feature_importance.png.png)
### **What we looked for**
- Nonlinear feature importance ranking
- Impact of engineered features
### **What we found**
- **price_per_carat** = most powerful feature
- volume & PCA components ranked very high
- confirms importance of dimensional/size attributes
- categorical dummy variables are much weaker
## Regression Model Comparison
We evaluated three regression models:
- **Linear Regression** (baseline)
- **Random Forest Regressor**
- **Gradient Boosting Regressor**
### Results
| Model | MAE | RMSE | R² |
|--------------------|----------|----------|-----------|
| Linear Regression | 0.0840 | 0.1248 | 0.9841 |
| Random Forest | **0.0042** | **0.0194** | **0.9996** |
| Gradient Boosting | 0.0250 | 0.0442 | 0.9980 |
### Summary
- **Random Forest** achieved the best performance: lowest errors and highest R².
- **Gradient Boosting** also performed very well but was slightly weaker.
- **Linear Regression** performed significantly worse, showing the price relationships are **non-linear**.
### Conclusion
**Random Forest is the best regression model** and was selected as the final model for this part.
Part 7: Regression → Classification
### Creating Price Classes (Quantile Binning)
To convert the continuous *price* variable into a classification target,
we split the data into **three equal-sized groups (33% each)** using quantiles.
- **Class 0** → bottom 33% (cheapest diamonds)
- **Class 1** → middle 33%
- **Class 2** → top 33% (most expensive diamonds)
This ensures balanced class sizes and prevents bias toward any group.
### Class Balance Check
After creating the three price classes, we checked whether the classes were balanced.
**Training Set:**
Each class contains ~33% of the samples
**Test Set:**
Same distribution — ~33% per class
This confirms the split is balanced and suitable for training classification models.
## 8.1 Conceptual Questions
### What is more important in our task — Precision or Recall?
In our diamond price classification task, **precision is slightly more important**.
We want to avoid labeling a low-value diamond as an expensive one (false positives).
This is especially relevant because misclassifying prices upward is more harmful than misclassifying downward.
### What is more critical — False Positive or False Negative?
**False Positives are more critical.**
A False Positive here means:
The model predicts “expensive” when the diamond actually belongs to a cheaper class.
This type of error is more problematic in real-world scenarios (e.g., pricing, valuation, inventory management),
because it **overestimates** the diamond’s value and can lead to financial loss.
## Part 8.3 — Classification Models Evaluation
After converting the regression problem into a 3-class classification task,
we trained and evaluated three models:
- Logistic Regression
- Random Forest
- Gradient Boosting
### What we evaluated
For each model we generated:
- **Classification Report** (precision, recall, f1-score, support)
- **Confusion Matrix** (to understand where the model makes mistakes)
---
## Results Summary
All three models performed extremely well, with **Gradient Boosting** achieving the best scores:
| Model | Accuracy | F1-macro |
|--------------------|----------|----------|
| Logistic Regression | 0.9866 | 0.9866 |
| Random Forest | 0.9926 | 0.9926 |
| Gradient Boosting | 0.9933 | 0.9933 |
### Key Insights
- All models classify the three price classes with **very high precision and recall**.
- **Gradient Boosting** makes the fewest mistakes across classes.
- Misclassifications mostly occur between **adjacent price classes** (e.g., class 1 ↔ class 2), which is expected because diamond prices are continuous.
- No model suffers from class imbalance problems.
---
## Confusion Matrices & Reports
### Logistic Regression
(
![image](https://cdn-uploads.huggingface.co/production/uploads/6911ea1a4cdaf097dd0456d9/6TfUlqSiD7rd4XKVFghRQ.png)
)
### Random Forest
(
![image](https://cdn-uploads.huggingface.co/production/uploads/6911ea1a4cdaf097dd0456d9/JGM6eXa09Tz1NlE0ydVOV.png)
)
### Gradient Boosting
(
![image](https://cdn-uploads.huggingface.co/production/uploads/6911ea1a4cdaf097dd0456d9/Q0Svg6_AlVwLYlZEkeFvQ.png)
)
---
## Winner
**Gradient Boosting** is the best performing model
→ Highest Accuracy
→ Highest F1-macro
→ Most stable confusion matrix
This is the model selected for export and deployment.