本文整理汇总了Python中bayesNet.Factor.variableDomainsDict方法的典型用法代码示例。如果您正苦于以下问题:Python Factor.variableDomainsDict方法的具体用法?Python Factor.variableDomainsDict怎么用?Python Factor.variableDomainsDict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bayesNet.Factor
的用法示例。
在下文中一共展示了Factor.variableDomainsDict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eliminate
# 需要导入模块: from bayesNet import Factor [as 别名]
# 或者: from bayesNet.Factor import variableDomainsDict [as 别名]
def eliminate(factor, eliminationVariable):
"""
Question 2: Your eliminate implementation
Input factor is a single factor.
Input eliminationVariable is the variable to eliminate from factor.
eliminationVariable must be an unconditioned variable in factor.
You should calculate the set of unconditioned variables and conditioned
variables for the factor obtained by eliminating the variable
eliminationVariable.
Return a new factor where all of the rows mentioning
eliminationVariable are summed with rows that match
assignments on the other variables.
Useful functions:
Factor.getAllPossibleAssignmentDicts
Factor.getProbability
Factor.setProbability
Factor.unconditionedVariables
Factor.conditionedVariables
Factor.variableDomainsDict
"""
# autograder tracking -- don't remove
if not (callTrackingList is None):
callTrackingList.append(('eliminate', eliminationVariable))
# typecheck portion
if eliminationVariable not in factor.unconditionedVariables():
print "Factor failed eliminate typecheck: ", factor
raise ValueError, ("Elimination variable is not an unconditioned variable " \
+ "in this factor\n" +
"eliminationVariable: " + str(eliminationVariable) + \
"\nunconditionedVariables:" + str(factor.unconditionedVariables()))
if len(factor.unconditionedVariables()) == 1:
print "Factor failed eliminate typecheck: ", factor
raise ValueError, ("Factor has only one unconditioned variable, so you " \
+ "can't eliminate \nthat variable.\n" + \
"eliminationVariable:" + str(eliminationVariable) + "\n" +\
"unconditionedVariables: " + str(factor.unconditionedVariables()))
"*** YOUR CODE HERE ***"
new_unconditioned = factor.unconditionedVariables()
new_unconditioned.remove(eliminationVariable)
new_Factor = Factor(new_unconditioned,factor.conditionedVariables(), factor.variableDomainsDict())
for j in new_Factor.getAllPossibleAssignmentDicts():
probabilitySum = 0
for i in new_Factor.variableDomainsDict().get(eliminationVariable):
j[eliminationVariable] = i
currentProb = factor.getProbability(j)
probabilitySum += currentProb
del j[eliminationVariable]
new_Factor.setProbability(j, probabilitySum)
return new_Factor