예제 1
데이터프레임 생성을 통해 베이즈 정리를 구현해보자
import pandas as pd
table = pd.DataFrame(index=['Spam', 'Ham'])
## LINK
table['prior'] = 0.5
table['likelihood'] = 0.6, 0.2
table['joint'] = table['prior'] * table['likelihood']
norm_const = table['joint'].sum()
table['posterior'] = table['joint'] / norm_const
print(table)
## WORD
table['prior'] = table['posterior']
table['likelihood'] = 0.4, 0.05 #특정 단어 W가 있는 상황을 가정
table['joint'] = table['prior'] * table['likelihood']
norm_const = table2['joint'].sum()
table['posterior'] = table['joint'] / norm_const
print(table)
예제2
함수를 선언해 출력값을 불러오자.
def bayesian_table(table, prior, likelihood):
if 'posterior' in table.columns: table['prior'] = table['posterior']
else: table['prior'] = prior
table['likelihood'] = likelihood
table['joint'] = table['prior'] * table['likelihood']
norm_const = table['joint'].sum()
table['posterior'] = table['joint'] / norm_const
return table
[문제설정]
[ex1. 공을 하나 꺼냈을 때, 검은색]
[ex2. 공을 하나 꺼냈을 때, 흰색]
pandas를 통해 Posterior를 구하면 결과는 아래와 같다
import pandas as pd
def bayesian_table(table, prior, likelihood):
table['prior'] = prior
table['likelihood'] = likelihood
table['joint'] = table['prior'] * table['likelihood']
norm_const = table['joint'].sum()
table['posterior'] = table['joint'] / norm_const
return table
table = pd.DataFrame(index=['X', 'Y'])
prior = 0.5
w_likelihood = [0.9, 0.2]
b_likelihood = [0.1, 0.8]
print("========================Black========================")
table = pd.DataFrame(index=['X', 'Y'])
b_table = bayesian_table(table, prior, likelihood=b_likelihood)
print(table, '\n')
print("========================White========================")
table = pd.DataFrame(index=['X', 'Y'])
w_table = bayesian_table(table, prior, likelihood=w_likelihood)
print(w_table, '\n')
[ex3. 공을 두 번 뽑았을 때, 검은색&검은색]
prior probability : P(X) = 0.5, P(Y) = 0.5
likelihood = P(BB | X) = 0.01
눈 앞의 단지가 X 타입일 posterior
눈 앞의 단지가 Y 타입일 posterior
[ex4. 공을 두 번 뽑았을 때, 검은색&흰색]
import pandas as pd
def bayesian_table(table, prior, likelihood):
table['prior'] = prior
table['likelihood'] = likelihood
table['joint'] = table['prior'] * table['likelihood']
norm_const = table['joint'].sum()
table['posterior'] = table['joint'] / norm_const
return table
table = pd.DataFrame(index=['X', 'Y'])
prior = 0.5
w_likelihood = [0.9, 0.2]
b_likelihood = [0.1, 0.8]
print("========================Black&Black========================")
table = pd.DataFrame(index=['X', 'Y'])
bb_table = bayesian_table(table, prior, likelihood=[likelihood**2 for likelihood in b_likelihood])
print(table, '\n')
print("========================Black&White========================")
table = pd.DataFrame(index=['X', 'Y'])
bw_table = bayesian_table(table, prior, likelihood=[likelihood*(1-likelihood) for likelihood in b_likelihood])
print(table, '\n')
학습 후 느낀점