You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sklearn决策树训练报错:无法将字符串'male'转为浮点数求解决

修复决策树训练时的字符串转浮点错误

问题原因

训练数据X_train中存在字符串类型的分类特征(如示例中的'male'),而Scikit-learn的DecisionTreeClassifier仅支持数值型输入,因此触发ValueError: could not convert string to float错误。

修复方案

需要先对字符串特征做编码处理,将文本转换为模型可识别的数值。以下是两种常用方法及完整修改代码:

方法1:独热编码(适合无序分类特征)

独热编码会为每个分类值生成新的二进制列,避免给分类特征强加顺序关系,是处理无序分类的常用方式。

%%time
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
import matplotlib.pyplot as plt

print(cross_val_scores['DecisionTreeClassifier']['best_params'])

# 1. 预处理:对字符串特征做独热编码
# 筛选所有字符串类型的特征列
categorical_cols = X_train.select_dtypes(include=['object']).columns.tolist()
# 创建预处理管道:分类列编码,数值列保持不变
preprocessor = ColumnTransformer(
    transformers=[
        ('cat_encoder', OneHotEncoder(sparse_output=False, drop='first'), categorical_cols)
    ],
    remainder='passthrough'
)
# 转换训练集(仅在训练集上fit,避免数据泄露)
X_train_processed = preprocessor.fit_transform(X_train)

# 2. 初始化并训练模型
decision_tree = DecisionTreeClassifier(
    random_state=RAND_STATE,
    class_weight='balanced',
    max_depth=3
)
decision_tree.fit(X_train_processed, y_train)

# 3. 获取处理后的特征名称,用于绘制重要性
cat_feature_names = preprocessor.named_transformers_['cat_encoder'].get_feature_names_out(categorical_cols)
numeric_feature_names = X_train.select_dtypes(exclude=['object']).columns.tolist()
all_feature_names = list(cat_feature_names) + numeric_feature_names

# 4. 绘制特征重要性
feature_imp = decision_tree.feature_importances_
plt.barh(range(len(feature_imp)), feature_imp)
plt.title('DecisionTreeClassifier Feature Importance')
plt.yticks(range(len(all_feature_names)), all_feature_names)
plt.show()

方法2:标签编码(适合有序分类特征)

如果你的字符串特征是有序的(如'low'/'medium'/'high'),可以用标签编码将其映射为连续数值:

%%time
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt

print(cross_val_scores['DecisionTreeClassifier']['best_params'])

# 1. 预处理:对每个字符串列做标签编码
X_train_processed = X_train.copy()
categorical_cols = X_train_processed.select_dtypes(include=['object']).columns.tolist()
for col in categorical_cols:
    le = LabelEncoder()
    X_train_processed[col] = le.fit_transform(X_train_processed[col])

# 2. 初始化并训练模型
decision_tree = DecisionTreeClassifier(
    random_state=RAND_STATE,
    class_weight='balanced',
    max_depth=3
)
decision_tree.fit(X_train_processed, y_train)

# 3. 绘制特征重要性
feature_imp = decision_tree.feature_importances_
labels = list(X_train_processed.columns)
plt.barh(range(len(feature_imp)), feature_imp)
plt.title('DecisionTreeClassifier Feature Importance')
plt.yticks(range(len(labels)), labels)
plt.show()

注意事项

  • 预处理时仅在训练集上调用fit,测试集使用transform,防止数据泄露。
  • 独热编码会增加特征维度,若分类类别过多需注意维度爆炸问题。

内容的提问来源于stack exchange,提问作者Cycloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:15:51