Python科学恋爱大法

“你肿么啦？”我仔细地掰开蟹壳，问道。

“十一假期好多朋友办婚礼，可我男票一点要求婚的意思都没有，我都想考虑备胎了。”

“你自己和他说嘛！”我放下了金黄的大闸蟹。

“我可是个妹子，这样多不好。”学姐叹了口气。

1)参加求婚的男女数量保持一致

2)每个男子都按喜爱程度对女子进行排序，比如最爱a，其次爱b，再次爱c

3)每个女子也同样给每个男子排序

*此方法名为Gale-Shapley算法。优点如下：1. 总有大家都订了婚的一天，不可能无限循环2. 中止后所有的婚姻是稳定婚姻（不稳定婚姻：比如有两对夫妇M1&F1和M2&F2, M1的老婆是F1，但他更爱F2；而F2的老公虽说是M2，但她更爱M1。这样的婚姻就是不稳定婚姻）有兴趣的读者可以自行搜索证明过程。

（此处展示部分代码，完整源文件请看文末）



#设置男女生喜好样本
 
print('==============================生成样本数据==============================')
 
man = pd.DataFrame( [['w'+str(i) for i in random.sample(range(1,women_num+1),women_num)] \
 
 for i in range(man_num)],
 
 index = ['m'+str(i) for i in range(1,man_num+1)],
 
 columns = ['level'+str(i) for i in range(1,women_num+1)]
 
 )
 

 
women = pd.DataFrame( [['m'+str(i) for i in random.sample(range(1,man_num+1),man_num)] \
 
 for i in range(women_num)],
 
 index = ['w'+str(i) for i in range(1,women_num+1)],
 
 columns = ['level'+str(i) for i in range(1,man_num+1)]
 
 )
 
return (man,women)
 



print('==============================测试集{}模拟开始=============================='.format(i))
 
print('==============================开始模拟求婚过程==============================')
 
level_num = 0
 
while man_ismapping['love_level'].min() == 0:
 
 level_num += 1
 
 print('==============================开始第{}天婚姻配对=============================='.format(level_num))
 
 u_mapping_man = man_ismapping[man_ismapping.target == 'n'].index.tolist()
 

 
 if level_num < 2:
 
 level_col = 'level' + str(level_num)
 
 man_choose = man[man.index.isin(u_mapping_man)][level_col].to_frame().reset_index()
 
 man_choose.columns = ['man_id', 'women_id']
 
 man_choose['range'] = 1
 
 else:
 
 m_id = u_mapping_man
 
 l = []
 
 for man_id in m_id:
 
 col_n = int(man_ismapping[man_ismapping.index == man_id].range[0])
 
 level_col = 'level' + str(col_n + 1)
 
 women_id = man[man.index == man_id][level_col][0]
 
 rg = col_n + 1
 
 l.append([man_id, women_id, rg])
 
 man_choose = pd.DataFrame(l, columns=['man_id', 'women_id', 'range'])
 



for r in range(0, len(man_choose)):
 
 relationship = man_choose[man_choose.index == r]
 
 m = [i for i in relationship['man_id']][0]
 
 w = [i for i in relationship['women_id']][0]
 
 find = women[women.index == w].unstack().reset_index()
 
 find.columns = ['level', 'women_id', 'man_id']
 
 find = int([i for i in find[find['man_id'] == m]['level']][0].split('level')[1])
 
 o_love_level = [i for i in women_ismapping[women_ismapping.index == w]['love_level']][0]
 
 rg = [i for i in relationship['range']][0]
 
 if o_love_level == 0:
 
 women_ismapping.loc[w, 'love_level'] = find
 
 women_ismapping.loc[w, 'target'] = m
 
 women_ismapping.loc[w, 'range'] = level_num
 
 man_ismapping.loc[m, 'love_level'] = rg
 
 man_ismapping.loc[m, 'target'] = w
 
 man_ismapping.loc[m, 'range'] = rg
 
 elif o_love_level > find:
 
 m_o = women_ismapping.loc[w, 'target']
 
 man_ismapping.loc[m_o, 'love_level'] = 0
 
 man_ismapping.loc[m_o, 'target'] = 'n'
 
 man_ismapping.loc[m, 'love_level'] = rg
 
 man_ismapping.loc[m, 'target'] = w
 
 man_ismapping.loc[m, 'range'] = rg
 
 women_ismapping.loc[w, 'love_level'] = find
 
 women_ismapping.loc[w, 'target'] = m
 
 women_ismapping.loc[w, 'range'] = level_num
 
 else:
 
 man_ismapping.loc[m, 'range'] = rg
 
 pass
 

*纵轴代表其中一次模拟中，男性/女性的平均伴侣喜爱排名均值，即：匹配到的伴侣是他们/她们第X喜欢的异性。

Python中文社区

+ 订阅