降维-基于RDD的API

Dimensionality reduction is the process of reducing the number of variables under consideration. It can be used to extract latent features from raw and noisy features or compress data while maintaining the structure. spark.mllib provides support for dimensionality reduction on the RowMatrix class.

降维是减少所考虑的变量数量的过程。可用于从原始和嘈杂的特征中提取潜在特征,或者在保持结构的同时压缩数据。 spark.mllib提供对RowMatrix维的支持。

奇异值分解Singular value decomposition (SVD)

Singular value decomposition (SVD) factorizes a matrix into three matrices奇异值分解(SVD) 将矩阵分解为三个矩阵:  UU, ΣΣ, and VV such that

A=UΣVT,A=UΣVT,

where

  • UU is an orthonormal matrix, whose columns are called left singular vectors, 正交矩阵,其列称为左奇异矢量
  • ΣΣ is a diagonal matrix with non-negative diagonals in descending order, whose diagonals are called singular values, 具有非负对角线降序的对角矩阵,其对角线称为奇异值
  • VV is an orthonormal matrix, whose columns are called right singular vectors. 正交矩阵,其列称为右奇异向量。

For large matrices, usually we don’t need the complete factorization but only the top singular values and its associated singular vectors. This can save storage, de-noise and recover the low-rank structure of the matrix. 对于大型矩阵,通常不需要完整的因式分解,而仅需要顶部top奇异值及其关联的奇异矢量。可以节省存储空间,降低噪声并恢复矩阵的低阶结构。

If we keep the top k singular values, then the dimensions of the resulting low-rank matrix will be如果保持领先 ķ 奇异值,则所得低秩矩阵的维将为:

  • UU: m×km×k,
  • ΣΣ: k×kk×k,
  • VV: n×kn×k.

Performance

We assume n is smaller than m假设n小于m. The singular values and the right singular vectors are derived from the eigenvalues and the eigenvectors of the Gramian matrix 奇异值和右奇异向量是从Gramian矩阵的特征值和特征向量得出。The matrix storing the left singular vectors UU, is computed via matrix multiplication as U=A(VS−1)U=A(VS−1), if requested by the user via the computeU parameter. The actual method to use is determined automatically based on the computational cost存储左奇异向量的矩阵U" role="presentation" style="box-sizing: border-box; overflow-wrap: normal; max-width: none; max-height: none; min-width: 0px; min-height: 0px; float: none;" id="MathJax-Element-18-Frame">ü通过矩阵乘法计算为 U=A(VS−1)" role="presentation" style="box-sizing: border-box; overflow-wrap: normal; max-width: none; max-height: none; min-width: 0px; min-height: 0px; float: none;" id="MathJax-Element-19-Frame">ü= A ,如果用户通过computeU参数请求。实际使用的方法是根据计算成本自动确定的:

  • If nn is small (n<100n<100) or k is large compared with nn (k>n/2), we compute the Gramian matrix first and then compute its top eigenvalues and eigenvectors locally on the driver. This requires a single pass with O(n2) storage on each executor and on the driver, and O(n2k) time on the driver.
  • Otherwise, we compute (ATA)v in a distributive way and send it to ARPACK to compute (ATA)(ATA)’s top eigenvalues and eigenvectors on the driver node. This requires O(k) passes, O(n) storage on each executor, and O(nk) storage on the driver.
  • 如果 ñ 是小 (n < 100) 或者 ķ 与 ñ (k > n / 2个),首先计算Gramian矩阵,然后在驱动程序上局部计算其最高特征值和特征向量。需要一次通过O (ñ2个)存储在每个执行器和驱动程序上,以及 O (ñ2个k ) 在驱动程序上的时间。
  • 否则,计算 (一个ŤA )v以分布式方式将其发送到 ARPACK以进行计算(一个ŤA ),驱动程序节点上的最大特征值和特征向量。需要O (k )通过, O (n )存储在每个执行器上,以及 Ø(ñķ) 存储在驱动程序上。

SVD Example

spark.mllib provides SVD functionality to row-oriented matrices, provided in the RowMatrix class. spark.mllibRowMatrix类提供的面向行的矩阵,提供SVD功能 。

Scala

Refer to the SingularValueDecomposition Scala docs for details on the API. 有关该API的详细信息,请参考SingularValueDecompositionScala文档

import org.apache.spark.mllib.linalg.Matrix

import org.apache.spark.mllib.linalg.SingularValueDecomposition

import org.apache.spark.mllib.linalg.Vector

import org.apache.spark.mllib.linalg.Vectors

import org.apache.spark.mllib.linalg.distributed.RowMatrix

val data = Array(

Vectors.sparse(5, Seq((1, 1.0), (3, 7.0))),

Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0),

Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0))

val rows = sc.parallelize(data)

val mat: RowMatrix = new RowMatrix(rows)

// Compute the top 5 singular values and corresponding singular vectors.

