Sklearn不会对连续数据进行回归



在使用连续标签的情况下,我的回归器不适合sklearn的问题。
下面是我的代码:

train_X = df_train_trimmed.loc[:, ~df_train_trimmed.columns.isin(['log_icu_days'])]
train_y = list(df_train_trimmed["log_icu_days"].astype(np.float32))
rfr = Pipeline(
[
("preprocessor", regressor_preprocessor),
(
"regressor",
RandomForestRegressor(n_estimators=1200, max_depth=30, criterion="squared_error", random_state=0, n_jobs=-1)
),
]
)
rfr.fit(
train_X, train_y
)

错误如下(减去很长的回调)

C:ProgramDataAnaconda3libsite-packagessklearnutilsmulticlass.py in unique_labels(*ys)
105     _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None)
106     if not _unique_labels:
--> 107         raise ValueError("Unknown label type: %s" % repr(ys))
108 
109     if is_array_api:
ValueError: Unknown label type: (array([ 2.19833517, -4.60517025, -4.60517025, ..., -4.60517025, -4.60517025, -4.60517025]),)

我看过其他问题,这主要出现在人们试图将分类器拟合到真实有价值的数据时,但在这里我使用回归器,所以我不确定为什么会发生这种情况。下面是我的train_y数据的一个小样本:

[2.1983350716202463,
-4.605170185988091,
-4.605170185988091,
0.009950330853168092,
0.009950330853168092,
-4.605170185988091,
-4.605170185988091,
...

无论我使用什么回归器,这个错误仍然存在。此外,这个结果度量是非负向量(icu停留天数)的对数变换,其中我添加了一个小值以使0值不是负无穷。我使用的每个回归器在非转换的非负整数结果向量上都工作得很好。

更新:
我试图将所有的y数据转换为整数,并且回归工作得很好,但是当我让它们是浮点数时,这个错误又出现了。

更新2:
我试着拟合一个没有预测因子的只有截距的模型。使用管道的模型有错误,而不使用管道的模型没有错误。这是数据管道的一个问题。作为参考,下面是我如何设置我的Pipeline对象:

categorical_features = [
"sex", "approach", "preop_pft"
]
categorical_transformer = Pipeline(
steps=[
("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
("selector", SelectPercentile(chi2, percentile=50)),
]
)
numeric_features = list(set(df_train_trimmed.columns) - set(categorical_features) - {'icu_days', 'log_icu_days'})
numeric_transformer = Pipeline(
steps=[("imputer", SimpleImputer(strategy="median")), ("scaler", StandardScaler())]
)

regressor_preprocessor = ColumnTransformer(
transformers=[

("num", numeric_transformer, numeric_features),
("cat", categorical_transformer, categorical_features),
],
remainder="passthrough"
)

更新3:
下面是我用pd.to_csv()导出的一些示例数据

, caseid anestart、aneend icu_days,年龄、性别、体重、bmi,亚撒,emop,方法,preop_htn, preop_dm, preop_pft, intraop_ebl, intraop_rbc log_icu_days3411、6246、-365、22735.0 9 8 F, 31.7, 15.4, 3.0, 0, 0, 0,正常,250.0,0,2.19833507162024633243, 5959, -1602, 4698.0, 0, 54岁,F, 52.6, 22.8, 2.0, 0, Videoscopic, 0, 1,正常,,0,-4.6051701859880911948, 3565, -640, 14900.0, 0, 27日,F, 51.8, 21.5, 2.0, 0,机器人,0,0,正常,50.0,0,-4.6051701859880912782、5118、-1144、11156.0、1、78米,59.4,21.5,3.0,0,0,0,0,0。0.0099503308531680921778、3258、-396、17904.0、1、73 F, 49.5, 23.7, 1,打开,0,0,正常、1200.0、3、0.009950330853168092982, 1802, -326, 13174.0, 0, 32岁的米,65.2,22.5,2.0,0,0,0,正常,300.0,0,-4.605170185988091

更新4:
移除"selector"从预处理器的分类部分,函数现在运行。我不知道为什么这导致了一个错误,虽然…

就像在这个答案中提到的,尝试使用float32类型的numpy nd.array来代替。

import numpy as np
train_X = df_train_trimmed.loc[:, ~df_train_trimmed.columns.isin(['log_icu_days'])]
train_y = np.array(df_train_trimmed["log_icu_days"]).astype(np.float32)
rfr = Pipeline(
[
("preprocessor", regressor_preprocessor),
(
"regressor",
RandomForestRegressor(n_estimators=1200, max_depth=30, criterion="squared_error", random_state=0, n_jobs=-1)
),
]
)
rfr.fit(train_X, train_y)