为什么 LogisticRegression 可以毫无错误地获取 object 类型的目标变量?

Why does `LogisticRegression` take target variable of type `object` without any error?

我正在对目标变量为 multiclass 的数据使用基本 LogisticRegression

我原以为 LogisticRegression 在调用 fit() 时会出现一些错误。但它没有。

LogisticRegression是否默认处理这种情况?如果是,对目标变量应用了哪些转换?

ddf = pd.DataFrame(
    [[1,2,3,4, "Blue"],
    [4,2,3,4, "Red"],
    [5,2,8,4, "Red"],
    [2,7,3,9, "Green"],
    [7,6,7,4, "Blue"]], columns=['A','B','C','D','E']
)
ddf
X = ddf[['A', 'B', 'C', 'D']]
y = ddf['E']
lr = LogisticRegression()
lr.fit(X, y)
preds = lr.predict(X)
print(preds)

给出输出:['Blue' 'Red' 'Red' 'Green' 'Blue']

Scikit-learn 默认能够处理所有 class 标识符的字符串标签,它在内部创建一个 LabelEncoder object, have a look at the code here。 String-class 标签被编码为整数值。