val svd: SingularValueDecomposition[RowMatrix, Matrix] = mat.computeSVD(5, computeU = true)

val U: RowMatrix = svd.U  // The U factor is a RowMatrix.

val s: Vector = svd.s     // The singular values are stored in a local dense vector.

val V: Matrix = svd.V     // The V factor is a local dense matrix.

Find full example code at "examples/src/main/scala/org/apache/spark/examples/mllib/SVDExample.scala" in the Spark repo.

The same code applies to IndexedRowMatrix if U is defined as an IndexedRowMatrix.

Principal component analysis (PCA) 主成分分析

Principal component analysis (PCA) is a statistical method to find a rotation such that the first coordinate has the largest variance possible, and each succeeding coordinate, in turn, has the largest variance possible. The columns of the rotation matrix are called principal components. PCA is used widely in dimensionality reduction.

spark.mllib supports PCA for tall-and-skinny matrices stored in row-oriented format and any Vectors.

主成分分析(PCA)是一种统计方法,用于查找旋转,以使第一个坐标具有最大的方差,而每个后续坐标又具有最大的方差。旋转矩阵的列称为主成分。PCA被广泛用于降维。

spark.mllib 支持PCA用于以行格式和任何向量存储的高和稀疏矩阵。

The following code demonstrates how to compute principal components on a RowMatrix and use them to project the vectors into a low-dimensional space.

Refer to the RowMatrix Scala docs for details on the API.

以下代码演示了如何在 RowMatrix 上计算主成分,将向量投影到低维空间中。

有关该API的详细信息,请参考RowMatrixScala文档

import org.apache.spark.mllib.linalg.Matrix

import org.apache.spark.mllib.linalg.Vectors

import org.apache.spark.mllib.linalg.distributed.RowMatrix

val data = Array(

Vectors.sparse(5, Seq((1, 1.0), (3, 7.0))),

Vectors.dense(2.0, 0.0, 3.0, 4.0, 5.0),

Vectors.dense(4.0, 0.0, 0.0, 6.0, 7.0))

val rows = sc.parallelize(data)

val mat: RowMatrix = new RowMatrix(rows)

// Compute the top 4 principal components.

// Principal components are stored in a local dense matrix.

val pc: Matrix = mat.computePrincipalComponents(4)

// Project the rows to the linear space spanned by the top 4 principal components.

val projected: RowMatrix = mat.multiply(pc)

Find full example code at "examples/src/main/scala/org/apache/spark/examples/mllib/PCAOnRowMatrixExample.scala" in the Spark repo.

The following code demonstrates how to compute principal components on source vectors and use them to project the vectors into a low-dimensional space while keeping associated labels:

Refer to the PCA Scala docs for details on the API.

在Spark存储库中找到完整的示例代码。

以下代码演示了如何在源向量上计算主成分,将向量投影到低维空间中,同时保留关联的标签:

有关该API的详细信息,请参考PCAScala文档

import org.apache.spark.mllib.feature.PCA

import org.apache.spark.mllib.linalg.Vectors

import org.apache.spark.mllib.regression.LabeledPoint

import org.apache.spark.rdd.RDD

val data: RDD[LabeledPoint] = sc.parallelize(Seq(

new LabeledPoint(0, Vectors.dense(1, 0, 0, 0, 1)),

new LabeledPoint(1, Vectors.dense(1, 1, 0, 1, 0)),

new LabeledPoint(1, Vectors.dense(1, 1, 0, 0, 0)),

new LabeledPoint(0, Vectors.dense(1, 0, 0, 0, 0)),

new LabeledPoint(1, Vectors.dense(1, 1, 0, 0, 0))))

// Compute the top 5 principal components.

val pca = new PCA(5).fit(data.map(_.features))

// Project vectors to the linear space spanned by the top 5 principal

// components, keeping the label

val projected = data.map(p => p.copy(features = pca.transform(p.features)))

Find full example code at "examples/src/main/scala/org/apache/spark/examples/mllib/PCAOnSourceVectorExample.scala" in the Spark repo.

