Bu safar biz NumPy va sklearn.cluster.KMeans dan foydalanamiz: - >>> import numpy as np >>> from sklearn.cluster import KMeans >>> x = np.array([(0.0, 0.0), ... (9.9, 8.1), ... (-1.0, 1.0), ... (7.1, 5.6), ... (-5.0, -5.5), ... (8.0, 9.8), ... (0.5, 0.5)]) >>> x array([[ 0. , 0. ], [ 9.9, 8.1], [-1. , 1. ], [ 7.1, 5.6], [-5. , -5.5], [ 8. , 9.8], [ 0.5, 0.5]])
Keyingi qadam ma'lumotlarni o'lchashdir, lekin bu har doim ham zarur emas. Ma'lumotlarni oldindan qayta ishlash tugallangach, biz KMeans nusxasini yaratamiz va uni ma'lumotlarimizga moslashtiramiz: - >>> cluster_analyzer = KMeans(n_clusters=3, init='k-means++') >>> cluster_analyzer.fit() >>> cluster_analyzer.fit(x) KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300, n_clusters=3, n_init=10, n_jobs=None, precompute_distances='auto', random_state=None, tol=0.0001, verbose=0)
Shunday qilib, biz klasterlar markazlarining koordinatalari va har bir kuzatuv tegishli bo'lgan klasterlarning yorliqlari kabi natijalarni olishga tayyormiz: - >>> cluster_analyzer.cluster_centers_ array([[ 8.33333333, 7.83333333], [-0.16666667, 0.5], [-5. , -5.5 ]]) >>> cluster_analyzer.labels_ array([1, 0, 1, 0, 2, 0, 1], dtype=int32)
E’TIBORINGIZ UCHUN RAHMAT!
Do'stlaringiz bilan baham: |