使用 PySpark 的 Big Data 基礎
Upendra Devisetty
Science Analyst, CyVerse


PySpark MLlib 提供特定資料型別:Vectors 與 LabeledPoint。
Vectors 有兩種型式:
denseVec = Vectors.dense([1.0, 2.0, 3.0])
DenseVector([1.0, 2.0, 3.0])
sparseVec = Vectors.sparse(4, {1: 1.0, 3: 5.5})
SparseVector(4, {1: 1.0, 3: 5.5})
LabeledPoint 是包裝輸入特徵與標記值的結構。
在邏輯斯迴歸的二元分類中,標記為 0(負類)或 1(正類)。
positive = LabeledPoint(1.0, [1.0, 0.0, 3.0])
negative = LabeledPoint(0.0, [2.0, 1.0, 1.0])
print(positive)
print(negative)
LabeledPoint(1.0, [1.0,0.0,3.0])
LabeledPoint(0.0, [2.0,1.0,1.0])
HashingTF() 演算法用來將特徵值對應到特徵向量中的索引。from pyspark.mllib.feature import HashingTF
sentence = "hello hello world"
words = sentence.split()
tf = HashingTF(10000)
tf.transform(words)
SparseVector(10000, {3065: 1.0, 6861: 2.0})
LogisticRegressionWithLBFGS 進行邏輯斯迴歸。data = [
LabeledPoint(0.0, [0.0, 1.0]),
LabeledPoint(1.0, [1.0, 0.0]),
]
RDD = sc.parallelize(data)
lrm = LogisticRegressionWithLBFGS.train(RDD)
lrm.predict([1.0, 0.0])
lrm.predict([0.0, 1.0])
1
0
使用 PySpark 的 Big Data 基礎