johnaphun
johnaphun

Reputation: 694

Numpy Attribute error pymc

I'm trying to implement a state-space model using pymc and numpy.

As such I'm using numpy arrays with dtype object to avoid a setting an array element with a sequence error. As demonstrated here

I then use a list as a 'container' of pymc nodes suggested here and implemented here

My issue comes when I try to use numpy's exponential function which won't work on an array with a dtype of object.

When I try to change the dtype to float I get the setting an array with a sequence error.

Here's some code which replicates the issue.

import pandas as pd
import pymc as pm
import numpy as np
from datetime import datetime
import pylab

df = pd.read_csv('http://www.football-data.co.uk/mmz4281/1314/E0.csv')

results = df[['HomeTeam','AwayTeam','FTHG','FTAG']]

teams = sorted(results['HomeTeam'].unique())

y1 = np.array(results['FTHG'])

y2 = np.array(results['FTAG'])

home_team = pd.Series(np.arange(20),index=teams)[results['HomeTeam']].values

away_team = pd.Series(np.arange(20),index=teams)[results['AwayTeam']].values

game = range(df.shape[0])

nteams = len(teams)
ngames = len(game)



df.Date = df.Date.apply(lambda x: datetime.strptime(x, '%d/%m/%y'))

df.Date = df.Date.apply(lambda x: (x - df.Date.ix[0]).days//7)


week = pd.factorize(df.Date)[0]

nweeks = max(week)+1

nweeks

home = pm.Normal('home', 0, .0001, value=[0]*nteams,size=(nteams,))
away = pm.Normal('away', 0, .0001, value=0)
mu_att = pm.Normal('mu_att', 0, .0001)
mu_def = pm.Normal('mu_def', 0, .0001, value=0)
tau_att = pm.Gamma('tau_att', .1, .1)
tau_def = pm.Gamma('tau_def', .1, .1)
sigma = pm.Gamma('sigma', .1, .1)


atts_0 = pm.Normal("atts_0",
               mu=mu_att,
               tau=tau_att,
               size=(nteams,1))

defs_0 = pm.Normal("atts_0",
               mu=mu_def,
               tau=tau_def,
               size=(nteams,1))

atts = [atts_0]
defs = [defs_0]

for i in range(1,nweeks+1):
    a = pm.Normal('a_%i'%i, mu = atts[i-1],tau=sigma)
    attsi = pm.Lambda('atts_%i' % i, lambda a=a: np.eye(nteams).dot(a) - np.ones(nteams).dot(np.ones(nteams).T))
    atts.append(attsi)

for i in range(1,nweeks+1):
    d = pm.Normal('d_%i'%i, mu = defs[i-1],tau=sigma)
    defsi = pm.Lambda('defs_%i' % i, lambda d=d: np.eye(nteams).dot(d) - np.ones(nteams).dot(np.ones(nteams).T))
    defs.append(defsi)

atts = np.array(atts[1:])
defs = np.array(defs[1:])


@pm.deterministic
def home_theta(home=home,
           atts=atts,
           defs=defs,
           week=week,
           home_team=home_team,
           away_team=away_team): 
    return  np.exp((home[home_team] + atts[week][home_team] + defs[week][away_team]))    


LazyFunction.pyx in pymc.LazyFunction.LazyFunction.force_compute (pymc/LazyFunction.c:2409)()

<ipython-input-35-9977366624a3> in home_theta(home, atts, defs, week, home_team, away_team)
  6                home_team=home_team,
  7                away_team=away_team): 
----> 8     return  np.exp((home[home_team] + atts[week][home_team] + defs[week][away_team]))

AttributeError: 'numpy.ndarray' object has no attribute 'exp'

Upvotes: 0

Views: 866

Answers (1)

hpaulj
hpaulj

Reputation: 231355

From the error

----> 8     return  np.exp((home[home_team] + atts[week][home_team] + defs[week][away_team]))
AttributeError: 'numpy.ndarray' object has no attribute 'exp'

I'm guessing that the numpy module (named np) has been replaced with an array, a np.ndarray object. In other words, something is doing something like:

np = np.array(...) # or
np = x + 3  # where x=np.array...

Possibly it's the @pm.deterministic decorator.

Without knowing anything about pymc you could try using numpy as the import name instead of np. In other words, try to bypass this renaming.

import numpy
....
numpy.exp(...)

Upvotes: 1

Related Questions