鱼C论坛

 找回密码
 立即注册
查看: 1364|回复: 5

贝叶斯

[复制链接]
发表于 2021-12-3 00:40:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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 '
求解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-3 10:29:21 | 显示全部楼层
数学战5渣渣,
但是,
报错信息提示你键值错误
请根据你所学的,验证字典键值为: 'Team' 的使用地方,是否异常.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-12-3 20:47:51 | 显示全部楼层
z5560636 发表于 2021-12-3 10:29
数学战5渣渣,
但是,
报错信息提示你键值错误

使用地方
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-3 21:35:39 | 显示全部楼层
z5560636 发表于 2021-12-3 10:29
数学战5渣渣,
但是,
报错信息提示你键值错误

找不到问题啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-3 23:17:15 | 显示全部楼层

p_x_given_y_1这个函数里的返回的 'Team '  错了,多了个空格!!!
不信你自己在当前页面Ctrl+F查一下,到底是 'Team',还是 'Team ' ?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-4 16:27:24 | 显示全部楼层
你的df3没有team这一列,df1,df2也没有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 17:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表