一、spark 数据类型(Data Types)
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});
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}));
LIBSVM 和LIBLINERAR 中用到的默认格式(LIBSVM和LIBLINERAR是台湾林智仁教授开发的的SVM库和线性分类器)。这是一种文本格式,每行表示一个标记的稀疏特征向量,示例如下:label index1:value1 index2:value2 ... 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();
会被存储为一维数组[1.0, 3.0, 5.0, 2.0, 4.0, 6.0],矩阵的大小是(3, 2)。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});
分布式矩阵[Distributed matrix]:
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);
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();
CoordinateMatrix)也是由RDD做底层结构的分布式矩阵。每个RDD元素是由多个(i : long, j : long, value: Double)组成的元组,其中i是行索引,j是列索引,value是元素值。CoordinateMatrix 只应该应用于矩阵纬度高并且稀疏的情况下。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();
rowsPerBlock xcolsPerBlock。BlockMatrix支持跟其他BlockMatrix做add(加)和multiply(乘)操作。BlockMatrix还有一个辅助方法validate,这个方法可以检查BlockMatrix是否设置是否恰当。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);
一、spark 数据类型(Data Types)的更多相关文章
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- 【12c】扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE
[12c]扩展数据类型(Extended Data Types)-- MAX_STRING_SIZE 在12c中,与早期版本相比,诸如VARCHAR2, NAVARCHAR2以及 RAW这些数据类型的 ...
- 表达式,数据类型和变量(Expressions,Data Types & Variables)
(一)表达式: 1)4+4就是表达式,它是程序中最基本的编程指令:表达式包含一个值(4)和操作符号(+),然后就会计算出一个单独的值; 2)一个单独的值没有包含操作符号也可以叫表达式,尽管它只计算它本 ...
- ExtJS笔记 Ext.data.Types
This is a static class containing the system-supplied data types which may be given to a Field. Type ...
- TypeScript学习指南第一章--基础数据类型(Basic Types)
基础数据类型(Basic Types) 为了搭建应用程序,我们需要使用一些基础数据类型比如:numbers,strings,structures,boolean等等. 在TypeScript中除了Ja ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
- Oracle Schema Objects——Tables——Oracle Data Types
Oracle Schema Objects Oracle Data Types 数据类型 Data Type Description NUMBER(P,S) Number value having a ...
- 4.Data Types in the mongo Shell-官方文档摘录
总结: 1.MongoDB 的BSON格式支持额外的数据类型 2 Date 对象内部存储64位字节存整数,存储使用NumberLong()这个类来存,使用NumberInt()存32位整数,128位十 ...
- 【翻译】苹果官网的命名规范之 Naming Properties and Data Types
苹果官方原文:Naming Properties and Data Types 前言:纯属练习英语和学习.翻译错误和不通顺的地方敬请谅解和指正.O(∩_∩)O 属性和数据类型的命名 本节讲述了属性定义 ...
随机推荐
- 矩阵快速幂(入门) 学习笔记hdu1005, hdu1575, hdu1757
矩阵快速幂是基于普通的快速幂的一种扩展,如果不知道的快速幂的请参见http://www.cnblogs.com/Howe-Young/p/4097277.html.二进制这个东西太神奇了,好多优秀的算 ...
- POJ2914
POJ2914 无向图的最小割 题意:给你一个无向图,然后去掉其中的n条边,使之形成两个连通分量,也即原无向图不连通,求n的最小值. 输入: m(无向图点集),n(无向图边集) a,b,c(a,b两点 ...
- 如何清理多余的Windows桌面右键菜单
删除多余的发送到选项 Win7使用一段时间后,我们可能会装很多软件,这时候右键菜单可能会变得很长,特别是“发送到(Send to)”里面的选项,有时候我们卸载软件之后在右键发送到菜单里还会有残存的选项 ...
- JavaAppArguments.java程序的更改
此程序模仿JvaAppArgyments.java编写,从命令行接受多个数字,求和之后输出结果. 设计思想:命令行参数都是字符串,可以考虑用 Integer.parseInt(args[]) ...
- 2013中国大数据技术大会——BDTC2013
阿里飞天平台总架构师唐洪分享了飞天开放平台. 飞天平台特点:同时支持在线和离线应用:安全控制:分布式部署,监控和诊断:无单点故障,可用性99.9%:数据3副本,数据可靠性“10个9”. 阿里飞天平台总 ...
- 转:测试用书(出处:cnblog--liangshi)
列表格式为:图书分类.中文书名.英文书名.作者.排名不分先后,用红色标记出我推荐的书籍. 测试入门 软件测试(第2版) Software Testing (2e), Ron Patton 一本测试入门 ...
- nginx虚拟配置
server { listen 8080; server_name www.manihome.com ; root "D:/WWW/mnmnh_2015"; location / ...
- Nginx并发访问优化
Nginx反向代理并发能力的强弱,直接影响到系统的稳定性.安装Nginx过程,默认配置并不涉及到过多的并发参数,作为产品运行,不得不考虑这些因素.Nginx作为产品运行,官方建议部署到Linux64位 ...
- JQUERY1.9学习笔记 之基本过滤器(七) 语言选择器
语言选择器 jQuery( ":lang(language)" ) 描述:选择所有用特定语言指定的标签. 根据标签指定语言的不同给标签上色. <!DOCTYPE html&g ...
- JQUERY1.9学习笔记 之基本过滤器(二) 等于选择器
等于选择器 :eq() 描述:选择与设定下标匹配的元素.jQuery( ":eq(index)" )jQuery( ":eq(-index)" ) <!D ...