大数据 爬虫Python AI SqlPython小哥哥

Python科学恋爱大法!保准你今天脱单!

2019-04-30  本文已影响0人  14e61d025165
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935786" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935792" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

夏天快到了,又是吃小龙虾的季节。白富美学姐前两天约我吃最爱的小龙虾,席间向来开心的她却显得心事重重。

Python学习交流群:1004391443,有大牛答疑,有资源共享!有想学习python编程的,想提升自己能力的,欢迎加入讨论学习。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935798" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

“你肿么啦?”我仔细地掰开虾壳,问道。

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

“你自己和他说嘛!”我放下了油亮亮的小龙虾。

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

看着学姐犹犹豫豫的样子就知道她需要鼓励了~我眉头一皱,计上心来:

学姐呀,我来给你讲个故事~

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935807" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

有一座城市,当地风俗是,想结婚的男子必须先向心仪的女子求婚,而女子则需要等待求婚。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935814" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

牧师每年会邀请人数相同的适婚男女参与一次集体相亲。一次相亲活动可能有很多轮,男子会首先向自己最爱的女子求婚,女子则会在所有的追求者中选择她的最爱;如果男子被拒绝,下一轮会向他第二喜欢的女子求婚;上一轮已经订婚的女子如果得到她更爱的人的求婚,则会毫不留情地抛弃未婚夫,和更爱的人在一起。被抛弃的男子需要重新参与求婚。

如此反复,等大家都订婚,就举办集体婚礼。

假设:1)参加求婚的男女数量保持一致2)每个男子都按喜爱程度对女子进行排序,比如最爱a,其次爱b,再次爱c3)每个女子也同样给每个男子排序

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935820" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

*此方法名为Gale-Shapley算法。优点如下:

1. 总有大家都订了婚的一天,不可能无限循环2. 中止后所有的婚姻是稳定婚姻(不稳定婚姻:比如有两对夫妇M1&F1和M2&F2, M1的老婆是F1,但他更爱F2;而F2的老公虽说是M2,但她更爱M1。这样的婚姻就是不稳定婚姻)有兴趣的读者可以自行搜索证明过程。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935827" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

学姐的眼睛开始放光了:

坐在家里等求婚,下辈子请让我生在这个城市吧!

我神秘一笑:

是不是女孩子的天堂,让我们用Python来看一看最后的匹配满意度吧!

(此处展示部分代码,完整源文件请看文末)

一、样本生成

为了完成模拟过程,我们首先需要一些样本,即随机生成数量相等(可设置数量)的男性和女性,同时生成他们对每个异性个体的喜爱排名

设置男女生喜好样本

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

四、运行代码,并导出结果

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935842" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935849" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

学姐看着正在运行模拟过程有点着急了,我抽取其中一轮的结果先展示给她看:

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935855" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

纵轴代表该次模拟结果中,某位男性/女性的伴侣喜爱排名,即:匹配到的伴侣是他/她第X喜欢的异性。

显然,男性匹配到的伴侣离自己的最爱比女性更近——不止一点点!这可和“妹子天堂”的预期有点远啊!

学姐陷入了困惑:

明明女性才掌握着订婚的决定权,而且男性即使暂时订婚成功,也有被抛弃的可能啊!你看,随着匹配轮次增加,

男性的伴侣总是从自己最喜欢的对象慢慢变成不那么喜欢的对象

而女性伴侣却在一步步变好

。你这个会不会是偶然啊!

我:

是不是偶然,让我们来看看

100次模拟中,男性/女性匹配到的伴侣喜爱排名均值分布吧~

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935865" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

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

可以明显看到,男性最终匹配到的伴侣的喜爱排名普遍高于女性。

学姐终于收起了质疑

原来主动出击真有这么大的作用!为什么呀~

我分析道

:学姐你看,女孩只能坐在家里等求婚,她喜欢的人可能连看到她的机会都没有。而

男性在主动做出选择之时,每次都能选择自己最喜欢的对象

。即使被拒绝,他的下一次求婚,也能送给可能接受他的、他最爱的女孩儿。这个游戏

保证男孩的伴侣是可能和他在一起的最好的那一个

,但对女孩就不是。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556608935870" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

学姐:

你说得对。

面对自己喜欢的人,我应该去争取,而不只是等待。

我:这就对啦!世界上有什么事不需要努力呢?你有喜欢的人,就要好好努力,提升自己,去吸引ta,爱ta~

上一篇 下一篇

猜你喜欢

热点阅读