本文整理汇总了Python中bayespy.nodes.Mixture.params方法的典型用法代码示例。如果您正苦于以下问题:Python Mixture.params方法的具体用法?Python Mixture.params怎么用?Python Mixture.params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bayespy.nodes.Mixture
的用法示例。
在下文中一共展示了Mixture.params方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generateCPD
# 需要导入模块: from bayespy.nodes import Mixture [as 别名]
# 或者: from bayespy.nodes.Mixture import params [as 别名]
def generateCPD(self, term): #, X_train, y_train, X_test, y_test, X_validation, y_validation, g_train, g_test, g_validation):
clf = loadClf(self.ontology[term]['name'], self.fold, self.clfName)
posTrain = sum(clf.y_train == POSTIVE_LABEL)
negTrain = sum(clf.y_train == NEGATIVE_LABEL)
totalTrain = posTrain + negTrain
children = sorted(self.ontology[term]['children'])
parents = sorted(self.ontology[term]['parents'])
labels = {l : PRIOR for l in product(*((POSTIVE_LABEL,NEGATIVE_LABEL),)*(len(children)+1))}
if children:
childNodes = [self.ontology[child]['node'][self.fold][self.clfName] for child in children]
for gene,y in zip(clf.g_train, clf.y_train):
event = []
for child in children:
event.append(POSTIVE_LABEL if gene in self.ontology.associations[child] else NEGATIVE_LABEL)
event.append(POSTIVE_LABEL if gene in self.ontology.associations[term] else NEGATIVE_LABEL)
assert (gene in self.ontology.associations[term]) == (y == POSTIVE_LABEL)
event = tuple(event)
labels[event] += 1
def countBoth(event):
return labels[event[:-1]+(POSTIVE_LABEL,)] + labels[event[:-1]+(NEGATIVE_LABEL,)]
cprior = PRIOR * (2 ** len(children))
types = [Mixture]*(len(children)-1) + [Categorical]
mixparams = [i for s in zip(childNodes, types) for i in s]
cpd = numpy.empty((2,)*(len(children)+1))
for event, counted in labels.items():
v=cpd
for b in event[:-1]:
v = v[b]
hid = event[-1]
print("Event: ", event)
if POSTIVE_LABEL not in event[:-1]: # Všichni potomci označeni "ne"
v[hid] = counted/countBoth(event)
print("Stored %d / %d" % (counted,countBoth(event)))
else:
v[hid] = {POSTIVE_LABEL: 0.99, NEGATIVE_LABEL:0.01}[hid]
print("Stored %d : %d" % (hid, v[hid]))
#print(term,"<-",",".join(children))
print(cpd)
#print(labels)
hidden = Mixture(*mixparams, cpd)
hidden.params = cpd
else: #No children
#hidden = DiscreteDistribution({'0': posTrain / totalTrain, '1': negTrain / totalTrain})
params = (posTrain / totalTrain, negTrain / totalTrain)
hidden = Categorical(params)
hidden.params = params
#print("Hidden node %s:" % term)
#print(repr(hidden))
#print([p for p in hidden.parents if isinstance(p, Stochastic)])
#print(hidden.get_moments())
conf = clf.conf + PRIOR
#posTest, negTest = numpy.sum(conf, 1)
posTest, negTest = numpy.sum(conf, 0)
#print("Confusion matrix:")
#print(conf)
try:
assert term != self.ontology.root
pos_decisions = clf.decision_function(clf.X_test[clf.y_test==POSTIVE_LABEL])
neg_decisions = clf.decision_function(clf.X_test[clf.y_test==NEGATIVE_LABEL])
means = [numpy.mean(pos_decisions)], [numpy.mean(neg_decisions)]
maxprec = 100.0
precs = [[numpy.min((1/numpy.var(pos_decisions), maxprec))]], [[numpy.min((1/numpy.var(neg_decisions), maxprec))]]
#else:
except (ValueError, AssertionError):
means = [-1.], [1.]
precs = [[1.]], [[1.]]
print("Gaussian params:", term, self.ontology[term]['name'], means, precs)
observed = Mixture(hidden, Gaussian, means, precs)
#observed = ConditionalProbabilityTable([
# ['0', '0', conf[0][0] / posTest], # if term != root else 1.],
# ['0', '1', conf[0][1] / posTest], # if term != root else 0.],
# ['1', '0', conf[1][0] / negTest], # if term != root else 0.],
# ['1', '1', conf[1][1] / negTest]], #if term != root else 1.]],
# [hidden.distribution])
#print("Observed node %s - %s:" % (term, self.ontology[term]['name']))
#print(repr(observed))
#print([p for p in observed.parents if isinstance(p, Stochastic)])
self.ontology[term]['node'][self.fold][self.clfName] = hidden
#self.ontology[term]['clf'][self.fold][self.clfName] = clf, X_validation, y_validation, g_validation
assert self.lenValidation is None or self.lenValidation == len(clf.y_validation)
self.lenValidation = len(clf.y_validation)
self.allobserved[term] = observed
self.allhidden[term] = hidden
self.extranodes.update((p for p in hidden.parents if isinstance(p, Stochastic)))