Skip to content

Refactor introvert-extrovert prediction notebook with ML best practices#1

Open
rattan-dev with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-794a42e3-e313-4676-aadc-d628e0de190b
Open

Refactor introvert-extrovert prediction notebook with ML best practices#1
rattan-dev with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-794a42e3-e313-4676-aadc-d628e0de190b

Conversation

Copilot AI commented Aug 3, 2025

Copy link
Copy Markdown

Overview

This PR completely refactors the introvert-from-extrovert-prediction.ipynb notebook to follow modern machine learning best practices, improve code quality, and enhance maintainability. The original notebook had several issues including improper use of regression for classification, manual categorical encoding, and lack of proper evaluation metrics.

Key Changes

🏗️ Improved Notebook Structure

  • Added clear section headers for Introduction, Data Loading, EDA, Preprocessing, Feature Engineering, Model Training, Hyperparameter Tuning, Evaluation, and Conclusions
  • Comprehensive markdown explanations before each major code section
  • Professional documentation with rationale for each approach

🔧 Enhanced Data Preprocessing

  • Replaced manual binary mapping with sklearn's OneHotEncoder for categorical variables
  • Implemented robust missing value handling using appropriate strategies (median for numerical, most frequent for categorical)
  • Created consistent preprocessing pipeline using ColumnTransformer applied uniformly across train, validation, and test sets

⚙️ Feature Engineering

  • Added three new engineered features:
    • Social_Engagement_Ratio: Measures social activities vs alone time
    • Activity_Per_Friend: Captures activity relative to friend circle size
    • Potential_Introversion_Score: Combines behavioral indicators of introversion
  • Provided clear rationale and visualization for each new feature

🤖 Model Improvements

  • Fixed Core Issue: Replaced RandomForestRegressor with RandomForestClassifier for proper classification
  • Added hyperparameter tuning using RandomizedSearchCV with comprehensive parameter grid
  • Implemented 5-fold cross-validation for robust performance estimation
  • Generated feature importance analysis with detailed interpretation

📊 Comprehensive Evaluation

  • Replaced inappropriate MAE metric with proper classification metrics (accuracy, precision, recall, F1-score)
  • Added confusion matrix heatmap visualization
  • Implemented ROC curve analysis with AUC calculation
  • Created feature importance visualizations (bar charts and category breakdown)

🔄 Reproducibility & Code Quality

  • Set consistent random seeds (RANDOM_STATE = 42) throughout the pipeline
  • Removed redundant and outdated code sections
  • Applied modern pandas and sklearn best practices
  • Ensured all transformations are applied consistently across data splits

Results

The refactored notebook now provides:

  • 32 total cells (11 markdown, 21 code) with clear organization
  • Proper classification pipeline with appropriate metrics
  • Feature engineering insights showing which behavioral patterns are most predictive
  • Comprehensive evaluation with multiple visualization types
  • Reproducible results with consistent random seeding
  • Professional documentation suitable for production use

Technical Details

# Before: Manual categorical encoding
def turn_to_numeric(Dataframe, column, string):
    new = Dataframe[column] == string
    new += 0
    Dataframe[column] = new

# After: Proper sklearn pipeline
categorical_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent')),
    ('onehot', OneHotEncoder(handle_unknown='ignore', sparse_output=False))
])
# Before: Regression for classification
model = RandomForestRegressor(n_estimators=150, random_state=0)

# After: Proper classification with tuning
pipeline = Pipeline([
    ('preprocessor', ColumnTransformer([...])),
    ('classifier', RandomForestClassifier(random_state=RANDOM_STATE))
])
RandomizedSearchCV(pipeline, param_grid, cv=5, scoring='accuracy')

This refactoring transforms a basic analysis into a production-ready machine learning pipeline that follows industry best practices and provides meaningful insights into personality prediction.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: rattan-dev <182332870+rattan-dev@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor and enhance data preprocessing and structure for introvert/extrovert prediction notebook Refactor introvert-extrovert prediction notebook with ML best practices Aug 3, 2025
Copilot AI requested a review from rattan-dev August 3, 2025 03:48
@rattan-dev rattan-dev marked this pull request as ready for review October 25, 2025 16:06
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