【问题标题】:How to determine label and features in logistic regression spark?如何确定逻辑回归火花中的标签和特征?
【发布时间】:2017-09-24 22:48:25
【问题描述】:

我正在使用 spark mlib,并使用逻辑回归模型进行分类。我点击了这个链接: https://spark.apache.org/docs/2.1.0/ml-classification-regression.html#logistic-regression

 import org.apache.spark.ml.classification.LogisticRegression;
import org.apache.spark.ml.classification.LogisticRegressionModel;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

// Load training data
Dataset<Row> training = spark.read().format("libsvm")
  .load("data/mllib/sample_libsvm_data.txt");

LogisticRegression lr = new LogisticRegression()
  .setMaxIter(10)
  .setRegParam(0.3)
  .setElasticNetParam(0.8);

// Fit the model
LogisticRegressionModel lrModel = lr.fit(training);

// Print the coefficients and intercept for logistic regression
System.out.println("Coefficients: "
  + lrModel.coefficients() + " Intercept: " + lrModel.intercept());

// We can also use the multinomial family for binary classification
LogisticRegression mlr = new LogisticRegression()
        .setMaxIter(10)
        .setRegParam(0.3)
        .setElasticNetParam(0.8)
        .setFamily("multinomial");

// Fit the model
LogisticRegressionModel mlrModel = mlr.fit(training);

如果我将 .csv 作为输入,我不确定此模型如何识别标签和特征?谁能解释一下?

【问题讨论】:

    标签: apache-spark machine-learning


    【解决方案1】:

    因为你从at数据加载libsvm,它由标签index1:value1 index2:value2...... 如果你使用.csv,你必须明确指定参数。

    【讨论】:

    • 感谢您的回复。因此,如果输入是 libsvm,它将第一列作为标签权并保留为特征?如果输入文件是.csv,我们如何设置标签和特征
    • 训练 = spark.read().format("csv").load("datapath");或培训 = spark.read().csv("datapath");
    • 谢谢,但我在哪里设置标签和给定行的功能?
    • setLabelCol("label") setPredictionCol("prediction")
    • 当我将字符串类型作为setLabelCol的输入时,它会给出IllegalArgumentException,所以我们是否总是需要使用Stringindexer对其进行转换然后将其设置为标签?
    【解决方案2】:

    最后我能够修复它,我需要使用 VectorAssemblerStringIndexer 转换器,并且我有 setInputColsetOutputCol 方法,它提供了设置标签和功能的方法。

    VectorAssembler assembler = new VectorAssembler()
                              .setInputCols(new String[]{"Lead ID"})
                              .setOutputCol("features");
    
    sparkSession.read().option("header", true).option("inferSchema","true").csv("Book.csv");    
            dataset = new StringIndexer().setInputCol("Status").setOutputCol("label").fit(dataset).transform(dataset);
    

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 1970-01-01
      • 2016-05-15
      • 2016-05-17
      • 2021-06-10
      • 2014-08-06
      • 2018-02-27
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多