Regression - RDD-based API
アイソトニック回帰
アイソトニック回帰 は回帰アルゴリズムの一族に所属します。Formally isotonic regression is a problem where given a finite set of real numbers $Y = {y_1, y_2, ..., y_n}$
representing observed responses and $X = {x_1, x_2, ..., x_n}$
the unknown response values to be fitted finding a function that minimises
\begin{equation}
f(x) = \sum_{i=1}^n w_i (y_i - x_i)^2
\end{equation}
with respect to complete order subject to $x_1\le x_2\le ...\le x_n$
where $w_i$
are positive weights. 結果の関数はアイソトニック回帰と呼ばれ、ユニークです。それは順番制限下にある最小二乗問題と見ることができます。本質的にアイソトニック回帰は元のデータポイントに最も合致する単調関数です。
spark.mllib
は 並行アイソトニック回帰へのやり方を使用するpool adjacent violators アルゴリズムです。訓練入力は3つのdouble値のタプルのRDDで、この順にラベル、特徴、および重み付けを現します。更にアイソトニック回帰アルゴリズムはデフォルトがtrueの $isotonic$ と呼ばれる任意のパラメータを持ちます。この引数はアイソトニック回帰がアイソトニック(単調増加)か、アンチトニック(単調減少)かを指定します。
訓練は既知あるいは未知の特徴の両方のためのラベルを予想するために使うことができるIsotonicRegressionModelを返します。アイソトニック回帰の結果は区分的に線形な関数として扱われます。従って予想のルールは以下のようになります:
- 予想入力が厳密に訓練特徴と一致する場合は関連する予想が返されます。同じ特徴を持つ複数の予想がある場合は、それらのうちの一つが返されます。 どちらかの一つが未定義です(java.util.Arrays.binarySearchと同じです)。
- 予想入力が全ての訓練特徴より低いあるいは高い場合は、一番低いあるいは高い特徴がそれぞれ返されます。 同じ特徴を持つ複数の予想がある場合は、一番低いあるいは高いものがそれぞれ返されます。
- 予想入力が2つの訓練特徴の間にある場合は、予想は部分的に線形関数として扱われ、補間値が二つの近接する特徴の予想から計算されます。同じ特徴を持つ複数の値がある場合は、以前のポイントと同じルールが使われます。
例
各行が ラベル,特徴 の形式を持つファイルからデータを読み込みます。例えば、4710.28,500.00。データは訓練およびテストのために分割されます。モデルは訓練セットを使って生成され、平均二乗エラーは予想されたラベルとテストセットの中の実際のラベルを使って計算されます。
APIの詳細はIsotonicRegression
Scala ドキュメント および IsotonicRegressionModel
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.regression.{IsotonicRegression, IsotonicRegressionModel}
import org.apache.spark.mllib.util.MLUtils
val data = MLUtils.loadLibSVMFile(sc,
"data/mllib/sample_isotonic_regression_libsvm_data.txt").cache()
// Create label, feature, weight tuples from input data with weight set to default value 1.0.
val parsedData = data.map { labeledPoint =>
(labeledPoint.label, labeledPoint.features(0), 1.0)
}
// Split data into training (60%) and test (40%) sets.
val splits = parsedData.randomSplit(Array(0.6, 0.4), seed = 11L)
val training = splits(0)
val test = splits(1)
// Create isotonic regression model from training data.
// Isotonic parameter defaults to true so it is only shown for demonstration
val model = new IsotonicRegression().setIsotonic(true).run(training)
// Create tuples of predicted and real labels.
val predictionAndLabel = test.map { point =>
val predictedLabel = model.predict(point._2)
(predictedLabel, point._1)
}
// Calculate mean squared error between predicted and real labels.
val meanSquaredError = predictionAndLabel.map { case (p, l) => math.pow((p - l), 2) }.mean()
println("Mean Squared Error = " + meanSquaredError)
// Save and load model
model.save(sc, "target/tmp/myIsotonicRegressionModel")
val sameModel = IsotonicRegressionModel.load(sc, "target/tmp/myIsotonicRegressionModel")
各行が ラベル,特徴 の形式を持つファイルからデータを読み込みます。例えば、4710.28,500.00。データは訓練およびテストのために分割されます。モデルは訓練セットを使って生成され、平均二乗エラーは予想されたラベルとテストセットの中の実際のラベルを使って計算されます。
APIの詳細はIsotonicRegression
Java ドキュメント および IsotonicRegressionModel
Java ドキュメント を参照してください。
import scala.Tuple2;
import scala.Tuple3;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.regression.IsotonicRegression;
import org.apache.spark.mllib.regression.IsotonicRegressionModel;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.util.MLUtils;
JavaRDD<LabeledPoint> data = MLUtils.loadLibSVMFile(
jsc.sc(), "data/mllib/sample_isotonic_regression_libsvm_data.txt").toJavaRDD();
// Create label, feature, weight tuples from input data with weight set to default value 1.0.
JavaRDD<Tuple3<Double, Double, Double>> parsedData = data.map(point ->
new Tuple3<>(point.label(), point.features().apply(0), 1.0));
// Split data into training (60%) and test (40%) sets.
JavaRDD<Tuple3<Double, Double, Double>>[] splits =
parsedData.randomSplit(new double[]{0.6, 0.4}, 11L);
JavaRDD<Tuple3<Double, Double, Double>> training = splits[0];
JavaRDD<Tuple3<Double, Double, Double>> test = splits[1];
// Create isotonic regression model from training data.
// Isotonic parameter defaults to true so it is only shown for demonstration
IsotonicRegressionModel model = new IsotonicRegression().setIsotonic(true).run(training);
// Create tuples of predicted and real labels.
JavaPairRDD<Double, Double> predictionAndLabel = test.mapToPair(point ->
new Tuple2<>(model.predict(point._2()), point._1()));
// Calculate mean squared error between predicted and real labels.
double meanSquaredError = predictionAndLabel.mapToDouble(pl -> {
double diff = pl._1() - pl._2();
return diff * diff;
}).mean();
System.out.println("Mean Squared Error = " + meanSquaredError);
// Save and load model
model.save(jsc.sc(), "target/tmp/myIsotonicRegressionModel");
IsotonicRegressionModel sameModel =
IsotonicRegressionModel.load(jsc.sc(), "target/tmp/myIsotonicRegressionModel");
各行が ラベル,特徴 の形式を持つファイルからデータを読み込みます。例えば、4710.28,500.00。データは訓練およびテストのために分割されます。モデルは訓練セットを使って生成され、平均二乗エラーは予想されたラベルとテストセットの中の実際のラベルを使って計算されます。
APIについての詳細はIsotonicRegression
Python ドキュメント およびIsotonicRegressionModel
Python ドキュメントを参照してください。
import math
from pyspark.mllib.regression import LabeledPoint, IsotonicRegression, IsotonicRegressionModel
from pyspark.mllib.util import MLUtils
# Load and parse the data
def parsePoint(labeledData):
return (labeledData.label, labeledData.features[0], 1.0)
data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_isotonic_regression_libsvm_data.txt")
# Create label, feature, weight tuples from input data with weight set to default value 1.0.
parsedData = data.map(parsePoint)
# Split data into training (60%) and test (40%) sets.
training, test = parsedData.randomSplit([0.6, 0.4], 11)
# Create isotonic regression model from training data.
# Isotonic parameter defaults to true so it is only shown for demonstration
model = IsotonicRegression.train(training)
# Create tuples of predicted and real labels.
predictionAndLabel = test.map(lambda p: (model.predict(p[1]), p[0]))
# Calculate mean squared error between predicted and real labels.
meanSquaredError = predictionAndLabel.map(lambda pl: math.pow((pl[0] - pl[1]), 2)).mean()
print("Mean Squared Error = " + str(meanSquaredError))
# Save and load model
model.save(sc, "target/tmp/myIsotonicRegressionModel")
sameModel = IsotonicRegressionModel.load(sc, "target/tmp/myIsotonicRegressionModel")