0
我試圖實現一個使用pymc和numpy的狀態空間模型。Numpy屬性錯誤pymc
因此,我用dtype對象使用numpy數組來避免設置一個具有序列錯誤的數組元素。作爲證明here
然後我用一個列表作爲pymc節點的「容器」建議here和實施here
我的問題是當我嘗試使用numpy的的指數函數,這將不與陣列上運行對象的類型。
當我嘗試將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'
感謝您的幫助大家好,但是這仍然會產生相同的錯誤 'AttributeError的: 'numpy.ndarray' 對象有沒有屬性 'EXP'' – johnaphun