使用Python实现K-means

写在前面:本代码用于课程作业,仅写到二维,欢迎大家进行拓展。

#定义函数:计算二维空间中两点的欧氏距离
def EuclideanDistance(x, y):
    return math.sqrt(math.pow(x[0]-y[0],2)+math.pow(x[1]-y[1],2))
#定义函数:计算一个DataFrame中点集的形心
def Centroid(df):
    Centroid_x = df['属性1'].mean()
    Centroid_y = df['属性2'].sum()/len(df)
    return [Centroid_x,Centroid_y]
#定义函数:以形心点组成的DF——’Centroid_group‘为基准,对于样本点集’df‘中的点进行聚类,并分别得到group_a/b/c三个新的点集,
#此为聚类后的三类
def Clustering(df,Centroid_group):
        group_a = np.zeros(shape = (1,2))
        group_b = np.zeros(shape = (1,2))
        group_c = np.zeros(shape = (1,2))
        for i in range(11):
            a = EuclideanDistance([df.iloc[i,0],df.iloc[i,1]],
                              [Centroid_group.iloc[0,0],Centroid_group.iloc[0,1]])
            b = EuclideanDistance([df.iloc[i,0],df.iloc[i,1]],
                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值