降维-基于RDD的API的更多相关文章

  1. Hive数据分析——Spark是一种基于rdd(弹性数据集)的内存分布式并行处理框架,比于Hadoop将大量的中间结果写入HDFS,Spark避免了中间结果的持久化

    转自:http://blog.csdn.net/wh_springer/article/details/51842496 近十年来,随着Hadoop生态系统的不断完善,Hadoop早已成为大数据事实上 ...

  2. Spark笔记:复杂RDD的API的理解(上)

    本篇接着讲解RDD的API,讲解那些不是很容易理解的API,同时本篇文章还将展示如何将外部的函数引入到RDD的API里使用,最后通过对RDD的API深入学习,我们还讲讲一些和RDD开发相关的scala ...

  3. Resumable.js – 基于 HTML5 File API 的文件上传

    Resumable.js 是一个 JavaScript 库,通过 HTML5 文件 API 提供,稳定和可恢复的批量上传功能.在上传大文件的时候通过每个文件分割成小块,每块在上传失败的时候,上传会不断 ...

  4. 基于.NET Socket API 通信的综合应用

    闲谈一下,最近和客户进行对接Scoket 本地的程序作为请求方以及接受方,对接Scoket 的难度实在比较大,因为涉及到响应方返回的报文的不一致性,对于返回的报文的格式我需要做反序列化的难度增大了不少 ...

  5. 基于ArcGIS JS API的在线专题地图实现

    0 引言     专题地图是突出而深入的表示一种或几种要素或现象,即按照地图主题的要求,集中表示与主题有关内容的地图.专题地图的专题要素多种多样,分类方法也多种多样,根据专题地图表现数据的特点可分为定 ...

  6. atitit.基于http json api 接口设计 最佳实践 总结o7

    atitit.基于http  json  api 接口设计 最佳实践 总结o7 1. 需求:::服务器and android 端接口通讯 2 2. 接口开发的要点 2 2.1. 普通参数 meth,p ...

  7. 基于 ArcGIS Silverlight API开发的WebGIS应用程序的部署

    部署流程概述 在微软的iis服务器上部署基于ArcGIS  Silverlight API的应用程序,主要包括以下几个步骤: 1)(可选)部署GIS服务 如果需要将GIS服务也部署在Web服务器上,则 ...

  8. 基于百度地图api + AngularJS 的入门地图

    转载请注明地址:http://www.cnblogs.com/enzozo/p/4368081.html 简介: 此入门地图为简易的“广州大学城”公交寻路地图,采用很少量的AngularJS进行inp ...

  9. PHP:基于百度大脑api实现OCR文字识别

    有个项目要用到文字识别,网上找了很多资料,效果不是很好,偶然的机会,接触到百度大脑.百度大脑提供了很多解决方案,其中一个就是文字识别,百度提供了三种文字识别,分别是银行卡识别.身份证识别和通用文字识别 ...

随机推荐

  1. 《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》

    一.利用回归树实现分类 分类也可以用回归树来做,简单说来就是训练与类别数相同的几组回归树,每一组代表一个类别,然后对所有组的输出进行softmax操作将其转换为概率分布,然后再通过交叉熵或者KL一类的 ...

  2. hdu2435最大流最小割

    2435  There is a war 题意:       给你一个有向图,其中可以有一条边是无敌的,这条边可以是图中的边,也可以是自己任意加上去的图中没有的边,这条无敌的边不可以摧毁,让1和n无法 ...

  3. hdu4118

    题意:       给你一颗无向带权树,每个定点上有一个人,问所有定点都不在自己位置上的最长路径总和是多少..   思路:       其实很简单,贪心的想下,既然要求全局最大,那么对于每一条边用的次 ...

  4. POJ1988(带权并查集,搬砖块)

    题意:        可以这样理解,有n快方形积木,一开始都是单独的放到哪,然后有两种操作 1 M a b 把a所在的那一堆落到b所在那一堆的上面(一开始自己是一堆) 2 C a 问a下面有多少个积木 ...

  5. (转)VMware中桥接模式与NAT模式的区别

    bridged networking(桥接模式) 在这样的模式下.VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它能够訪问网内不论什么一台机器. 在桥接模式下.你须要手工为虚拟系统配置 ...

  6. PowerDesigner16安装和使用

    安装 安装参考链接:PowerDesigner安装教程 因为这个博主已经操作的很详细了,这边就不做过多的赘述. 使用 新建模型 选择物理模型 调出面板Palette 建表 最终的效果(一般不在数据库层 ...

  7. 从零开始搞监控系统(1)——SDK

    目前市面上有许多成熟的前端监控系统,但我们没有选择成品,而是自己动手研发.这里面包括多个原因: 填补H5日志的空白 节约公司费用支出 可灵活地根据业务自定义监控 回溯时间能更长久 反哺运营和产品,从而 ...

  8. 『动善时』JMeter基础 — 17、JMeter配置元件【HTTP请求默认值】

    目录 1.HTTP请求默认值介绍 2.HTTP请求默认值界面 3.HTTP请求默认值的使用 (1)用于演示的项目说明 (2)测试计划内包含的元件 (3)说明HTTP请求默认值用法 4.总结 5.拓展知 ...

  9. 如何提高CRM系统使用率?

    随着时代的发展和市场的变化,客户在企业的眼中开始变得越来越重要.谁拥有更多的客户,谁就能在激烈的市场竞争中占据一席之地.现在很多企业通过CRM系统转变为了"以客户为中心".但是,许 ...

  10. [c++] 子类构造函数中有默认参数

    子类创建对象时调用父类的构造函数: 1 #include <iostream> 2 using namespace std; 3 class Base 4 { 5 public: 6 Ba ...