क्लस्टर्स का विज़ुअलाइज़ेशन
आपने अभी-अभी optimum k मान (k=16) के साथ k-means मॉडल ट्रेन किया है और cluster centers (centroids) जनरेट किए हैं. इस अंतिम अभ्यास में, आप क्लस्टर्स और centroids को एक साथ ओवरले करके विज़ुअलाइज़ करेंगे. इससे पता चलेगा कि क्लस्टरिंग कितनी अच्छी रही (आदर्श रूप से, क्लस्टर्स एक-दूसरे से स्पष्ट रूप से अलग होने चाहिए और centroids अपने-अपने क्लस्टर के केंद्र में होने चाहिए).
इसे करने के लिए, आप पहले rdd_split_int RDD को Spark DataFrame में बदलेंगे, और फिर उसे plotting के लिए उपयुक्त Pandas DataFrame में कन्वर्ट करेंगे. इसी तरह, आप cluster_centers को भी Pandas DataFrame में बदलेंगे. दोनों DataFrames बनने के बाद, आप Matplotlib का उपयोग करके scatter plots बनाएँगे.
SparkContext sc, साथ ही वैरिएबल्स rdd_split_int और cluster_centers, और पैकेज matplotlib.pyplot (जिसे plt के रूप में इम्पोर्ट किया गया है) आपके workspace में उपलब्ध हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PySpark के साथ Big Data Fundamentals
अभ्यास निर्देश
rdd_split_intRDD को पहले Spark DataFrame में, फिर pandas DataFrame में कन्वर्ट करें.cluster_centerslist से एक pandas DataFrame बनाएँ.- raw data के pandas DataFrame (
rdd_split_int_df_pandas) से एक scatter plot बनाएँ, और उस पर centroids के Pandas DataFrame (cluster_centers_pandas) के scatter plot को ओवरले करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Convert rdd_split_int RDD into Spark DataFrame and then to Pandas DataFrame
rdd_split_int_df_pandas = spark.____(rdd_split_int, schema=["col1", "col2"]).toPandas()
# Convert cluster_centers to a pandas DataFrame
cluster_centers_pandas = pd.DataFrame(____, columns=["col1", "col2"])
# Create an overlaid scatter plot of clusters and centroids
plt.scatter(rdd_split_int_df_pandas["col1"], rdd_split_int_df_pandas["col2"])
plt.scatter(____["col1"], ____["col2"], color="red", marker="x")
plt.show()