[Python] multiprocessing hangs xgboost scoring

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to score multiple datasets at the same time using multiprocessing. The following code hangs on run, yet when I run score on the base_model outside of the pool it executes instantly.

    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split

    titanic = pd.read_csv('titanic.csv')
    titanic['Age'].fillna(titanic['Age'].mean(), inplace=True) #fill all missing values with the average
    #create indicator for cabin because there is areason it was missing
    titanic['Cabin_ind'] = np.where(titanic['Cabin'].isnull(), 0, 1)
    #convert sex to numeric
    gender_num = {'male': 0, 'female': 1}
    titanic['Sex'] = titanic['Sex'].map(gender_num)
    #drop uncessary variables
    titanic.drop(columns=['Cabin', 'Embarked', 'Name', 'Ticket'], inplace=True)
    features = titanic.drop(columns=['Survived'])
    labels = titanic['Survived']
    X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=.2, random_state=42)

    from xgboost import XGBClassifier

    base_model = XGBClassifier(objective='binary:logistic', nthread=4, seed=27, scoring='auc')
    base_model.fit(X_train, y_train)

    import multiprocessing as mp

    def my_func(X_test, y_test, base_model):
    val = base_model.score(X_test, y_test)
    print(val)

    def main():
    to_pass = [(X_test, y_test, base_model)]
    pool = mp.Pool(1)
    pool.starmap(my_func, to_pass)


    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página