赞
踩
pyspark.mllib(Python)内。
(1.0, 0.0, 3.0)
写成稠密形式就是 [1.0, 0.0, 3.0]
,而写成稀疏形式则是 (3, [0, 2], [1.0, 3.0])
,后者的第一个 3 是指向量的大小。稀疏和稠密的界限没有严格意义上的标准,通常需要依据具体的问题来决定。
Scala环境下:
//创建稠密向量
scala> val denseVec1 = Vectors.dense(1.0,2.0,3.0)
denseVec1: org.apache.spark.mllib.linalg.Vector = [1.0,2.0,3.0]
scala> val denseVec2 = Vectors.dense(Array(1.0,2.0,3.0))
denseVec2: org.apache.spark.mllib.linalg.Vector = [1.0,2.0,3.0]
//创建稀疏向量
scala> val sparseVec1 = Vectors.sparse(4,Array(0,2),Array(1.0,2.0))
sparseVec1: org.apache.spark.mllib.linalg.Vector = (4,[0,2],[1.0,2.0])
python环境下:
>>> from pyspark.mllib.linalg import Vectors
>>> den = Vectors.dense([1.0,2.0,3.0])
>>> den
DenseVector([1.0, 2.0, 3.0])
>>> spa = Vectors.sparse(4,[0,2],[1.0,2.0])
>>> spa
SparseVector(4, {0: 1.0, 2: 2.0})
mllib.regression
包中。
Scala环境中:
// 首先需要引入标签点相关的类
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
// 创建一个带有正面标签和稠密特征向量的标签点。
val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
// 创建一个带有负面标签和稀疏特征向量的标签点。
val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)))
注意其第一个参数为标签,第二个参数为向量。标签是用Double
类型表示的。
Python环境中:
>>> from pyspark.mllib.regression import LabeledPoint
>>> from pyspark.mllib.linalg import Vectors
>>> pos = LabeledPoint(1.0,Vectors.dense([1.0,2.0,3.0]))
>>> neg = LabeledPoint(0.0,Vectors.dense([1.0,2.0,3.0]))
import org.apache.spark.mllib.linalg.{Matrix, Matrices}
// 创建稠密矩阵 ((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))
关于稀疏矩阵的存储方式请参考:http://www.tuicool.com/articles/A3emmqi
/下列矩阵
1.0 0.0 4.0
0.0 3.0 5.0
2.0 0.0 6.0
/
如果采用稀疏矩阵存储的话,其存储信息包括:
实际存储值: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]`,
矩阵元素对应的行索引:rowIndices=[0, 2, 1, 0, 1, 2]`
列起始位置索引: `colPointers=[0, 2, 3, 6]`.
则生成稀疏矩阵的方式为:
scala> val sparseMatrix= Matrices.sparse(3, 3, Array(0, 2, 3, 6), Array(0, 2, 1, 0, 1, 2), Array(1.0, 2.0, 3.0,4.0,5.0,6.0))
sparseMatrix: org.apache.spark.mllib.linalg.Matrix =
3 x 3 CSCMatrix
(0,0) 1.0
(2,0) 2.0
(1,1) 3.0
(0,2) 4.0
(1,2) 5.0
(2,2) 6.0
关于更多类型介绍参考:
http://spark.apache.org/docs/1.6.1/mllib-data-types.html#local-matrix
(Pearson’s independence test) 结果。返回一个ChiSqTestResult 对象, 其中有p 值
(p-value)、测试统计及每个特征的自由度。标签和特征值必须是分类的(即离散值)。构建测试数据,
Python环境下:
from pyspark.mllib.stat import Statistics
from pyspark.mllib.linalg import Vectors
//构建RDD
basicTestRDD = sc.parallelize([Vectors.dense([60, 70, 80, 0]),
Vectors.dense([80, 50, 0, 90]),
Vectors.dense([60, 70, 80, 0])])
//以查看下summary里的成员,这个对象中包含了大量的统计内容
>>> print summary.mean()
[ 66.66666667 63.33333333 53.33333333 30. ]
>>> print summary.variance()
[ 133.33333333 133.33333333 2133.33333333 2700. ]
>>> print summary.numNonzeros()
[ 3. 3. 2. 1.]
Scala环境:
import org.apache.spark.mllib.linalg.{Vector, Vectors}
import org.apache.spark.rdd.RDD
val array1: Array[Double] = Array[Double](60, 70, 80, 0)
val array2: Array[Double] = Array[Double](80, 50, 0, 90)
val array3: Array[Double] = Array[Double](60, 70, 80, 0)
val denseArray1 = Vectors.dense(array1)
val denseArray2 = Vectors.dense(array2)
val denseArray3 = Vectors.dense(array3)
val seqDenseArray: Seq[Vector] = Seq(denseArray1, denseArray2, denseArray3)
val basicTestRDD: RDD[Vector] = sc.parallelize[Vector](seqDenseArray)
val summary: MultivariateStatisticalSummary = Statistics.colStats(basicTestRDD)

关于更多的统计可以参考:http://spark.apache.org/docs/1.6.1/mllib-statistics.html
Python环境下:
# 读取数据文件,创建RDD
dataFile = "/opt/spark-1.6.1-bin-hadoop2.6/data/mllib/kmeans_data.txt"
lines = sc.textFile(dataFile)
# 创建Vector,将每行的数据用空格分隔后转成浮点值返回numpy的array
data = lines.map(lambda line: np.array([float(x) for x in line.split(' ')]))
# 其中2是簇的个数
model = KMeans.train(data, 2)
print("Final centers: " + str(model.clusterCenters))
print("Total Cost: " + str(model.computeCost(data)))
/opt/spark-1.6.1-bin-hadoop2.6/data/mllib/sample_libsvm_data.txt
。
# 加载模块
from pyspark.mllib.util import MLUtils
from pyspark.mllib.classification import SVMWithSGD
# 读取数据
dataFile = '/opt/spark-1.6.1-bin-hadoop2.6/data/mllib/sample_libsvm_data.txt'
data = MLUtils.loadLibSVMFile(sc, dataFile)
splits = data.randomSplit([0.8, 0.2], seed = 9L)
training = splits[0].cache()
test = splits[1]
# 打印分割后的数据量
print "TrainingCount:[%d]" % training.count();
print "TestingCount:[%d]" % test.count();
model = SVMWithSGD.train(training, 100)
scoreAndLabels = test.map(lambda point : (model.predict(point.features), point.label))
#输出结果,包含预测的数字结果和0/1结果:
for score, label in scoreAndLabels.collect():
print score, label

参考资料:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。