[英]TypeError: cannot convert the series to <type 'float'> ( need to take log)
[英]TypeError: cannot convert the series to <type 'float'>
我的代码是,我很难使其正确运行。
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'>
我已经尝试做一些我在互联网上发现的事情,但是它没有用。 有谁知道我该如何解决?
使用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"
中的其他单词element in row number "i"
但是对于熊猫来说, 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”中获取第i个元素
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
之后,此功能没有问题-但仍然存在其他问题,但我不知道您会尝试做什么并且无法提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.