def run(): user_loc = localload.load(user_info_path); rec = {}; for loc in user_loc: name = user_loc[loc][1]; rec[name] = rec.get(name,0)+1; # print(loc,user_loc[loc]); for name in rec: print(name,rec[name]); Y = []; sli = sorted(rec.items(), key=lambda p:p[1], reverse=True); for i,item in enumerate(sli): print(i,item); Y.append(item[1]); Y = np.array(Y); X = np.arange(len(Y)); plt.bar(X, Y); plt.show();
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) res = {} res1 = {} for serid in ser_loc: sn = ser_loc[serid][1] if sn not in res: res[sn] = 0 res[sn] = res[sn] + 1 an = ser_loc_m[sn][0] if an not in res1: res1[an] = 0 res1[an] = res1[an] + 1 res = sorted(res.items(), key=lambda p: p[1], reverse=True) Y = [] for i, item in enumerate(res): print(i, item) Y.append(item[1]) Y = np.array(Y) X = np.arange(len(Y)) plt.bar(X, Y) plt.ylabel('number of services') plt.show() print()
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) ser_name = localload.load_location_name(ser_info_path) oridata = np.loadtxt(origin_path, np.float) _, s = np.where(oridata) res = {} res1 = {} for sn in ser_name: res[sn] = 0 res1['SA'] = 0 res1['A'] = 0 res1['O'] = 0 res1['E'] = 0 res1['NA'] = 0 res1['ME'] = 0 res1['SAF'] = 0 for sid in s: sn = ser_loc[sid][1] res[sn] = res[sn] + 1 an = ser_loc_m[sn][0] res1[an] = res1[an] + 1 for i in res: print(i, res[i]) print() for i in res1: print(i, res1[i]) pass
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) os.remove(loc_class_out) data = [] names = [] area = [] k = 5 for sn in ser_loc_m: data.append(ser_loc_m[sn][1]) names.append(sn) area.append(ser_loc_m[sn][0]) data = np.array(data) # np.random.shuffle(data); cent, res = simple_km(data, k) print(cent) print(res) for i in range(k): tmp = [] tmp2 = [] for id in res[i]: tmp.append(area[id]) tmp2.append(names[id]) print(tmp) print(tmp2) print() classf(tmp2, ser_loc) pass
def run(): ser_loc = localload.load(ser_info_path); ser_loc_m = localload.load_locmore(ser_info_more_path); os.remove(loc_class_out); data=[]; names=[]; area=[]; k=3; for sn in ser_loc_m: data.append(ser_loc_m[sn][1]); names.append(sn); area.append(ser_loc_m[sn][0]) data=np.array(data); cent,res = simple_km(data,k); print(cent); print(res); for i in range(k): tmp=[]; tmp2=[]; for id in res[i]: tmp.append(area[id]); tmp2.append(names[id]); print(tmp) print(tmp2); print(); classf(tmp2,ser_loc); pass;
def run(): ser_loc = localload.load(ser_info_path); ser_loc_m = localload.load_locmore(ser_info_more_path); user_class = localtools.load_classif(loc_class_for_user); R = np.loadtxt(origin_path,np.float); if os.path.isfile(loc_class_out): os.remove(loc_class_out); idx = np.where(R<0); R[idx]=0; user_mean = []; for uc in user_class: UR= R[uc]; ser_sum = np.sum(UR,axis=0); ser_cot = np.count_nonzero(UR, axis=0); uc_ser_mean = np.divide(ser_sum,ser_cot, out=np.zeros_like(ser_sum),where=ser_cot!=0); all_mean = np.sum(ser_sum)/np.sum(ser_cot); uc_ser_mean[np.where(ser_cot==0)] = all_mean; user_mean.append(uc_ser_mean); data=[]; names=[]; area=[]; k=6; for sid in range(5825): sn = ser_loc[sid][1]; names.append(sn); area.append(ser_loc_m[sn][0]) lc = []; lc.extend(ser_loc_m[sn][1]); for um in user_mean: lc.append(um[sid]); data.append(lc); data=np.array(data); cent,res = simple_km2(data,k,1); print(cent); print(res); for i in range(k): tmp=[]; tmp2=[]; for id in res[i]: if names[id] not in tmp2: tmp2.append(names[id]); tmp.append(area[id]); print(tmp) print(tmp2); print(); write2file(res); pass;
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) R = np.loadtxt(origin_path, np.float) os.remove(loc_class_out) idx = np.where(R < 0) R[idx] = 0 ser_sum = np.sum(R, axis=0) ser_cot = np.count_nonzero(R, axis=0) ser_mean = np.divide(ser_sum, ser_cot, out=np.zeros_like(ser_sum), where=ser_cot != 0) all_mean = np.sum(ser_sum) / np.sum(ser_cot) ser_mean[np.where(ser_cot == 0)] = all_mean data = [] names = [] area = [] k = 8 for sid in range(5825): sn = ser_loc[sid][1] names.append(sn) area.append(ser_loc_m[sn][0]) lc = [] lc.extend(ser_loc_m[sn][1]) lc.append(ser_mean[sid]) data.append(lc) data = np.array(data) # np.random.shuffle(data); cent, res = simple_km(data, k, 8) print(cent) print(res) for i in range(k): tmp = [] tmp2 = [] for id in res[i]: if names[id] not in tmp2: tmp2.append(names[id]) tmp.append(area[id]) print(tmp) print(tmp2) print() write2file(res) pass
def run(): ser_loc = localload.load(ser_info_path) user_loc = localload.load_userinfo(user_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) user_class = localtools.load_classif(loc_class_for_user) R = np.loadtxt(origin_path, np.float) # os.remove(loc_class_out); idx = np.where(R > 0) u = idx[0].astype(np.int) s = idx[1].astype(np.int) dataize = len(u) data = [] names = [] area = [] k = 8 for sid in range(5825): sn = ser_loc[sid][1] names.append(sn) area.append(ser_loc_m[sn][0]) for did in range(dataize): sn = ser_loc[s[did]][1] cl = [] cl.extend(user_loc[u[did]][2]) cl.extend(ser_loc_m[sn][1]) cl.append(R[u[did], s[did]]) data.append(cl) data = np.array(data) cent, res = simple_km(data, k, 6) print(cent) # print(res); for i in range(k): tmp = [] tmp2 = [] for id in res[i]: if names[id] not in tmp2: tmp2.append(names[id]) tmp.append(area[id]) print(tmp) print(tmp2) print() write2file(res) pass
def run(): ser_loc = localload.load(ser_info_path); ser_loc_m = localload.load_locmore(ser_info_more_path); res={}; res1={}; for serid in ser_loc: sn = ser_loc[serid][1]; if sn not in res: res[sn]=0; res[sn]=res[sn]+1; an = ser_loc_m[sn][0]; if an not in res1: res1[an]=0; res1[an]=res1[an]+1; for i in res: print(i,res[i]); print(); for i in res1: print(i,res1[i]); pass;
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) user_class = localtools.load_classif(loc_class_for_user) R = np.loadtxt(origin_path, np.float) idx = np.where(R < 0) R[idx] = 0 user_mean = [] for uc in user_class: UR = R[uc] ser_sum = np.sum(UR, axis=0) ser_cot = np.count_nonzero(UR, axis=0) uc_ser_mean = np.divide(ser_sum, ser_cot, out=np.zeros_like(ser_sum), where=ser_cot != 0) all_mean = np.sum(ser_sum) / np.sum(ser_cot) uc_ser_mean[np.where(ser_cot == 0)] = all_mean user_mean.append(uc_ser_mean) data = [] names = [] area = [] k = 6 di = 3 for sid in range(5825): sn = ser_loc[sid][1] names.append(sn) area.append(ser_loc_m[sn][0]) lc = [] lc.extend(ser_loc_m[sn][1]) for um in user_mean: lc.append(um[sid]) data.append(lc) data = np.array(data) # np.random.shuffle(data); cent, res, dis_rate = simple_km(data, k, di) print(cent) print(res) for i in range(k): tmp = [] tmp2 = [] for id in res[i]: if names[id] not in tmp2: tmp2.append(names[id]) tmp.append(area[id]) print(tmp) print(tmp2) print() # 计算类别距离 dis_rate = 1 / dis_rate print(dis_rate) print(np.sort(dis_rate, axis=1)) dis_rate = np.exp(dis_rate) dis_sum = np.sum(dis_rate, axis=0) dis_rate /= dis_sum print(dis_rate) print(np.sort(dis_rate, axis=1)) np.savetxt(loc_class_dis_rate_out, dis_rate, '%.8f') os.remove(loc_class_out) write2file(res) pass
def run(): ser_loc = localload.load(ser_info_path) ser_loc_m = localload.load_locmore(ser_info_more_path) user_class = localtools.load_classif(loc_class_for_user) R = np.loadtxt(origin_path, np.float) if os.path.isfile(loc_class_out): os.remove(loc_class_out) idx = np.where(R < 0) R[idx] = 0 user_mean = [] for uc in user_class: UR = R[uc] ser_sum = np.sum(UR, axis=0) ser_cot = np.count_nonzero(UR, axis=0) uc_ser_mean = np.divide(ser_sum, ser_cot, out=np.zeros_like(ser_sum), where=ser_cot != 0) all_mean = np.sum(ser_sum) / np.sum(ser_cot) uc_ser_mean[np.where(ser_cot == 0)] = all_mean user_mean.append(uc_ser_mean) data = [] names = [] area = [] k = 6 for sid in range(5825): sn = ser_loc[sid][1] names.append(sn) area.append(ser_loc_m[sn][0]) lc = [] lc.extend(ser_loc_m[sn][1]) # 添加ip lc.extend(ser_loc[sid][2][:2]) for um in user_mean: lc.append(um[sid]) data.append(lc) data = np.array(data) cent, res = simple_km2(data, k, 1) print(cent) print(res) for i in range(k): tmp = [] tmp2 = [] for id in res[i]: if names[id] not in tmp2: tmp2.append(names[id]) tmp.append(area[id]) print(tmp) print(tmp2) print() write2file(res) pass