Scala(非pyspark)将线性回归系数映射到特征名称(分类和连续)
我在scala中有一个数据帧,看起来像这样Scala(非pyspark)将线性回归系数映射到特征名称(分类和连续),scala,apache-spark,encoding,regression,categorical-data,Scala,Apache Spark,Encoding,Regression,Categorical Data,我在scala中有一个数据帧,看起来像这样 df.show +---+-----+-------------------+--------+------------------+--------+------+------------+-------------+ | id|group| normalized_amount|query_id| y| y1|group1|groupIndexed| groupEncoded| +---+-----+---
df.show
+---+-----+-------------------+--------+------------------+--------+------+------------+-------------+
| id|group| normalized_amount|query_id| y| y1|group1|groupIndexed| groupEncoded|
+---+-----+-------------------+--------+------------------+--------+------+------------+-------------+
| 1| B| 0.22874172014806| 1| 0.317739988492575| 0| B| 1.0|(2,[1],[1.0])|
| 2| A| -1.42432215217563| 2| -1.32008967486074| 0| C| 0.0|(2,[0],[1.0])|
| 3| B| -2.03644548423379| 3| -1.65740392834359| 0| B| 1.0|(2,[1],[1.0])|
| 4| B| 0.425753803902096| 4|-0.127591370989296| 0| C| 0.0|(2,[0],[1.0])|
| 5| A| 0.521050829955076| 5| 0.824285664580579| 1| A| 2.0| (2,[],[])|
| 6| A|-0.0416682439998418| 6| 0.321350404322885| 1| C| 0.0|(2,[0],[1.0])|
| 7| A| -1.2787327462978| 7| -0.88099379032367| 0| A| 2.0| (2,[],[])|
| 8| A| 0.431780409975322| 8| 0.575249966796747| 1| C| 0.0|(2,[0],[1.0])|
我正在对group1
(3个类别的分类变量)和标准化金额
(一个连续变量)进行y
的线性回归,如下所示
var assembler = new VectorAssembler().setInputCols(Array("groupEncoded", "normalized_amount")).setOutputCol("features")
val dfFeatures = assembler.transform(df)
var lr = new LinearRegression()
var lrModel = lr.fit(dfFeatures)
var lrPrediction = lrModel.transform(dfFeatures)
lmModel.intercept
lrModel.coefficients //model coefficient estimates (not intercept)
lrModel.summary.coefficientStandardErrors //standard error of intercept and coefficients, not sure in which order
我可以访问系数和标准误差,如下所示
var assembler = new VectorAssembler().setInputCols(Array("groupEncoded", "normalized_amount")).setOutputCol("features")
val dfFeatures = assembler.transform(df)
var lr = new LinearRegression()
var lrModel = lr.fit(dfFeatures)
var lrPrediction = lrModel.transform(dfFeatures)
lmModel.intercept
lrModel.coefficients //model coefficient estimates (not intercept)
lrModel.summary.coefficientStandardErrors //standard error of intercept and coefficients, not sure in which order
我的问题是
我已经看到了类似问题的一些答案,但它们都在pyspark中,而不是scala中,我只使用scala作为转换后的df的数据帧,其中包括预测和LogisticRegressionModel,您可以访问VectorAssembler字段的属性。这段代码来自,我稍微将其修改为LogisticRegressionModel,而不是Pipeline。请注意,您可以选择是否需要截距估计:
val lrToFit : LinearRegression = ???
lrToFit.setFitIntercept(false)
// With this dataframe as your transformed df that includes the prediction
val df: DataFrame = ???
val lr : LogisticRegressionModel = ???
val schema = df.schema
// Using the schema, the attributes of the Vector Assembler(features) can be extracted
val features = AttributeGroup.fromStructField(schema(lr.getFeaturesCol)).attributes.get.map(_.name.get)
val featureNames: Array[String] = if (lr.getFitIntercept) {
Array("(Intercept)") ++ features
} else {
features
}
val coefficients = lr.coefficients.toArray
val coeffs = if (lr.getFitIntercept) {
coefficients ++ Array(lr.intercept)
} else {
coefficients
}
featureNames.zip(coeffs).foreach { case (feature, coeff) =>
println(s"$feature\t$coeff")
}
如果加载预训练模型,则可以使用此方法,因为在这种情况下,您可能不知道VectorAssembler转换中特征的顺序。我认为您需要手动选择参考类别。我应该如何选择参考类别?