-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-13015][MLlib][DOC] Replace example code in mllib-data-types.md using include_example #14006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4fa23b4
add scala example and fix error prompt in include_example
yinxusen 5ce1ef1
change md file
yinxusen 922ba78
refine modifier
yinxusen b0e74e3
add sc
yinxusen 51018c9
fix java imports
yinxusen 176a240
refine error prompt
yinxusen ed271b0
fix QR decompostion error
yinxusen 9e102a6
add spark sqlcontext for toDF
yinxusen 47c7b16
fix type erase for RowMatrix
yinxusen 2f879e2
Merge branch 'master' into SPARK-13015-consolidate
yinxusen c28fdb8
revert some code
yinxusen 265a62f
merge with master
yinxusen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
add scala example and fix error prompt in include_example
- Loading branch information
commit 4fa23b4fb74e252bc9b8cd9cda0f1453752639bd
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
examples/src/main/scala/org/apache/spark/examples/mllib/DataTypesExamples.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // scalastyle:off println | ||
| package org.apache.spark.examples.mllib | ||
|
|
||
| import org.apache.spark.{SparkConf, SparkContext} | ||
| import org.apache.spark.mllib.linalg.{Matrices, Matrix} | ||
| // $example on:local-vector$ | ||
| import org.apache.spark.mllib.linalg.{Vector, Vectors} | ||
| // $example off:local-vector$ | ||
| import org.apache.spark.mllib.linalg.distributed.{BlockMatrix, CoordinateMatrix, MatrixEntry} | ||
| import org.apache.spark.mllib.linalg.distributed.{IndexedRow, IndexedRowMatrix, RowMatrix} | ||
| import org.apache.spark.mllib.regression.LabeledPoint | ||
| import org.apache.spark.mllib.util.MLUtils | ||
| import org.apache.spark.rdd.RDD | ||
|
|
||
|
|
||
| object DataTypesExamples { | ||
|
|
||
| def localVectorExample(): Unit = { | ||
| // $example on:local-vector$ | ||
| // 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))) | ||
| // $example off:local-vector$ | ||
| } | ||
|
|
||
| def labeledPointExample(): Unit = { | ||
| // $example on:labeled-point$ | ||
| // 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))) | ||
| // $example off:labeled-point$ | ||
| } | ||
|
|
||
| def libsvmExample(): Unit = { | ||
| val sc = SparkContext.getOrCreate() | ||
| // $example on:libsvm$ | ||
| val examples: RDD[LabeledPoint] = | ||
| MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt") | ||
| // $example off:libsvm$ | ||
| } | ||
|
|
||
| def localMatrixExample(): Unit = { | ||
| // $example on:local-matrix$ | ||
| // 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)) | ||
| // $example off:local-matrix$ | ||
| } | ||
|
|
||
| def rowMatrixExample(): Unit = { | ||
| val sc = SparkContext.getOrCreate() | ||
| // $example on:row-matrix$ | ||
| val v1 = Vectors.dense(1.0, 10.0, 100.0) | ||
| val v2 = Vectors.dense(2.0, 20.0, 200.0) | ||
| val v3 = Vectors.dense(3.0, 30.0, 300.0) | ||
|
|
||
| val rows: RDD[Vector] = sc.parallelize(Seq(v1, v2, v3)) // 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) | ||
| // $example off:row-matrix$ | ||
| } | ||
|
|
||
| def indexedRowMatrixExample(): Unit = { | ||
| val sc = SparkContext.getOrCreate() | ||
|
|
||
| // $example on:indexed-row-matrix$ | ||
| val r0 = IndexedRow(0, Vectors.dense(1, 2, 3)) | ||
| val r1 = IndexedRow(1, Vectors.dense(4, 5, 6)) | ||
| val r2 = IndexedRow(2, Vectors.dense(7, 8, 9)) | ||
| val r3 = IndexedRow(3, Vectors.dense(10, 11, 12)) | ||
|
|
||
| val rows: RDD[IndexedRow] = sc.parallelize(Seq(r0, r1, r2, r3)) // 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() | ||
| // $example off:indexed-row-matrix$ | ||
| } | ||
|
|
||
| def coordinateMatrixExample(): Unit = { | ||
| val sc = SparkContext.getOrCreate() | ||
|
|
||
| // $example on:coordinate-row-matrix$ | ||
| val me1 = MatrixEntry(0, 0, 1.2) | ||
| val me2 = MatrixEntry(1, 0, 2.1) | ||
| val me3 = MatrixEntry(6, 1, 3.7) | ||
|
|
||
| val entries: RDD[MatrixEntry] = sc.parallelize(Seq(me1, me2, me3)) // 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() | ||
| // $example off:coordinate-row-matrix$ | ||
| } | ||
|
|
||
| def blockMatrixExample(): Unit = { | ||
| val sc = SparkContext.getOrCreate() | ||
|
|
||
| // $example on:block-matrix$ | ||
| val me1 = MatrixEntry(0, 0, 1.2) | ||
| val me2 = MatrixEntry(1, 0, 2.1) | ||
| val me3 = MatrixEntry(6, 1, 3.7) | ||
|
|
||
| // an RDD of (i, j, v) matrix entries | ||
| val entries: RDD[MatrixEntry] = sc.parallelize(Seq(me1, me2, me3)) | ||
| // 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) | ||
| // $example off:block-matrix$ | ||
| } | ||
|
|
||
| def main(args: Array[String]): Unit = { | ||
| val conf = new SparkConf().setAppName("DataTypeExamples") | ||
| val sc = new SparkContext(conf) | ||
|
|
||
| localVectorExample() | ||
| labeledPointExample() | ||
| libsvmExample() | ||
| localMatrixExample() | ||
| rowMatrixExample() | ||
| indexedRowMatrixExample() | ||
| coordinateMatrixExample() | ||
| blockMatrixExample() | ||
|
|
||
| sc.stop() | ||
| } | ||
| } | ||
| // scalastyle:on println |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer
Otherwise the label itself might be mistaken for a file path.