|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pandas as pd
import numpy as np
d = {'Gender':['male','male','male','male','female','female'],
'Height':[6.00,5.92,5.58,5.92,5.00,5.50],
'Weight':[180,190,170,165,100,150],
'Size':[12,11,12,10,6,8],
'Team':['i100','i100','i500','i100','i500','i100']}
data=pd.DataFrame(d)
n_i100 = data['Team'][data['Team'] == 'i100'].count()
n_i500 = data['Team'][data['Team'] == 'i500'].count()
total_ppl = data['Team'].count()
P_i100 = n_i100 * 1.0 / total_ppl
P_i500 = n_i500 * 1.0 / total_ppl
df1 = data.groupby(['Team', 'Gender']).size().rename('cnt').reset_index().set_index('Team')
df2 = pd.DataFrame(data.groupby(['Team']).size().rename('total'))
df3 = df1.merge(df2, left_index=True, right_index=True)
df3['p'] = df3['cnt'] * 1.0 / df3['total']
data_means = data.groupby('Team').mean()
data_variance = data.groupby('Team').var()
i100_height_mean = data_means['Height'][data_means.index == 'i100'].values[0]
i100_weight_mean = data_means['Weight'][data_means.index == 'i100'].values[0]
i100_size_mean = data_means['Size'][data_means.index == 'i100'].values[0]
i100_height_variance = data_variance['Height'][data_variance.index == 'i100'].values[0]
i100_weight_variance = data_variance['Weight'][data_variance.index == 'i100'].values[0]
i100_size_variance = data_variance['Size'][data_variance.index == 'i100'].values[0]
i500_height_mean = data_means['Height'][data_means.index == 'i500'].values[0]
i500_weight_mean = data_means['Weight'][data_means.index == 'i500'].values[0]
i500_size_mean = data_means['Size'][data_means.index == 'i500'].values[0]
i500_height_variance = data_variance['Height'][data_variance.index == 'i500'].values[0]
i500_weight_variance = data_variance['Weight'][data_variance.index == 'i500'].values[0]
i500_size_variance = data_variance['Size'][data_variance.index == 'i500'].values[0]
def p_x_given_y_1(team, gender):
return df3['p'][df3['Team '] ==team][df3['Gender'] == gender].values[0]
def p_x_given_y_2(x, mean_y, variance_y):
p = 1 / (np.sqrt(2 * np.pi * variance_y)) * np.exp((-(x - mean_y) **2) / (2 * variance_y))
return p
per ={'Gender':['female'],
'Height':[6.00],
'Weight':[130],
'Size':[8]
}
person=pd.DataFrame(per)
P_i100 * p_x_given_y_1('i100', person['Gender'][0]) *\
p_x_given_y_2(person['Height'][0], i100_height_mean, i100_height_variance) *\
p_x_given_y_2(person['Weight'][0], i100_weight_mean, i100_weight_variance) *\
p_x_given_y_2(person['Size'][0], i100_size_mean, i100_size_variance)
问题
raceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2895, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Team '
求解 |
|