Data Types - MLlib
MLlib は一つのマシーン上で格納されたローカルベクトルとマトリックスをサポートとし、1つ以上のRDDによって分散型マトリックスによって支援されます。ローカルベクトルとローカルマトリックスは公開インタフェースとして提供する単純なデータモデルです。基礎となる線形代数操作は Breeze と jblas によって提供あsれます。管理される学習の訓練の例はMLlibでは"ラベル付きの点"と呼ばれます。
ローカルベクトル
ローカルベクトルは一つのマシーン上で保存された整数値型および0ベースのインデックスとdouble型の値を持ちます。MLlibは2つの種類のローカルベクトルをサポートします: dense と sparse。dense ベクトルはそのエントリー値を表すdoubleの配列によって支援されますが、sparseベクトルは2つの配列: インデックスと値 によって支援されます。例えば、ベクトル (1.0, 0.0, 3.0)
は [1.0, 0.0, 3.0]
としてdense形式、あるいは (3, [0, 2], [1.0, 3.0])
3
はベクトルのサイズとしてsparse形式で表すことができます。
ローカルベクトルのベースクラスは Vector
で、2つの実装を提供します: DenseVector
と SparseVector
です。ローカルベクトルを生成するにはVectors
で実装されているファクトリーメソッドを使うことをお勧めします。
APIの詳細はVector
Scala ドキュメント および Vectors
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.{Vector, Vectors}
// Create a dense vector (1.0, 0.0, 3.0).
val dv: Vector = Vectors.dense(1.0, 0.0, 3.0)
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.
val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0)))
Note:
Scala imports scala.collection.immutable.Vector
by default, so you have to import
org.apache.spark.mllib.linalg.Vector
explicitly to use MLlib’s Vector
.
ローカルベクトルのベースクラスは Vector
で、2つの実装を提供します: DenseVector
と SparseVector
です。ローカルベクトルを生成するにはVectors
で実装されているファクトリーメソッドを使うことをお勧めします。
APIの詳細はVector
Java ドキュメント および Vectors
Java ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.Vectors;
// Create a dense vector (1.0, 0.0, 3.0).
Vector dv = Vectors.dense(1.0, 0.0, 3.0);
// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.
Vector sv = Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0});
MLlib は dense ベクトルとして以下のタイプを認識します。
- NumPy の
配列
- Pythonのリスト、例えば
[1, 2, 3]
そして、以下はsparseベクトルです:
- MLlib の
SparseVector
. - 一つのカラムを持つ SciPy の
csc_matrix
効率のためにリスト上のNumPy配列を使うことをお勧めします。sparseベクトルを生成するにはVectors
で実装されているファクトリーメソッドを使うことをお勧めします。
APIの詳細はVectors
Python ドキュメント を参照してください。
import numpy as np
import scipy.sparse as sps
from pyspark.mllib.linalg import Vectors
# Use a NumPy array as a dense vector.
dv1 = np.array([1.0, 0.0, 3.0])
# Use a Python list as a dense vector.
dv2 = [1.0, 0.0, 3.0]
# Create a SparseVector.
sv1 = Vectors.sparse(3, [0, 2], [1.0, 3.0])
# Use a single-column SciPy csc_matrix as a sparse vector.
sv2 = sps.csc_matrix((np.array([1.0, 3.0]), np.array([0, 2]), np.array([0, 2])), shape = (3, 1))
ラベル付きの点
ラベル付きの点はローカルベクトル、denseあるいはsparseのどちらか、label/response に関係しています。MLlibでは、ラベル付きの点は管理されている学習アルゴリズムで使用されます。ラベルを格納するためにdoubleを使うため、ラベル付きの点を回帰と分類の両方で使うことができます。2値分類のためには、ラベルは 0
(否定) あるいは 1
(肯定)のどちらかでなければなりません。多層分類のためには、ラベルはゼロから始まる分類インデックスでなければなりません: 0, 1, 2, ...
。
ラベル付きの点は LabeledPoint
の場合クラスによって表されます。
APIの詳細はLabeledPoint
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
// Create a labeled point with a positive label and a dense feature vector.
val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
// Create a labeled point with a negative label and a sparse feature vector.
val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)))
ラベル付きの点は LabeledPoint
によって表されます。
APIの詳細はLabeledPoint
Java ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.Vectors;
import org.apache.spark.mllib.regression.LabeledPoint;
// Create a labeled point with a positive label and a dense feature vector.
LabeledPoint pos = new LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0));
// Create a labeled point with a negative label and a sparse feature vector.
LabeledPoint neg = new LabeledPoint(0.0, Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0}));
ラベル付きの点は LabeledPoint
によって表されます。
APIの詳細はLabeledPoint
Python ドキュメント を参照してください。
from pyspark.mllib.linalg import SparseVector
from pyspark.mllib.regression import LabeledPoint
# Create a labeled point with a positive label and a dense feature vector.
pos = LabeledPoint(1.0, [1.0, 0.0, 3.0])
# Create a labeled point with a negative label and a sparse feature vector.
neg = LabeledPoint(0.0, SparseVector(3, [0, 2], [1.0, 3.0]))
希薄なデータ
実際においてsparse訓練データを持つことは良くある事です。MLlib はLIBSVM
形式で保存された訓練データの読み込みをサポートし、これはLIBSVM
および LIBLINEAR
によって使われるデフォルトの形式です。各行は以下の形式を使ったラベル付きのsparse特徴ベクトルのテキスト形式です:
label index1:value1 index2:value2 ...
ここでインデックスは1から始まる昇順です。ロードした後で特徴インデックスはゼロから始まるように変換されます。
MLUtils.loadLibSVMFile
はLIBSVM形式で格納された訓練の例を読み込みます。
APIの詳細はMLUtils
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.rdd.RDD
val examples: RDD[LabeledPoint] = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
MLUtils.loadLibSVMFile
はLIBSVM形式で格納された訓練の例を読み込みます。
APIの詳細はMLUtils
Java ドキュメント を参照してください。
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.util.MLUtils;
import org.apache.spark.api.java.JavaRDD;
JavaRDD<LabeledPoint> examples =
MLUtils.loadLibSVMFile(jsc.sc(), "data/mllib/sample_libsvm_data.txt").toJavaRDD();
MLUtils.loadLibSVMFile
はLIBSVM形式で格納された訓練の例を読み込みます。
APIの詳細はMLUtils
Python ドキュメント を参照してください。
from pyspark.mllib.util import MLUtils
examples = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
ローカル マトリックス
ローカルマトリックスは一つのマシーン上で格納された、整数型の行とカラムインデックスとdouble型の値を持ちます。MLlibは、各値が1x2の列優先順序で格納されている密行列と、非ゼロの値が列優先順序の Compressed Sparse Column (CSC)形式で格納されている疎行列をサポートします。例えば、以下の密行列 \[ \begin{pmatrix} 1.0 & 2.0 \\ 3.0 & 4.0 \\ 5.0 & 6.0 \end{pmatrix} \]
は(3, 2)
のマトリックスサイズを持つ一次元の配列 [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]
に保持されます。
ローカルマトリックスの基本クラスは Matrix
で、2つの実装を提供します: DenseMatrix
, および SparseMatrix
。ローカルマトリックスを生成するには Matrices
で実装されているファクトリーメソッドを使うことをお勧めします。MLlibのローカルマトリックスはcolumn-major順に格納されていることを思い出してください。
APIの詳細はMatrix
Scala ドキュメント および Matrices
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.{Matrix, Matrices}
// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))
// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))
val sm: Matrix = Matrices.sparse(3, 2, Array(0, 1, 3), Array(0, 2, 1), Array(9, 6, 8))
ローカルマトリックスの基本クラスは Matrix
で、2つの実装を提供します: DenseMatrix
, および SparseMatrix
。ローカルマトリックスを生成するには Matrices
で実装されているファクトリーメソッドを使うことをお勧めします。MLlibのローカルマトリックスはcolumn-major順に格納されていることを思い出してください。
APIの詳細はMatrix
Java ドキュメント および Matrices
Java ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.Matrix;
import org.apache.spark.mllib.linalg.Matrices;
// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
Matrix dm = Matrices.dense(3, 2, new double[] {1.0, 3.0, 5.0, 2.0, 4.0, 6.0});
// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))
Matrix sm = Matrices.sparse(3, 2, new int[] {0, 1, 3}, new int[] {0, 2, 1}, new double[] {9, 6, 8});
ローカルマトリックスの基本クラスは Matrix
で、2つの実装を提供します: DenseMatrix
, および SparseMatrix
。ローカルマトリックスを生成するには Matrices
で実装されているファクトリーメソッドを使うことをお勧めします。MLlibのローカルマトリックスはcolumn-major順に格納されていることを思い出してください。
APIについての詳細はMatrix
Python ドキュメント およびMatrices
Python ドキュメントを参照してください。
import org.apache.spark.mllib.linalg.{Matrix, Matrices}
// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
dm2 = Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6])
// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))
sm = Matrices.sparse(3, 2, [0, 1, 3], [0, 2, 1], [9, 6, 8])
分散型マトリックス
分散型マトリックスはlong型の行とdouble型の値を持ち、分散して一つ以上のRDDに格納されます。大きく分散したマトリックスを格納するには正しい形式を選択することがとても重要です。分散されたマトリックスの異なる形式への変換はグローバルシャッフルを必要とし、これはとても高価なものとなります。今のところ3つの種類の分散型マトリックスが実装されています。
基本タイプは RowMatrix
と呼ばれます。RowMatrix
は意味を持たない行インデックスの行指向の分散型マトリックスです。例えば、特徴ベクトルのコレクション。それはその行のRDDによって支援され、各行はローカルベクトルです。We assume that the number of columns is not huge for a RowMatrix
so that a single local vector can be reasonably communicated to the driver and can also be stored / operated on using a single node. IndexedRowMatrix
はRowMatrix
に似ていますが、行のインデックスを持ち、それは行の指定とjoinの実行のために使うことができます。CoordinateMatrix
はcoordinate list (COO) 形式で保持される分散型マトリックスで、そのエントリのRDDによって支援されています。
注意
マトリックスのサイズをキャッシュするため、分散マトリックのRDDの背景は決定的でなければなりません。haikei 一般的に決定的ではないRDDの使用はエラーにつながります。
RowMatrix
RowMatrix
は意味を持たない行インデックスの行指向の分散型マトリックスで、その行はRDDによって支援され、各行はローカルベクトルを持ちます。 各行はローカルベクトルによって表されるため、行数は整数の範囲に収まりますが、実際にはもっと小さくなければなりません。
RowMatrix
はRDD[Vector]
インスタンスから生成することができます。そして、その行のsummary統計と分解を計算することができます。QR decomposition is of the form A = QR where Q is an orthogonal matrix and R is an upper triangular matrix. 特異値分解 (SVD) と 主成分分析 (PCA)については、次元削減を参照してください。
APIの詳細はRowMatrix
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.Vector
import org.apache.spark.mllib.linalg.distributed.RowMatrix
val rows: RDD[Vector] = ... // an RDD of local vectors
// Create a RowMatrix from an RDD[Vector].
val mat: RowMatrix = new RowMatrix(rows)
// Get its size.
val m = mat.numRows()
val n = mat.numCols()
// QR decomposition
val qrResult = mat.tallSkinnyQR(true)
RowMatrix
は JavaRDD<Vector>
インスタンスから生成することができます。そして、その行のsummary統計を計算することができます。
APIの詳細はRowMatrix
Java ドキュメント を参照してください。
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.mllib.linalg.distributed.RowMatrix;
JavaRDD<Vector> rows = ... // a JavaRDD of local vectors
// Create a RowMatrix from an JavaRDD<Vector>.
RowMatrix mat = new RowMatrix(rows.rdd());
// Get its size.
long m = mat.numRows();
long n = mat.numCols();
// QR decomposition
QRDecomposition<RowMatrix, Matrix> result = mat.tallSkinnyQR(true);
RowMatrix
はベクトルのRDD
から生成することができます。
APIの詳細はRowMatrix
Python ドキュメント を参照してください。
from pyspark.mllib.linalg.distributed import RowMatrix
# Create an RDD of vectors.
rows = sc.parallelize([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
# Create a RowMatrix from an RDD of vectors.
mat = RowMatrix(rows)
# Get its size.
m = mat.numRows() # 4
n = mat.numCols() # 3
# Get the rows as an RDD of vectors again.
rowsRDD = mat.rows
IndexedRowMatrix
IndexedRowMatrix
はRowMatrix
に似ていますが、意味のある行インデックスを持ちます。それはインデックスされた行のRDDによって支援され、各行はインデックス(long型)とローカルベクトルによって表されます。
IndexedRowMatrix
はRDD[IndexedRow]
インスタンスから生成することができ、 IndexedRow
は (Long, Vector)
をラップします。IndexedRowMatrix
は行のインデックスを落とすことで RowMatrix
に変換することができます。
APIの詳細はIndexedRowMatrix
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.distributed.{IndexedRow, IndexedRowMatrix, RowMatrix}
val rows: RDD[IndexedRow] = ... // an RDD of indexed rows
// Create an IndexedRowMatrix from an RDD[IndexedRow].
val mat: IndexedRowMatrix = new IndexedRowMatrix(rows)
// Get its size.
val m = mat.numRows()
val n = mat.numCols()
// Drop its row indices.
val rowMat: RowMatrix = mat.toRowMatrix()
IndexedRowMatrix
は JavaRDD<IndexedRow>
インスタンスから生成することができ、IndexedRow
は (long, Vector)
をラップします。IndexedRowMatrix
は行のインデックスを落とすことで RowMatrix
に変換することができます。
APIの詳細はIndexedRowMatrix
Java ドキュメント を参照してください。
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.linalg.distributed.IndexedRow;
import org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix;
import org.apache.spark.mllib.linalg.distributed.RowMatrix;
JavaRDD<IndexedRow> rows = ... // a JavaRDD of indexed rows
// Create an IndexedRowMatrix from a JavaRDD<IndexedRow>.
IndexedRowMatrix mat = new IndexedRowMatrix(rows.rdd());
// Get its size.
long m = mat.numRows();
long n = mat.numCols();
// Drop its row indices.
RowMatrix rowMat = mat.toRowMatrix();
IndexedRowMatrix
はIndexedRow
のRDD
から生成することができ、 IndexedRow
は (long, vector)
をラップします。IndexedRowMatrix
は行のインデックスを落とすことで RowMatrix
に変換することができます。
APIの詳細はIndexedRowMatrix
Python ドキュメント を参照してください。
from pyspark.mllib.linalg.distributed import IndexedRow, IndexedRowMatrix
# Create an RDD of indexed rows.
# - This can be done explicitly with the IndexedRow class:
indexedRows = sc.parallelize([IndexedRow(0, [1, 2, 3]),
IndexedRow(1, [4, 5, 6]),
IndexedRow(2, [7, 8, 9]),
IndexedRow(3, [10, 11, 12])])
# - or by using (long, vector) tuples:
indexedRows = sc.parallelize([(0, [1, 2, 3]), (1, [4, 5, 6]),
(2, [7, 8, 9]), (3, [10, 11, 12])])
# Create an IndexedRowMatrix from an RDD of IndexedRows.
mat = IndexedRowMatrix(indexedRows)
# Get its size.
m = mat.numRows() # 4
n = mat.numCols() # 3
# Get the rows as an RDD of IndexedRows.
rowsRDD = mat.rows
# Convert to a RowMatrix by dropping the row indices.
rowMat = mat.toRowMatrix()
# Convert to a CoordinateMatrix.
coordinateMat = mat.toCoordinateMatrix()
# Convert to a BlockMatrix.
blockMat = mat.toBlockMatrix()
CoordinateMatrix
CoordinateMatrix
はエントリのRDDによって支援される分散型マトリックスです。各円取りは (i: Long, j: Long, value: Double)
のタプルで、i
は行インデックスで、j
はカラムのインデックスで、value
はエントリの値です。CoordinateMatrix
はマトリックスの次元が大きくマトリックスがとてもまばらな時に使うべきです。
CoordinateMatrix
は RDD[MatrixEntry]
インスタンスから生成することができ、MatrixEntry
は (Long, Long, Double)
のラッパーです。CoordinateMatrix
はtoIndexedRowMatrix
を呼び出すことでまばらな行を持つ IndexedRowMatrix
に変換することができます。CoordinateMatrix
のための他の計算は現在のところサポートされていません。
APIの詳細はCoordinateMatrix
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.distributed.{CoordinateMatrix, MatrixEntry}
val entries: RDD[MatrixEntry] = ... // an RDD of matrix entries
// Create a CoordinateMatrix from an RDD[MatrixEntry].
val mat: CoordinateMatrix = new CoordinateMatrix(entries)
// Get its size.
val m = mat.numRows()
val n = mat.numCols()
// Convert it to an IndexRowMatrix whose rows are sparse vectors.
val indexedRowMatrix = mat.toIndexedRowMatrix()
CoordinateMatrix
はJavaRDD<MatrixEntry>
インスタンスから生成することができ、 MatrixEntry
は (long, long, double)
をラップします。CoordinateMatrix
はtoIndexedRowMatrix
を呼び出すことでまばらな行を持つ IndexedRowMatrix
に変換することができます。CoordinateMatrix
のための他の計算は現在のところサポートされていません。
APIの詳細はCoordinateMatrix
Java ドキュメント を参照してください。
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.linalg.distributed.CoordinateMatrix;
import org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix;
import org.apache.spark.mllib.linalg.distributed.MatrixEntry;
JavaRDD<MatrixEntry> entries = ... // a JavaRDD of matrix entries
// Create a CoordinateMatrix from a JavaRDD<MatrixEntry>.
CoordinateMatrix mat = new CoordinateMatrix(entries.rdd());
// Get its size.
long m = mat.numRows();
long n = mat.numCols();
// Convert it to an IndexRowMatrix whose rows are sparse vectors.
IndexedRowMatrix indexedRowMatrix = mat.toIndexedRowMatrix();
CoordinateMatrix
は MatrixEntry
エントリのRDD
から生成することができ、 MatrixEntry
は(long, long, float)
のラッパーです。CoordinateMatrix
は toRowMatrix
を呼び出すことで RowMatrix
に変換することができ、あるいはtoIndexedRowMatrix
を呼び出すことでまばらな行を持つIndexedRowMatrix
に変換することができます。
APIの詳細はCoordinateMatrix
Python ドキュメント を参照してください。
from pyspark.mllib.linalg.distributed import CoordinateMatrix, MatrixEntry
# Create an RDD of coordinate entries.
# - This can be done explicitly with the MatrixEntry class:
entries = sc.parallelize([MatrixEntry(0, 0, 1.2), MatrixEntry(1, 0, 2.1), MatrixEntry(6, 1, 3.7)])
# - or using (long, long, float) tuples:
entries = sc.parallelize([(0, 0, 1.2), (1, 0, 2.1), (2, 1, 3.7)])
# Create an CoordinateMatrix from an RDD of MatrixEntries.
mat = CoordinateMatrix(entries)
# Get its size.
m = mat.numRows() # 3
n = mat.numCols() # 2
# Get the entries as an RDD of MatrixEntries.
entriesRDD = mat.entries
# Convert to a RowMatrix.
rowMat = mat.toRowMatrix()
# Convert to an IndexedRowMatrix.
indexedRowMat = mat.toIndexedRowMatrix()
# Convert to a BlockMatrix.
blockMat = mat.toBlockMatrix()
BlockMatrix
BlockMatrix
はMatrixBlock
のRDDによって支援される分散型マトリックスで、MatrixBlock
は((Int, Int), Matrix)
のタプルで、(Int, Int)
はブロックのインデックスで、Matrix
はサイズが rowsPerBlock
x colsPerBlock
の指定されたインデックスの部分マトリックスです。BlockMatrix
は 他のBlockMatrix
とのadd
および multiply
のようなメソッドをサポートします。BlockMatrix
はBlockMatrix
が適切にセットアップされたかどうかをチェックするために使用することができる ヘルパー関数validate
も持ちます。
BlockMatrix
は toBlockMatrix
を呼び出すことでIndexedRowMatrix
あるいはCoordinateMatrix
から最も簡単に生成することができます。toBlockMatrix
はデフォルトで 1024 x 1024 のサイズのブロックを生成します。ユーザは toBlockMatrix(rowsPerBlock, colsPerBlock)
を使って値を渡すことでブロックサイズを変更するかも知れません。
APIの詳細はBlockMatrix
Scala ドキュメント を参照してください。
import org.apache.spark.mllib.linalg.distributed.{BlockMatrix, CoordinateMatrix, MatrixEntry}
val entries: RDD[MatrixEntry] = ... // an RDD of (i, j, v) matrix entries
// Create a CoordinateMatrix from an RDD[MatrixEntry].
val coordMat: CoordinateMatrix = new CoordinateMatrix(entries)
// Transform the CoordinateMatrix to a BlockMatrix
val matA: BlockMatrix = coordMat.toBlockMatrix().cache()
// Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.
// Nothing happens if it is valid.
matA.validate()
// Calculate A^T A.
val ata = matA.transpose.multiply(matA)
BlockMatrix
は toBlockMatrix
を呼び出すことでIndexedRowMatrix
あるいはCoordinateMatrix
から最も簡単に生成することができます。toBlockMatrix
はデフォルトで 1024 x 1024 のサイズのブロックを生成します。ユーザは toBlockMatrix(rowsPerBlock, colsPerBlock)
を使って値を渡すことでブロックサイズを変更するかも知れません。
APIの詳細はBlockMatrix
Java ドキュメント を参照してください。
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.mllib.linalg.distributed.BlockMatrix;
import org.apache.spark.mllib.linalg.distributed.CoordinateMatrix;
import org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix;
JavaRDD<MatrixEntry> entries = ... // a JavaRDD of (i, j, v) Matrix Entries
// Create a CoordinateMatrix from a JavaRDD<MatrixEntry>.
CoordinateMatrix coordMat = new CoordinateMatrix(entries.rdd());
// Transform the CoordinateMatrix to a BlockMatrix
BlockMatrix matA = coordMat.toBlockMatrix().cache();
// Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.
// Nothing happens if it is valid.
matA.validate();
// Calculate A^T A.
BlockMatrix ata = matA.transpose().multiply(matA);
BlockMatrix
は部分マトリックスのブロックRDD
から生成することができ、部分マトリックスのブロックは((blockRowIndex, blockColIndex), sub-matrix)
タプルです。
APIの詳細はBlockMatrix
Python ドキュメント を参照してください。
from pyspark.mllib.linalg import Matrices
from pyspark.mllib.linalg.distributed import BlockMatrix
# Create an RDD of sub-matrix blocks.
blocks = sc.parallelize([((0, 0), Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6])),
((1, 0), Matrices.dense(3, 2, [7, 8, 9, 10, 11, 12]))])
# Create a BlockMatrix from an RDD of sub-matrix blocks.
mat = BlockMatrix(blocks, 3, 2)
# Get its size.
m = mat.numRows() # 6
n = mat.numCols() # 2
# Get the blocks as an RDD of sub-matrix blocks.
blocksRDD = mat.blocks
# Convert to a LocalMatrix.
localMat = mat.toLocalMatrix()
# Convert to an IndexedRowMatrix.
indexedRowMat = mat.toIndexedRowMatrix()
# Convert to a CoordinateMatrix.
coordinateMat = mat.toCoordinateMatrix()