'TypeError: Wrong number of dimensions: expected 0, got 1 with shape (6,)
I am performing a linear regression analysis using bayessian method.
X = data.drop(columns='Power')
Y = data['Power']
a_0=1.
b_0=1.
mu_0=(0.,0.,0.,0.,0.,0.)
row1=[1.,0.,0.,0.,0.,0.]
row2=[0.,1.,0.,0.,0.,0.]
row3=[0.,0.,1.,0.,0.,0.]
row4=[0.,0.,0.,1.,0.,0.]
row5=[0.,0.,0.,0.,1.,0.]
row6=[0.,0.,0.,0.,0.,1.]
Sigma_0 =np.array([row1,row2,row3,row4,row5,row6])
with pm.Model() as model:
sigma_square = 1.0 / pm.Gamma('sigma square', alpha=a_0, beta=b_0)
w = pm.MvNormal("w", mu=mu_0, cov=Sigma_0)
likelihood = pm.Normal('likelihood', mu=np.dot(X,w), cov=sigma_square + np.dot(X.T ,np.dot(Sigma_n, X)), observed=Y)
trace = sample(3000, return_inferencedata=True)
This returns the following error:
TypeError Traceback (most recent call last)
in () 2 sigma_square = 1.0 / pm.Gamma('sigma square', alpha=a_0, beta=b_0) 3 ----> 4 w = pm.MvNormal("w", mu=mu_0, cov=Sigma_0) 5 likelihood = pm.Normal('likelihood', mu=np.dot(X,w), cov=sigma_square + np.dot(X.T ,np.dot(Sigma_n, X)), observed=Y) 6 trace = sample(3000, return_inferencedata=True)
/usr/local/lib/python3.7/dist-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
120 else:
121 dist = cls.dist(*args, **kwargs)
--> 122 return model.Var(name, dist, data, total_size, dims=dims) 123 124 def getnewargs(self):
/usr/local/lib/python3.7/dist-packages/pymc3/model.py in Var(self, name, dist, data, total_size, dims) 1136 if getattr(dist, "transform", None) is None: 1137 with self: -> 1138 var = FreeRV(name=name, distribution=dist, total_size=total_size, model=self) 1139 self.free_RVs.append(var) 1140 else:
/usr/local/lib/python3.7/dist-packages/pymc3/model.py in init(self, type, owner, index, name, distribution, total_size, model) 1667 self.distribution = distribution 1668 self.tag.test_value = ( -> 1669 np.ones(distribution.shape, distribution.dtype) * distribution.default() 1670 ) 1671 self.logp_elemwiset = distribution.logp(self)
/usr/local/lib/python3.7/dist-packages/theano/graph/utils.py in setattr(self, attr, obj) 264 265 if getattr(self, "attr", None) == attr: --> 266 obj = self.attr_filter(obj) 267 268 return object.setattr(self, attr, obj)
/usr/local/lib/python3.7/dist-packages/theano/tensor/type.py in filter(self, data, strict, allow_downcast) 180 if self.ndim != data.ndim: 181 raise TypeError( --> 182 f"Wrong number of dimensions: expected {self.ndim}," 183 f" got {data.ndim} with shape {data.shape}." 184 )
TypeError: Wrong number of dimensions: expected 0, got 1 with shape (6,).
It seems my covariance matrix does not meet the required type\format. I don't know how to fix this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
