Numpy 属性错误 pymc



我正在尝试使用 pymc 和 numpy 实现状态空间模型。

因此,我将 numpy 数组与 dtype 对象一起使用,以避免设置带有序列错误的数组元素。如此处所示

然后,我使用列表作为此处建议并在此处实现的pymc节点的"容器"

当我尝试使用 numpy 的指数函数时,我的问题来了,该函数不适用于具有 dtype 对象的数组。

当我尝试将 dtype 更改为浮点数时,我得到了一个带有序列错误的数组设置。

下面是一些复制该问题的代码。

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'

从错误

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

我猜numpy模块(名为 np)已被一个数组(一个np.ndarray对象)替换。 换句话说,某些事情正在做类似的事情:

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

可能是@pm.deterministic装饰器。

在不知道pymc的情况下,您可以尝试使用 numpy 作为导入名称而不是 np 。 换句话说,尝试绕过此重命名。

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

最新更新