В исходной документации приведен пример:
Код: Выделить всё
categorical=["sex", "smoker", "region", "children"]
# Default estimators are lgbm classifier and regressor
mf = MissForest(categorical=categorical)
mf.fit(x=train)
Код: Выделить всё
from missforest import MissForest
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
mf = MissForest(
clf=RandomForestClassifier(n_jobs=-1),
rgr=RandomForestRegressor(n_jobs=-1),
categorical=list(self.data.select_dtypes(exclude='number').columns)
)
self.data = mf.fit_transform(self.data)
Предупреждение пользователя: кодирование меток больше не выполняется по умолчанию. Пользователям придется самостоятельно кодировать категориальные функции.
Все мои попытки кодировать их самостоятельно, например:
Код: Выделить всё
from missforest import MissForest
from sklearn.preprocessing import OrdinalEncoder
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
# Categorical + numeric columns
cat_cols = list(self.data.select_dtypes(exclude='number').columns)
num_cols = list(self.data.select_dtypes(include='number').columns)
# Encode categorical columns safely
encoders = {}
for col in cat_cols:
enc = OrdinalEncoder()
self.data.loc[:, col] = enc.fit_transform(self.data[[col]].astype(str))
encoders[col] = enc
# Initialize MissForest
mf = MissForest(
clf=RandomForestClassifier(n_jobs=-1),
rgr=RandomForestRegressor(n_jobs=-1),
categorical=cat_cols
)
# Fit + transform
self.data = mf.fit_transform(self.data)
# Decode categorical columns
for col in cat_cols:
self.data.loc[:, col] = encoders[col].inverse_transform(self.data[[col]])
сначала попробуйте закодировать переменные, а затем используйте эту модифицированную версию Missforest
но они не указали, какой метод кодирования использовать, и обновленная версия Missforest по-прежнему не работает.>
Подробнее здесь: https://stackoverflow.com/questions/798 ... ding-by-th
Мобильная версия