大数据行业十分火热,本人认为python是比较强大的分析工具,在网易云课堂上学习了python数据分析。做了案例,写下代码分析过程以及分析结论。
以下是婚恋配对的python数据分析项目。
该项目有三大策略:
1,男才女貌 2,门当户对 3,志趣相投,适度引领
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 13 19:07:45 2018
@author: Administrator
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
from bokeh.plotting import figure,show,output_file
from bokeh.models import ColumnDataSource,HoverTool
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
data_norm=pd.DataFrame({"正态分布":np.random.normal(loc=60,scale=15,size=1000)})
data_exp=pd.DataFrame({"指数分布":np.random.exponential(scale=15,size=1000)+45})
fig,axes=plt.subplots(1,2,figsize=(12,4))
data_norm.hist(bins=50,ax=axes[0])#外貌和性格分布图
data_exp.hist(bins=50,ax=axes[1])#财富分布图
#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
def create_sample(n,gender):
sample_data=pd.DataFrame({
"fortune":np.random.exponential(scale=15,size=n)+45,
"apperence":np.random.normal(loc=60,scale=15,size=n),
"character":np.random.normal(loc=60,scale=15,size=n)},
index=[gender+str(i) for i in range(1,n+1)])
sample_data.index.name="id"
sample_data["score"]=sample_data.sum(axis=1)/3#求综合分数
return sample_data
sample_m= create_sample(10000,'m')
sample_f=create_sample(10000,'f')
fig,axes=plt.subplots(2,1,figsize=(12,8))
sample_m[["fortune","apperence","character"]].iloc[:30].plot(
kind="bar",colormap="Blues_r",stacked=True,grid=True,ax=axes[0]
)
#绘出男性三大特征的分布情况
sample_f[["fortune","apperence","character"]].iloc[:30].plot(
kind="bar",colormap="Reds_r",stacked=True,grid=True,ax=axes[1]
)
#绘出女性三大特征的分布情况
#''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#第一轮模拟
sample_m_test=create_sample(99,"m")
sample_f_test=create_sample(99,"f")
#创建男女样本
sample_m_test["strategy"]=np.random.choice([1,2,3],99)#随机选策略
#建立空数据集
match_success=pd.DataFrame(columns=["m","f","round_n","strategy"])
round1_m=sample_m_test.copy()
round1_f=sample_f_test.copy()#源数据不能被破坏
round1_m["chioce"]=np.random.choice(round1_f.index,len(round1_m))#男性选女性
round1_match=pd.merge(round1_m,round1_f,left_on="chioce",right_index=True).reset_index()
#配对但没成功
round1_match["score_dis"]=np.abs(round1_match["score_x"]-round1_match["score_y"])
round1_match["cha_dis"]=np.abs(round1_match["character_x"]-round1_match["character_y"])
round1_match["for_dis"]=np.abs(round1_match["fortune_x"]-round1_match["fortune_y"])
ro