載入並剖析 5000 個點的資料
分群是一種非監督式學習任務,用來將物件依相似度高低分成群組。不同於有標籤的監督式任務,分群可用來理解未標記的資料。PySpark MLlib 提供常見的 K-means 分群演算法。在這個分成 3 個部分的練習中,你將在一個包含 5000 列、2 欄的資料集中找出有多少個叢集。為此,你會先把資料載入成 RDD,依分隔符剖析 RDD,執行 KMeans 模型、評估模型,最後視覺化叢集。
在第一部分,你會把資料載入成 RDD,依分隔符剖析 RDD,並將字串型別的資料轉換為整數。
請記住,你的工作環境中已提供 SparkContext sc。另外,變數 file_path(指向 5000_points.txt 的路徑)也已可用。
本練習屬於課程
使用 PySpark 的 Big Data 基礎
練習說明
- 將
5000_points資料集載入為名為clusterRDD的 RDD。 - 依照定位字元("\t")拆分各行,轉換
clusterRDD。 - 進一步轉換拆分後的 RDD,為兩個欄位建立整數清單。
- 確認資料集中共有 5000 列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load the dataset into an RDD
clusterRDD = sc.____(file_path)
# Split the RDD based on tab
rdd_split = clusterRDD.____(lambda x: ____.split(____))
# Transform the split RDD by creating a list of integers
rdd_split_int = rdd_split.____(lambda x: [int(____), int(x[1])])
# Count the number of rows in RDD
print("There are {} rows in the rdd_split_int dataset".format(____.____()))