詹惠儿

2019-01-04   阅读量: 584

数据分析师

用python怎么做分类项目

扫码加入数据分析学习群

为了实际找到方法,我们将遍历所有项目,将它们分类到最近的集群并更新集群的均值。我们将重复该过程一定数量的迭代。如果在两次迭代之间没有项目更改分类,我们会在算法找到最佳解决方案时停止该过程。

以下函数将输入k(所需簇的数量),项目和最大迭代次数作为输入,并返回均值和簇。的项的分类存储在数组属于关联和项目的群集中的号被存储在clusterSizes

def CalculateMeans(k,items,maxIterations=100000):

# Find the minima and maxima for columns

cMin, cMax = FindColMinMax(items);

# Initialize means at random points

means = InitializeMeans(items,k,cMin,cMax);

# Initialize clusters, the array to hold

# the number of items in a class

clusterSizes= [0 for i in range(len(means))];

# An array to hold the cluster an item is in

belongsTo = [0 for i in range(len(items))];

# Calculate means

for e in range(maxIterations):

# If no change of cluster occurs, halt

noChange = True;

for i in range(len(items)):

item = items[i];

# Classify item into a cluster and update the

# corresponding means.

index = Classify(means,item);

clusterSizes[index] += 1;

cSize = clusterSizes[index];

means[index] = UpdateMean(cSize,means[index],item);

# Item changed cluster

if(index != belongsTo[i]):

noChange = False;

belongsTo[i] = index;

# Nothing changed, return

if (noChange):

break;

return means;

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 3 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子