我正在尝试从子类中调用父类的方法.具体来说,我的父类是一个PySide.QtGui.QMainWindow对象,我的子类是一个PySide.QtGui.QWidget对象; 后者被设定为前者的中心小部件.我正在尝试将子项中的按钮连接到父类中的方法.这在过去使用过self.parent().method_name,但在下面的示例中不起作用,我不明白为什么:
import sys
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.do_something() #sanity check
self.cw = ChildWidget()
self.setCentralWidget(self.cw)
self.show()
def do_something(self):
print 'doing something!'
class ChildWidget(QtGui.QWidget):
def __init__(self):
super(ChildWidget, self).__init__()
self.button1 = QtGui.QPushButton()
self.button1.clicked.connect(self.do_something_else)
self.button2 = QtGui.QPushButton()
self.button2.clicked.connect(self.parent().do_something)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.button1)
self.layout.addWidget(self.button2)
self.setLayout(self.layout)
self.show()
def do_something_else(self):
print 'doing something else!'
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
这是错误:
self.button2.clicked.connect(self.parent().do_something)
AttributeError: 'NoneType' object …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据另一个数组的内容来索引一个数组,如下所示:
import numpy as np
a = np.random.randint(0,100,10)
b = np.linspace(0,100,10)
print a[b<75]
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我真正想做的是基于两个条件的索引,如下所示:
print a[25<b<75]
Run Code Online (Sandbox Code Playgroud)
但这会产生以下错误:
ValueError:包含多个元素的数组的真值不明确。使用 a.any() 或 a.all()
谢谢你的帮助!
我对计算贝叶斯因子以比较 PyMC 3 中的两个模型感兴趣。根据此网站,在 PyMC 2 中,该过程似乎相对简单:包括伯努利随机变量和自定义似然函数,该函数返回第一个模型的似然性当伯努利变量的值为 0 时,第二个模型的似然性为 1。然而,在 PyMC 3 中,事情变得更加复杂,因为随机节点需要是 Theano 变量。
我的两个似然函数是二项式,所以我想我需要重写这个类:
class Binomial(Discrete):
R"""
Binomial log-likelihood.
The discrete probability distribution of the number of successes
in a sequence of n independent yes/no experiments, each of which
yields success with probability p.
.. math:: f(x \mid n, p) = \binom{n}{x} p^x (1-p)^{n-x}
======== ==========================================
Support :math:`x \in \{0, 1, \ldots, n\}`
Mean :math:`n p`
Variance :math:`n p (1 - p)`
======== ==========================================
Parameters
---------- …Run Code Online (Sandbox Code Playgroud)