#P4475.第2题-终端款型聚类识别

第2题-终端款型聚类识别 - problem_ide - CodeFun2000

python 复制代码
import sys
import numpy as np

def solve():
    # 读取数据
    input_data = sys.stdin.read().strip().split()
    if not input_data:
        return

    k = int(input_data[0])
    m = int(input_data[1])
    n = int(input_data[2])

    # 将数据 reshape 成了 (m, 4) 的矩阵!
    data = np.array(input_data[3:], dtype=np.float64).reshape(m, 4)

    # 1. 初始化质心 (前 k 个点)
    centers = data[0:k].copy()

    # np.full(shape, fill_value, dtype)
    # np.zeros(shape, dtype)

    labels = np.zeros(m,dtype=int)

    for stap in range(n):
        old_centers = centers.copy()

        for i in range(m):
            distances = np.zeros(k)

            for j in range(k):
                dist = np.sqrt(np.sum((data[i]-old_centers[j] )**2))
                distances[j] = dist
            
            labels[i] = np.argmin(distances)
        
        
        # 更新质心
        for j in range(k):
            cluster = data[labels==j]

            if len(cluster)>0:
                centers[j] = np.mean(cluster,axis=0)
        
        max_shift = 0
        # 检查是否收敛 (质心不怎么动了)
        for j in range(k):
            shift = np.sqrt(np.sum((centers[j]-old_centers[j] )**2))
            if shift>max_shift:
                max_shift = shift

        if max_shift<1e-8:
            break

    counts = []
    for i in range(k):
        counts.append(np.sum(labels==i))
    
    counts.sort()
    print(" ".join(map(str,counts)))

if __name__ =='__main__':
    solve()
相关推荐
血色橄榄枝2 小时前
基于用户注册信息的关键词检测挑战赛「Datawhale AI 夏令营」
人工智能·算法·机器学习
QN1幻化引擎6 小时前
Dalin L — 我造了一门支持中文编程的语言,完整移植到 Rust 了
人工智能·算法·机器学习
Axis tech7 小时前
Manus基于手关节角度和指尖数据的Revo 3灵巧手遥操作
科技·机器学习
KaMeidebaby7 小时前
卡梅德生物技术快报|如何制备单克隆抗体:小众禽类靶点单抗制备实操流程:双载体抗原交叉筛选完整工艺记录
人工智能·python·深度学习·算法·机器学习
z小猫不吃鱼9 小时前
模型剪枝经典论文精读:DepGraph: Towards Any Structural Pruning
算法·机器学习·剪枝
中微极客9 小时前
KoMA:基于知识驱动的多智能体LLM自动驾驶框架深度解析
人工智能·机器学习·自动驾驶
YangYang9YangYan10 小时前
2026电子信息工程专业学数据分析在校招时的价值
数据挖掘·数据分析
orion-orion12 小时前
学习理论:凸代理与在线学习regret界证明
机器学习·统计学习·学习理论
richdata12 小时前
千店千面的智能配货体系:门店差异化分配怎么做
大数据·人工智能·数据挖掘
Yunzenn14 小时前
强化学习1-Liu2026_GFlowRL_精读笔记
人工智能·笔记·深度学习·机器学习·transformer·集成学习·vllm