1
私は線形モデルのいくつかの機能を一般化することを目的としたScalaクラスを持っています - 具体的には、係数の配列と配列予測子を使用し、クラスはDataFrameからデータを取得し、単純な線形モデルを使用して、以下に示すようにDataFrame全体で予測を作成します。単一値の代わりに配列を返すscala/sparkマップ
私は予測値の列を生成すると期待している最後の行についています。私はいくつかのアプローチを試してきました(どれもがコメントアウトされています)。それが今、文句を言わない/型の不一致のC bは、コンパイルされたように、コード:
[error] found : Array[org.apache.spark.sql.Column]
[error] required: org.apache.spark.sql.Column
[error] .withColumn("prediction", colMod(preds.map(p => data(p))))
[error] ^
...私もpredを<で取得 - バージョンをpreds ...とforeachのバージョン:
[error] found : Unit
[error] required: org.apache.spark.sql.Column
[error] .withColumn("prediction", colMod(preds.foreach(data(_))))
[error] ^
問題を解決しようと無駄に努力していました...提案に感謝します。ここで
class LinearModel(coefficients: Array[Double],
predictors: Array[String],
data: DataFrame) {
val coefs = coefficients
val preds = Array.concat(Array("bias"), predictors)
require(coefs.length == preds.length)
/**
* predict: computes linear model predictions as the dot product of the coefficents and the
* values (X[i] in the model matrix)
* @param values: the values from a single row of the given variables from model matrix X
* @param coefs: array of coefficients to be applied to each of the variables in values
* (the first coef is assumed to be 1 for the bias/intercept term)
* @return: the predicted value
*/
private def predict(values: Array[Double], coefs: Array[Double]): Unit = {
(for ((c, v) <- coefs.zip(values)) yield c * v).sum
}
/**
* colMod (udf): passes the values for each relevant value to predict()
* @param values: an Array of the numerical values of each of the specified predictors for a
* given record
*/
private val colMod = udf((values: Array[Double]) => predict(values, coefs))
val dfPred = data
// create the column with the prediction
.withColumn("prediction", colMod(preds.map(p => data(p))))
//.withColumn("prediction", colMod(for (pred <- preds) yield data(pred)))
//.withColumn("prediction", colMod(preds.foreach(data(_))))
// prev line should = colMod(data(pred1), data(pred2), ..., data(predn))
}