Fundamentos de Big Data com PySpark
Upendra Devisetty
Science Analyst, CyVerse


O PySpark MLlib tem tipos próprios: Vectors e LabeledPoint
Dois tipos de 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 é um wrapper para features de entrada e o rótulo previsto
Em classificação binária com Regressão Logística, o rótulo é 0 (negativo) ou 1 (positivo)
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() mapeia valores de features para índices no vetor de featuresfrom 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})
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
Fundamentos de Big Data com PySpark