MLlib Estimator 如何知道特征和目标列是什么?

How does an MLlib Estimator know what are the features and target columns?

我试着遵循这个MLlib tutorial。我了解估算器的概念。它以数据帧作为输入并使用它来训练 return 预测模型,它是 MLlib 术语中的 Transformer(将数据帧作为输入并 return 另一个数据的东西-frame).

我不清楚的是 Estimator 如何知道数据框的哪些列应该被视为特征以及哪些列应该被视为目标。

我们来看这个例子:

from pyspark.ml.classification import LogisticRegression

# Prepare training data from a list of (label, features) tuples.
training = spark.createDataFrame([
    (1.0, Vectors.dense([0.0, 1.1, 0.1])),
    (0.0, Vectors.dense([2.0, 1.0, -1.0])),
    (0.0, Vectors.dense([2.0, 1.3, 1.0])),
    (1.0, Vectors.dense([0.0, 1.2, -0.5]))], ["label", "features"])

# Create a LogisticRegression instance. This instance is an Estimator.
lr = LogisticRegression(maxIter=10, regParam=0.01)
# Print out the parameters, documentation, and any default values.
print("LogisticRegression parameters:\n" + lr.explainParams() + "\n")

# Learn a LogisticRegression model. This uses the parameters stored in lr.
model1 = lr.fit(training)

我猜该模型将 features 列作为特征并将 label 列视为目标。这是正确的吗?如果是这样,是否可以更改此默认行为?

LogisticRegressionlabelCol and featuresCol Params,默认情况下是 lablelfeatures。两者都可以在构造函数中设置

LogisticRegression(maxIter=10, regParam=0.01, labelCol="foo", featuresCol="bar")

或使用相应的setter方法:

lr.setLabelCol("foo").setFeaturesCol("bar")

大多数 ml 分类和预测算法都使用相同的 Params