Typeerror: a float is required

Asked

Viewed 229 times

3

This is my code:

### BIBLIOTECAS

import scipy.special as sps
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
from scipy.stats import norm
from scipy.stats import gamma
from math import exp
import operator
import csv


########################################

############### GERAR VALORES DA DISTRIBUIO GE ################

def rge(n, alpha, beta):
    u = np.random.uniform(low=0, high=1, size=n)
    x = -beta * np.log((-1 / alpha) * np.log(u))
    return (x)


teste = rge(100, 1, 3)
plt.hist(teste)
plt.show()

temp = pd.DataFrame(teste)


########################################

### FUNCAO DE DISTRIBUICAO

#   t[0] = alfa
#   t[1] = beta

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

##########################################

##### FUNCAO DE VEROSSIMILHANCA


def L(x, t):
    return fx(x, t)

##########################################

### FUNCAO MCMC

def mcmc(N, k={"t0": 1, "t1": 1}, x=[]):
    chute = {"t0": [1], "t1": [1]}
    M = chute
    hiper = {"t0": [0.1, 0.1], "t1": [0.1, 0.1]}  # VALORES DOS HIPERPARAMETROS
    contador = {"t0": [], "t1": []}  # CONTADOR PARA TAXA DE ACEITACAO

    thetas = M.keys()
    for i in range(N - 1):
        for j in thetas:

            if j == "t0":

                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],

                                                                                                           loc=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)

    print("Tamanho do theta 0 : " + str(len(M["t0"])))
    print("\nTamanho do theta 1 : " + str(len(M["t1"])))

    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)

And it’s generating the following error:

Traceback (most recent call last):
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 116, in <module>
    MP = mcmc(N=N, x=temp)
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 74, 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],
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 53, in L
    return fx(x, t)
  File "/home/karlla/Documentos/Documentos/Mestrado/2° semestre/Estatistica computacional /Trabalho-artigo2/gerar.py", line 44, 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'>

I can’t fix this.

  • Where is that n? and t?

  • The n is the temp size (which is data I generated). I will put the full code.

  • When so, please put the stacktrace of the whole error mensgaem - note that it is listed which line of each function is running at the time of the error - as you pasted only the last part, we only see where the error occurred within the Pandas package - but the error happened there because you sent unexpected data to a Pandas function.

  • All right, sorry. I’ve put the whole error message.

1 answer

5

The error that is occurring is due to the fact that you are using an object Dataframe of Pandas and not a list of values (in the contents of the variable x). If you put, right there at the beginning of the function mcmc the following:

print(x) 

You will see as a result something like (in the test I used N = 5):

Entre com o N: 5
            0
0   -1.056996
1    2.015035
2    1.401659
3    3.210307
..        ...
98   5.822160
99  -4.474483

[100 rows x 1 columns]

That is, its variable x is a Dataframe with 100 rows and 1 column. The column has label (label) 0 (that appears right there at the beginning on the column), so that when you do x[i] (in the calculation within the function fx) you’re trying to get the label column i. Your code would give error when the i was different from 0, but is already giving error in the beginning because you are trying to use the whole column x[0] (do print(x[0]) and you will notice that it returns the entire column) in place of an expected real value (i.e., a float).

That is, try to switch in your code to the following:

def fx(x, t):
    prod = 1.0
    for i in range(0, len(x)):
        prod *= (t[0] / t[1]) * exp(- (x[0][i] / t[1])) * exp(-t[0] * exp(-(x[0][i] / t[1])))
    return prod

Note the use of x[0][i] (that is, the value in the position i of the only spine 0) instead of x[i].

After "fix" this, your code gives another error later (he complains that in line with t2 = gamma.pdf ... you are not informing the mandatory argument a), but it works (that is, it runs without errors - I don’t know if it produces the result you want).

Anyway, if you will use Dataframe, use it correctly, or convert it to a doing list (and in this case your code in fx can be maintained as originally):

MP = mcmc(N=N, x=temp[0].tolist())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.