我的代码是,我有一些困难使它正常运行。
import scipy.special as sps
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import norm
from scipy.stats import gamma
from math import exp
########################################
### DADOS
dados= [2.3572833,0.7383197,14.1423990,2.0310423,7.1052727,1.8851099,12.9464459,4.4056236,1.0471756,0.4672236]
temp = pd.DataFrame(dados)
##########################################
def fx(x, t):
prod = 1.0
for i in range(0, len(x)):
prod *= ((t[0]/t[1])* exp(- (x[i]/t[1]) ) * exp(-t[0] * exp(-(x[i]/t[1]) ) ) )
return prod
#########################
def L(x, t):
n = len(x)
return fx(x,t)
##########################################
### MCMC
def mcmc(N=[], k={"t1": 1, "t2": 1}, x=[]):
chute = {"t1": [1], "t2": [1]}
M = chute
hiper = {"t1": [0.1, 0.1], "t2": [0.1, 0.1]}
contador = {"t1": [], "t2": []}
thetas = M.keys()
for i in range(N - 1):
for j in thetas:
if j == "t1":
M[j].append( np.random.gamma(shape = M[j][-1], scale = k[j]) )
lista = [ [ M[l][-1] for l in thetas] , [ M[l][-1] if l!=j else M[l][-2] for l in thetas ] ]
t1 = gamma.pdf(M[j][-1], a = hiper[j][0], scale = hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2], a = M[j][-1], scale = k[j])
t2 = gamma.pdf(M[j][-2], a = hiper[j][0], scale = hiper[j][1]) * L(x, lista[1]) * gamma.pdf(M[j][-1], a = M[j][-2], scale = k[j])
teste = (t1/t2)
else:
M[j].append( np.random.gamma(shape = M[j][-1], scale = k[j]) )
lista = [ [ M[l][-1] for l in thetas] , [ M[l][-1] if l!=j else M[l][-2] for l in thetas ] ]
t1 = gamma.pdf(M[j][-1], a = hiper[j][0], scale = hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2], a = M[j][-1], scale = k[j])
t2 = gamma.pdf(M[j][-2], a = hiper[j][0], scale = hiper[j][1]) * L(x, lista[1]) * gamma.pdf(M[j][-1], a = M[j][-2], scale = k[j])
teste = (t1/t2)
if (min(1, teste) < np.random.uniform(low=0, high=1)) or (np.isinf(teste)) or (np.isnan(teste)):
M[j][-1] = M[j][-2]
contador[j].append(0)
else:
contador[j].append(1)
M = pd.DataFrame.from_dict(M)
contador = pd.DataFrame.from_dict(contador)
cont = contador.apply(sum)
print(cont)
return (M)
N = int(input("Entre com o N: "))
MP = mcmc(N=N, x=temp)
print(MP)
它会产生一个我无法解决的错误。
Traceback (most recent call last):
File "teste2.py", line 92, in <module>
MP = mcmc(N=N, x=temp)
File "teste2.py", line 71, in mcmc
t1 = gamma.pdf(M[j][-1], a = hiper[j][0], scale = hiper[j][1]) * L(x, lista[0]) * gamma.pdf(M[j][-2], a = M[j][-1], scale = k[j])
File "teste2.py", line 40, in L
return fx(x,t)
File "teste2.py", line 32, in fx
prod *= ((t[0]/t[1])* exp(- (x[i]/t[1]) ) * exp(-t[0] * exp(-(x[i]/t[1]) ) ) )
File "/home/karlla/anaconda2/lib/python2.7/site-packages/pandas/core/series.py", line 78, in wrapper
"{0}".format(str(converter)))
TypeError: cannot convert the series to <type 'float'>
我已经尝试过在互联网上发现的一些事情但它没有用。有谁知道我怎么解决这个问题?
答案 0 :(得分:1)
使用print()
在功能
def fx(x, t):
prod = 1.0
print('x:', type(x), x)
print('t:', type(t), t)
for i in range(0, len(x)):
print('i:', i)
print('x[i]:', type(x[i]), x[i])
#print('x[0][i]:', type(x[0][i]), x[0][i])
a = t[0]/t[1]
print('a:', type(a), a)
b = -(x[i]/t[1])
print('b:', type(b), b)
exp_b = exp(b)
print('exp(b):', type(exp_b), exp_b)
c = -t[0]
print('c:', type(c), c)
prod *= a * exp_b * exp(c * exp_b)
print('prod:', type(prod), prod)
print('exit fx')
return prod
我不知道你尝试做什么,也许我的结果是错误的。
它显示pandas
的问题,因为您有二维DataFrame
,但您将其视为一维list
x
是DataFrame
。
执行x[i]
时,您可能希望列中的第i个元素 - 换句话说element in row number "i"
,但对于pandas x[i]
,则get column with number/name 'i'
表示x[0]
不是从列中给出第一个元素,而是在"0"
中给出完整列(系列)中的数字/名称。 (但同时len(x)
给出了行数,而不是列数)
所以x[i]
是Series
,你有
exp(Series)
并且Python不知道如何将Series
转换为float
并且您收到错误。
您需要x[0][i]
从列“0”`
def fx(x, t):
prod = 1.0
print('x:', type(x), x)
print('t:', type(t), t)
for i in range(len(x)):
print('i:', i)
print('x[0][i]:', type(x[0][i]), x[0][i])
a = t[0]/t[1]
print('a:', type(a), a)
b = -(x[0][i]/t[1])
print('b:', type(b), b)
exp_b = exp(b)
print('exp(b):', type(exp_b), exp_b)
c = -t[0]
print('c:', type(c), c)
prod *= a * exp_b * exp(c * exp_b)
print('prod:', type(prod), prod)
print('exit fx')
return prod
更短
def fx(x, t):
prod = 1.0
for i in range(len(x)):
a = t[0]/t[1]
b = -(x[0][i]/t[1])
exp_b = exp(b)
c = -t[0]
prod *= a * exp_b * exp(c * exp_b)
return prod
更好 - 没有range(len())
def fx(x, t):
prod = 1.0
a = t[0]/t[1]
c = -t[0]
for val in x[0]:
b = -(val/t[1])
exp_b = exp(b)
prod *= a * exp_b * exp(c * exp_b)
return prod
之后这个功能没有问题 - 但仍有其他问题,但我不知道你尝试做什么,也无法帮助更多。