Skip to content

Complete Random Forest and MLP regression analysis with metrics and visualizations#11

Draft
MiguelMonr with Copilot wants to merge 2 commits into
mainfrom
copilot/complete-random-forest-mlp
Draft

Complete Random Forest and MLP regression analysis with metrics and visualizations#11
MiguelMonr with Copilot wants to merge 2 commits into
mainfrom
copilot/complete-random-forest-mlp

Conversation

Copilot AI commented Dec 18, 2025

Copy link
Copy Markdown

The notebook contained only a basic RF stub (line ~1000) and incomplete MLP attempts. Added comprehensive implementations with full metrics, cross-validation, and comparative analysis.

Implementation

Random Forest Regressor

  • 500 estimators, 80/20 split, random_state=42
  • Metrics: R², MAE, RMSE, MAPE
  • 5-fold cross-validation
  • Feature importance extraction and visualization

Multi-Layer Perceptron

  • StandardScaler normalization
  • Architecture: (100, 50, 25) hidden layers
  • Adam optimizer with early stopping
  • Learning curve tracking

Comparative Analysis

  • Side-by-side metrics table
  • Actual vs predicted scatter plots (both models)
  • Residual plots and distributions
  • Performance breakdown by price range (<5K, 5K-10K, 10K-20K, 20K-50K, >50K)
  • Error analysis by vehicle age and power segments

Code Structure

Added 31 cells starting at cell 41 with complete workflow:

# Train RF with full metrics
rf_model = RandomForestRegressor(n_estimators=500, random_state=42, n_jobs=-1)
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)

# Calculate all metrics
rf_r2 = r2_score(y_test, y_pred_rf)
rf_mae = mean_absolute_error(y_test, y_pred_rf)
rf_rmse = np.sqrt(mean_squared_error(y_test, y_pred_rf))
rf_mape = mean_absolute_percentage_error(y_test, y_pred_rf)

# Cross-validation
cv_scores = cross_val_score(rf_model, X_train, y_train, cv=5, scoring='r2')

# Train MLP with scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

mlp_model = MLPRegressor(
    hidden_layer_sizes=(100, 50, 25),
    early_stopping=True,
    random_state=42
)
mlp_model.fit(X_train_scaled, y_train)

Includes 10+ visualizations: feature importance, prediction scatter plots, residual analysis, performance by price range, error distributions by vehicle characteristics, and learning curves.

Comprehensive conclusions section with model performance summary, strengths comparison, and deployment recommendations.

Original prompt

Objective

Complete the Notebooks/rf_and_mlp.ipynb notebook by adding:

  1. Complete Random Forest implementation with:

    • Train/test split configuration
    • Model training code
    • Full metrics: R², MAE, RMSE, MAPE
    • Feature importance analysis
    • Cross-validation results
  2. Multi-Layer Perceptron (MLP) implementation with:

    • Data scaling/normalization
    • Model architecture definition
    • Training with proper hyperparameters
    • Full metrics: R², MAE, RMSE, MAPE
    • Learning curve visualization
  3. Comparative analysis including:

    • Side-by-side metrics comparison table
    • Prediction vs actual scatter plots for both models
    • Residual analysis plots
    • Performance comparison by price range
    • Training time comparison
  4. Visualizations to add:

    • Feature importance bar chart (RF)
    • Actual vs Predicted scatter plots (both models)
    • Residual distribution histograms
    • Error distribution by vehicle characteristics

Current State

The notebook stops at line ~1000 with only a markdown cell stating "Variance of test data explained: 0.804" for Random Forest, but no actual implementation code for either model.

Expected Implementation Details

Random Forest:

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, mean_absolute_percentage_error

# Split and train
# Calculate all metrics
# Show feature importance
# Cross-validation with 5 folds

MLP:

from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler

# Scale features
# Define architecture (suggested: hidden_layers=(100, 50, 25))
# Train with appropriate parameters
# Calculate all metrics
# Show learning curves

Comparison:

  • Create DataFrame comparing metrics
  • Generate matplotlib/seaborn visualizations
  • Add interpretation markdown cells explaining which model performs better and why

Data Context

  • Dataset: autos_encoded with 37,866 records and 65 features (after one-hot encoding)
  • Target: price_EUR
  • Features: power_ps, odometer_km, age, vehicle types, brands, regions, etc.
  • Test size: Use 20% split (random_state=42 for reproducibility)

Success Criteria

  • ✅ Both RF and MLP models fully implemented
  • ✅ All metrics calculated and displayed clearly
  • ✅ At least 4 visualizations comparing model performance
  • ✅ Markdown cells with insights and conclusions
  • ✅ Code is well-commented and reproducible

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…tions

Co-authored-by: MiguelMonr <111590855+MiguelMonr@users.noreply.github.com>
Copilot AI changed the title [WIP] Complete Random Forest and MLP implementation in notebook Complete Random Forest and MLP regression analysis with metrics and visualizations Dec 18, 2025
Copilot AI requested a review from MiguelMonr December 18, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants