k-means क्लस्टरिंग के बेसिक्स

Python में क्लस्टर विश्लेषण

Shaumik Daityari

Business Analyst

k-means क्लस्टरिंग क्यों?

  • हाइअरार्किकल क्लस्टरिंग की बड़ी कमी: रनटाइम
  • K-means बड़े डेटासेट पर काफ़ी तेज़ चलता है
Python में क्लस्टर विश्लेषण

कदम 1: क्लस्टर सेंटर बनाइए

kmeans(obs, k_or_guess, iter, thresh, check_finite)
  • obs: standardized observations
  • k_or_guess: क्लस्टर्स की संख्या
  • iter: iterations की संख्या (डिफॉल्ट: 20)
  • thres: threshold (डिफॉल्ट: 1e-05)
  • check_finite: क्या observations में केवल finite numbers हैं, यह जाँचे (डिफॉल्ट: True)

दो ऑब्जेक्ट लौटाता है: cluster centers, distortion

Python में क्लस्टर विश्लेषण

Distortion कैसे निकाला जाता है?

Python में क्लस्टर विश्लेषण

कदम 2: क्लस्टर लेबल बनाइए

vq(obs, code_book, check_finite=True)
  • obs: standardized observations
  • code_book: cluster centers
  • check_finite: क्या observations में केवल finite numbers हैं, यह जाँचे (डिफॉल्ट: True)

दो ऑब्जेक्ट लौटाता है: cluster labels की list, distortions की list

Python में क्लस्टर विश्लेषण

Distortions पर नोट

  • kmeans एक single distortion value लौटाता है
  • vq distortions की एक list लौटाता है.
Python में क्लस्टर विश्लेषण

k-means चलाना

# Import kmeans and vq functions
from scipy.cluster.vq import kmeans, vq
# Generate cluster centers and labels
cluster_centers, _ = kmeans(df[['scaled_x', 'scaled_y']], 3)
df['cluster_labels'], _ = vq(df[['scaled_x', 'scaled_y']], cluster_centers)
# Plot clusters
sns.scatterplot(x='scaled_x', y='scaled_y', hue='cluster_labels', data=df)
plt.show()
Python में क्लस्टर विश्लेषण

Python में क्लस्टर विश्लेषण

अगले: अभ्यास!

Python में क्लस्टर विश्लेषण

Preparing Video For Download...