Customer Data Classification with Pre-Trained RandomForestClassifier

  • Share this:

Code introduction


This function uses a pre-trained RandomForestClassifier to classify customer data. It takes a customer ID and a dictionary of customer data as input and returns the classification result of the customer.


Technology Stack : NumPy, Pandas, scikit-learn

Code Type : Classification function

Code Difficulty : Intermediate


                
                    
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier

def classify_customer_data(customer_id, customer_data):
    # The function takes a customer ID and a dictionary of customer data and returns a classification of the customer
    # based on a pre-trained RandomForestClassifier model.

    # Load the pre-trained model (assuming it's already trained and saved)
    model = RandomForestClassifier()
    model.load_model('customer_classification_model.pkl')

    # Prepare the data
    # Assuming customer_data is a dictionary where keys are feature names and values are feature values
    X = pd.DataFrame([customer_data])

    # Predict the customer type
    prediction = model.predict(X)

    return prediction[0]