def entropy(x, data): """entropy of an attribute x from dataset data""" if type(x)==orange.EnumVariable: return _entropy(p2f(orange.Distribution(x, data))) if type(x)==list: if len(x)==2: # joint entropy of a pair of attributes c = orange.ContingencyAttrAttr(x, y, data) return _entropy(p2f(flatten(c))) else: # joint entropy of for a set of attributes pass
# Description: Demonstrates the use of ContingencyClassAttr # Category: statistics # Classes: Contingency, ContingencyClassAttr # Uses: monk1 # Referenced: contingency.htm import orange data = orange.ExampleTable("bridges") cont = orange.ContingencyAttrAttr("SPAN", "MATERIAL", data) print "Distributions:" for val in cont.outerVariable: print " p(.|%s) = %s" % (val.native(), cont.p_attr(val)) print cont.normalize() for val in cont.outerVariable: print "%s:" % val.native() for inval, p in cont[val].items(): if p: print " %s (%i%%)" % (inval, int(100 * p + 0.5)) print cont = orange.ContingencyAttrAttr(data.domain["SPAN"], data.domain["MATERIAL"]) for ex in data: cont.add(ex["SPAN"], ex["MATERIAL"]) print "Distributions from a matrix computed manually:" for val in cont.outerVariable: print " p(.|%s) = %s" % (val.native(), cont.p_attr(val)) print
def joint_entropy(x, y, data): """return H(x,y), a joint entropy of attributes x and y""" c = orange.ContingencyAttrAttr(x, y, data) return _entropy(p2f(flatten(c)))
# Description: Demonstrates the use of correspondence analysis # Category: correspondence, projection # Classes: CA # Uses: bridges.tab import orange import orngCA data = orange.ExampleTable("bridges") cm = orange.ContingencyAttrAttr("PURPOSE", "MATERIAL", data) ca = orngCA.CA([list(col) for col in cm]) def report(coors, labels): for coor, label in zip(coors, labels): print " %-10s (%.3f, %.3f)" % (label + ":", coor[0], coor[1]) print "PURPOSE" report(ca.getPrincipalColProfilesCoordinates(), data.domain["PURPOSE"].values) print print "MATERIAL" report(ca.getPrincipalRowProfilesCoordinates(), data.domain["PURPOSE"].values) print