Team Ai
Apppublic

nhvrm1998/Explainable-AI-Income-Prediction

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
model_utils.py40 linesDownload Raw Back to root
1import joblib2import pandas as pd3 4# Load saved objects5model = joblib.load("random_forest.pkl")6feature_columns = joblib.load("feature_columns.pkl")7label_encoder = joblib.load("label_encoder.pkl")8 9 10def preprocess_input(user_input):11    """12    Convert dictionary input into the same format used during training.13    """14 15    df = pd.DataFrame([user_input])16 17    # One-hot encode categorical columns18    df = pd.get_dummies(df)19 20    # Match training feature columns21    df = df.reindex(columns=feature_columns, fill_value=0)22 23    return df24 25 26def predict_income(user_input):27    """28    Predict income class and confidence.29    """30 31    processed = preprocess_input(user_input)32 33    prediction = model.predict(processed)[0]34    probability = model.predict_proba(processed)[0]35 36    confidence = round(probability.max() * 100, 2)37 38    label = label_encoder.inverse_transform([prediction])[0]39 40    return label, confidence