การจำแนกประเภท

Big Data Fundamentals with PySpark

Upendra Devisetty

Science Analyst, CyVerse

การจำแนกประเภทด้วย PySpark MLlib

  • การจำแนกประเภท (Classification) คืออัลกอริทึม machine learning แบบ supervised ที่จัดกลุ่มข้อมูลขาเข้าออกเป็นหมวดหมู่ต่างๆ

Big Data Fundamentals with PySpark

แนะนำ Logistic Regression

  • Logistic Regression ทำนายผลลัพธ์แบบ binary จากตัวแปรที่กำหนด

Big Data Fundamentals with PySpark

การทำงานกับ Vectors

  • PySpark MLlib มีชนิดข้อมูลเฉพาะ ได้แก่ Vectors และ LabelledPoint

  • Vectors มี 2 ประเภท

    • Dense Vector: เก็บค่าทั้งหมดในอาร์เรย์ของตัวเลขทศนิยม
    • Sparse Vector: เก็บเฉพาะค่าที่ไม่เป็นศูนย์พร้อม index
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})
Big Data Fundamentals with PySpark

`LabeledPoint()` ใน PySpark MLlib

  • LabeledPoint คือ wrapper สำหรับ feature ของข้อมูลขาเข้าและค่าที่ต้องการทำนาย

  • สำหรับการจำแนกแบบ binary ใน Logistic Regression label จะมีค่าเป็น 0 (negative) หรือ 1 (positive)

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])
Big Data Fundamentals with PySpark

`HashingTF()` ใน PySpark MLlib

  • อัลกอริทึม HashingTF() ใช้แมปค่า feature ไปยัง index ใน feature vector
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})
Big Data Fundamentals with PySpark

Logistic Regression ด้วย LogisticRegressionWithLBFGS

  • การทำ Logistic Regression ด้วย PySpark MLlib ใช้คลาส 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
Big Data Fundamentals with PySpark

สไลด์สุดท้าย

Big Data Fundamentals with PySpark

Preparing Video For Download...