Stanford CS246 homework of NTHU-CS-MDA lecture ( K-means )

concept

c1 : 10个cluster 的 起点, 随机起点

c2 : 10个cluster 的 起点, 很远的起点

data : 所有数据,最长维度 == 233

使用mac

  • 注意hadoop 只能用 os 本身的 python, 我裝了 anaconda ,需要先移開,並在 os python pip numpy 才能用

  • 記得在文件開頭加上 #!/usr/bin/env python

  • 要使用文件名稱的話,加上

    try:
        input_file = os.environ['mapreduce_map_input_file']
    except KeyError:
        input_file = os.environ['map_input_file']
    
  • debug 流程,寫在知乎了。
    https://zhuanlan.zhihu.com/p/49264405

implement

mapper.py

#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import fileinput
import numpy as np
import logging


try:
    input_file = os.environ['mapreduce_map_input_file']
except KeyError:
    input_file = os.environ['map_input_file']


c1 , c2 , data , vocab , newc1,newc2,newdata= [] , [] , [] , [] , [] , [] , [] 


for line in fileinput.input(): # dui

    # bu neng you \n de a xiong di
    if "c1.txt" in input_file:
    	print("%s\t111"%(line.strip()))
    if "c2.txt" in input_file:
    	print("%s\t222"%(line.strip()))
    if "data.txt" in input_file:
    	print("%s\t333"%(line.strip()))
    if "vocab.txt" in input_file:
    	print("%s\t444"%(line.strip()))

reducer.py

#!/usr/bin/env python
# coding:utf-8
from __future__ import print_function
import fileinput
import ast
import numpy as np
# np.set_printoptions(threshold=np.inf,linewidth=200)
from tqdm import tqdm

def get_loss(init,dist = "euclidean"):

    #  c1 , c2         随机起点 ,远起点
    costs_=[]
    MAX_ITER = 20
    
    for itr in  range(MAX_ITER)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值