【问题标题】:Numpy Attribute error pymcNumpy属性错误pymc
【发布时间】:2016-03-05 20:07:00
【问题描述】:

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

因此,我使用带有 dtype 对象的 numpy 数组来避免设置具有序列错误的数组元素。如图所示here

然后我使用一个列表作为 pymc 节点的“容器”,建议 here 并实施 here

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

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

这里有一些复制问题的代码。

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'

【问题讨论】:

    标签: python numpy pymc


    【解决方案1】:

    来自错误

    ----> 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(...)
    

    【讨论】:

    • 您好,感谢您的帮助,但这仍然会产生相同的错误AttributeError: 'numpy.ndarray' object has no attribute 'exp'
    猜你喜欢
    • 2015-03-02
    • 2020-04-08
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多