---
title: Logistic Regression
description: "LogisticRegression为ML classification API。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2200/bds/kunpengbdssparkml_16_0014.html
sourcePath: /source/zh/kunpengboostkithistory/2200/bds/kunpengbdssparkml_16_0014.html
indexId: 31f41f6fe9f787f64e409b7c3aefdf60ed303bca03094f0e54275337e08f365973
---
# Logistic Regression

LogisticRegression为ML classification API。

| 模型接口类别 | 函数接口 |
| --- | --- |
| ML classification API | def fit(dataset: Dataset[\_]):LogisticRegressionModel |
| ML classification API | def fit(dataset: Dataset[\_], paramMap: ParamMap): LogisticRegressionModel |
| ML classification API | def fit(dataset: Dataset[\_], firstParamPair: ParamPair[\_], otherParamPairs: ParamPair[\_]\*):LogisticRegressionModel |
| ML classification API | def fit(dataset: Dataset[\_], paramMaps: Array[ParamMap]): Seq[LogisticRegressionModel] |


#### ML classification API

- 功能描述
  传入Dataset格式的样本数据，调用fit接口，输出Logistic Regression模型。


- 输入输出

  1. 包名：package org.apache.spark.ml.classification
  2. 类名：LogisticRegression
  3. 方法名：fit
  4. 输入：Dataset[_]，训练样本数据，必须字段如下| 参数名称 | 取值类型 | 缺省值 | 描述 |
| --- | --- | --- | --- |
| labelCol | Double | label | Label，require： label == label.toInt label >= 0 |
| featuresCol | Vector | features | 特征标签 |


  5. 基于原生算法优化的参数

```
def setRegParam(value: Double): LogisticRegression.this.type
def setElasticNetParam(value: Double): LogisticRegression.this.type
def setMaxIter(value: Int): LogisticRegression.this.type
def setTol(value: Double): LogisticRegression.this.type
def setFitIntercept(value: Boolean): LogisticRegression.this.type
def setFamily(value: String): LogisticRegression.this.type
def setStandardization(value: Boolean): LogisticRegression.this.type
override def setThreshold(value: Double): LogisticRegression.this.type
def setWeightCol(value: String): LogisticRegression.this.type
override def setThresholds(value: Array[Double]): LogisticRegression.this.type
def setAggregationDepth(value: Int): LogisticRegression.this.type
def setLowerBoundsOnCoefficients(value: Matrix): LogisticRegression.this.type
def setUpperBoundsOnCoefficients(value: Matrix): LogisticRegression.this.type
def setLowerBoundsOnIntercepts(value: Vector): LogisticRegression.this.type
def setUpperBoundsOnIntercepts(value: Vector): LogisticRegression.this.type
```


    参数及fit代码接口示例： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import org.apache.spark.ml.param.{ParamMap, ParamPair} val logR = new LogisticRegression() //定义def fit(dataset: Dataset[_], paramMap: ParamMap) 接口参数 val paramMap = ParamMap(logR.maxIter -> maxIter) .put(logR.regParam, regParam) // 定义def fit(dataset: Dataset[_], paramMaps: Array[ParamMap]): 接口参数 val paramMaps: Array[ParamMap] = new Array[ParamMap](2) for (i <- 0 to 2) { paramMaps(i) = ParamMap(logR.maxIter -> maxIter) .put(logR.regParam, regParam) }//对paramMaps进行赋值 // 定义def fit(dataset: Dataset[_], firstParamPair: ParamPair[_], otherParamPairs: ParamPair[_]*) 接口参数 val regParamPair = ParamPair(logR.regParam, regParam) val maxIterParamPair = ParamPair(logR.maxIter, maxIter) val tolParamPair = ParamPair(logR.tol, tol) // 调用各个fit接口 model = logR.fit(trainingData) model = logR.fit(trainingData, paramMap) models = logR.fit(trainingData, paramMaps) model = logR.fit(trainingData, regParamPair, maxIterParamPair, tolParamPair)

  6. 输出：LogisticRegressionModel，模型预测时的输出字段如下| 参数名称 | 取值类型 | 缺省值 | 描述 |
| --- | --- | --- | --- |
| predictionCol | Double | prediction | Predicted Label |


- 使用样例
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import org.apache.spark.ml.classification.LogisticRegression // Load training data val training = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt") val lr = new LogisticRegression() .setMaxIter(10) .setRegParam(0.3) .setElasticNetParam(0.8) // Fit the model val lrModel = lr.fit(training) // Print the coefficients and intercept for logistic regression println(s"Coefficients: ${lrModel.coefficients} Intercept: ${lrModel.intercept}") // We can also use the multinomial family for binary classification val mlr = new LogisticRegression() .setMaxIter(10) .setRegParam(0.3) .setElasticNetParam(0.8) .setFamily("multinomial") val mlrModel = mlr.fit(training)
