Кластерный анализ (k-means) на python

Кластерный анализ (k-means) на python

k-means, Python. Python , C , Java Python . , , k-means . , , . , : k means, . get_data , file open, numpy.array map . , . Python 3.x. : import numpy as np def get_data: source row.strip.split; for row in openexport.csv names row0 for row in source1: data mapfloat, row1: for row in source1: return names, normdata def normdata: matrix np.arraylistdata, f len_val lenmatrix1, : for i in rangelen_val: local_min matrix:, i.min if local_min 0.0: matrix:, i - local_min local_max matrix:, i.max if local_max 0.0: matrix:, i local_max return matrix.tolist if __name__ __main__: get_data

- Pandas SciKit Learn . : import pandas as pd from sklearn.cluster import KMeans from sklearn.preprocessing import MinMaxScaler, StandardScaler read data drop last empty column, caused by an extra last colon in the header data pd.read_csvfilename, sep;.dropnaaxis1, howall normalize data X MinMaxScaler.fit_transformdata.drop, 1 clustering n_clusters 3 km KMeansn_clustersn_clusters fit predict clusters datacluster km.fit_predictX results - we should have 3 clusters: 0,1,2 printdata clusters centroids printkm.cluster_centers_ : In 47: data Out47: 1 2 3 4 5 6 7 8 9 10 11 0 91.76.155.87 2 2 1 5 3 4 1 1 2 2 1 1 188.123.252.12 2 1 1 6 6 1 2 1 2 3 4 2 46.242.13.93 3 3 1 4 1 2 5 3 1 5 1 3 94.19.223.144 2 2 1 5 6 4 1 1 1 2 4 4 46.39.46.15 2 2 2 5 4 3 3 4 3 2 4 12 13 14 15 16 17 18 cluster 0 3 5 4 5 6 6 4 2 1 5 3 3 4 5 5 5 0 2 5 4 5 5 5 6 4 1 3 5 2 4 6 6 6 5 2 4 4 4 3 3 4 5 3 0 In 48: printkm.cluster_centers_ 0. 0.25 0.5 0.75 0.8 0.33333333 0.375 0.5 0.75 0.16666667 1. 0.75 0.5 0. 0.16666667 0.25 0. 0.5 1. 1. 0. 0. 0. 0.33333333 1. 0.66666667 0. 1. 0. 1. 0.66666667 1. 0.66666667 0.5 1. 0.5 0. 0.5 0. 0.5 0.7 1. 0. 0. 0.25 0. 0.5 0.5 0.5 0.5 0.83333333 1. 1. 0.75

Комментарии

Популярные сообщения из этого блога

Skipping acquire of configured file 'contrib/binary-i386/Packages' as repository … doesn't support architecture 'i386'

Connection string for MariaDB using ODBC

Celery like system based on django channels