您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spark中怎么實現矩陣相乘操作,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1,Pom.xml引入 Spark-Mllib 類庫
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-mllib_2.11</artifactId> <version>2.3.1</version> <scope>runtime</scope> </dependency>
這里需要注意,因為我們需要使用相關API,所以這里<scope>runtime</scope>這句要去掉,使用默認的依賴方式就行了
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-mllib_2.11</artifactId> <scope>runtime</scope> </dependency>
2,代碼
public static void main(String[] args) { SparkConf sparkConf = new SparkConf().setAppName("Mllib-test").setMaster("local"); JavaSparkContext jpc = new JavaSparkContext(sparkConf); double[][] data = new double[4][4] ; data[0][0] = 0.0; data[0][1] = 2.0; data[0][2] = 3.0; data[0][3] = 4.0; data[1][0] = 1.0; data[1][1] = 3.0; data[1][2] = 4.0; data[1][3] = 5.0; data[2][0] = 2.0; data[2][1] = 4.0; data[2][2] = 5.0; data[2][3] = 6.0; data[3][0] = 3.0; data[3][1] = 5.0; data[3][2] = 6.0; data[3][3] = 7.0; JavaRDD<IndexedRow> rdd=jpc.parallelize(Arrays.asList(data)).map(f->{ long key = new Double(f[0]).longValue(); double[] value = new double[f.length-1]; for(int i = 1;i<f.length;i++) { value[i-1] = f[i]; } return new IndexedRow(key,Vectors.dense(value)); }); BlockMatrix block = new IndexedRowMatrix(rdd.rdd()).toBlockMatrix(2, 2); double[][] data1 = new double[3][3] ; data1[0][0] = 0.0; data1[0][1] = 100.0; data1[0][2] = 10.0; data1[1][0] = 1.0; data1[1][1] = 10.0; data1[1][2] = 100.0; data1[2][0] = 2.0; data1[2][1] = 1.0; data1[2][2] = 1000.0; JavaRDD<IndexedRow> rdd1 = jpc.parallelize(Arrays.asList(data1)).map(f->{ long key = new Double(f[0]).longValue(); double[] value = new double[f.length-1]; for(int i = 1;i<f.length;i++) { value[i-1] = f[i]; } return new IndexedRow(key,Vectors.dense(value)); }); BlockMatrix block1 = new IndexedRowMatrix(rdd1.rdd()).toBlockMatrix(2, 2); block = block.multiply(block1); }
關于Spark中怎么實現矩陣相乘操作就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。