Skip to content

Commit d318895

Browse files
committed
fixes np.int64 bug in MaximumLikelihoodEstimation
This bug occurs because numpy does not take numpy.int64 to be an instance of int. For more details refer - numpy/numpy#2951
1 parent 58ef0b5 commit d318895

File tree

3 files changed

+235
-1
lines changed

3 files changed

+235
-1
lines changed

pgmpy/factors/Factor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def __init__(self, variables, cardinality, values):
9898

9999
values = np.array(values)
100100

101-
if values.dtype != int and values.dtype != float:
101+
if all([values.dtype != value_type for value_type in (float, int, np.int64, np.float32)]):
102102
raise TypeError("Values: Expected type int or type float, got ", values.dtype)
103103

104104
if len(cardinality) != len(variables):

test_bif.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<BIF version="0.3">
2+
<NETWORK>
3+
<VARIABLE TYPE="nature">
4+
<NAME>bowel-problem</NAME>
5+
<OUTCOME>0</OUTCOME>
6+
<OUTCOME>1</OUTCOME>
7+
<PROPERTY>position = (190, 69)</PROPERTY>
8+
</VARIABLE>
9+
<VARIABLE TYPE="nature">
10+
<NAME>dog-out</NAME>
11+
<OUTCOME>0</OUTCOME>
12+
<OUTCOME>1</OUTCOME>
13+
<PROPERTY>position = (155, 165)</PROPERTY>
14+
</VARIABLE>
15+
<VARIABLE TYPE="nature">
16+
<NAME>family-out</NAME>
17+
<OUTCOME>0</OUTCOME>
18+
<OUTCOME>1</OUTCOME>
19+
<PROPERTY>position = (112, 69)</PROPERTY>
20+
</VARIABLE>
21+
<VARIABLE TYPE="nature">
22+
<NAME>hear-bark</NAME>
23+
<OUTCOME>0</OUTCOME>
24+
<OUTCOME>1</OUTCOME>
25+
<PROPERTY>position = (154, 241)</PROPERTY>
26+
</VARIABLE>
27+
<VARIABLE TYPE="nature">
28+
<NAME>light-on</NAME>
29+
<OUTCOME>0</OUTCOME>
30+
<OUTCOME>1</OUTCOME>
31+
<PROPERTY>position = (73, 165)</PROPERTY>
32+
</VARIABLE>
33+
<DEFINITION>
34+
<FOR>bowel-problem</FOR>
35+
<TABLE>0.01 0.99 </TABLE>
36+
</DEFINITION>
37+
<DEFINITION>
38+
<FOR>dog-out</FOR>
39+
<GIVEN>bowel-problem</GIVEN>
40+
<GIVEN>family-out</GIVEN>
41+
<TABLE>0.99 0.01 0.97 0.03 0.9 0.1 0.3 0.7 </TABLE>
42+
</DEFINITION>
43+
<DEFINITION>
44+
<FOR>family-out</FOR>
45+
<TABLE>0.15 0.85 </TABLE>
46+
</DEFINITION>
47+
<DEFINITION>
48+
<FOR>hear-bark</FOR>
49+
<GIVEN>dog-out</GIVEN>
50+
<TABLE>0.7 0.3 0.01 0.99 </TABLE>
51+
</DEFINITION>
52+
<DEFINITION>
53+
<FOR>light-on</FOR>
54+
<GIVEN>family-out</GIVEN>
55+
<TABLE>0.6 0.4 0.05 0.95 </TABLE>
56+
</DEFINITION>
57+
</NETWORK>
58+
</BIF>

test_xml.pgmx

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<ProbModelXML formatVersion="1.0">
2+
<ProbNet type="BayesianNetwork">
3+
<Variables>
4+
<Variable name="difficulty" role="Chance" type="FiniteState">
5+
<Comment />
6+
<Coordinates />
7+
<AdditionalProperties />
8+
<States>
9+
<State name="difficult">
10+
<AdditionalProperties />
11+
</State>
12+
<State name="easy">
13+
<AdditionalProperties />
14+
</State>
15+
</States>
16+
</Variable>
17+
<Variable name="intelligence" role="Chance" type="FiniteState">
18+
<Comment />
19+
<Coordinates />
20+
<AdditionalProperties />
21+
<States>
22+
<State name="dumb">
23+
<AdditionalProperties />
24+
</State>
25+
<State name="smart">
26+
<AdditionalProperties />
27+
</State>
28+
</States>
29+
</Variable>
30+
</Variables>
31+
<Links>
32+
<Link directed="1" var1="difficulty" var2="grade">
33+
<Comment>Directed Edge from difficulty to grade</Comment>
34+
<Label>diff_to_grad</Label>
35+
<AdditionalProperties />
36+
</Link>
37+
<Link directed="1" var1="grade" var2="recommendation_letter">
38+
<Comment>Directed Edge from grade to recommendation_letter</Comment>
39+
<Label>grad_to_reco</Label>
40+
<AdditionalProperties />
41+
</Link>
42+
<Link directed="1" var1="intelligence" var2="SAT">
43+
<Comment>Directed Edge from intelligence to SAT</Comment>
44+
<Label>intel_to_sat</Label>
45+
<AdditionalProperties />
46+
</Link>
47+
<Link directed="1" var1="intelligence" var2="grade">
48+
<Comment>Directed Edge from intelligence to grade</Comment>
49+
<Label>intel_to_grad</Label>
50+
<AdditionalProperties />
51+
</Link>
52+
</Links>
53+
<Potentials>
54+
<Potential role="Utility" type="Tree/ADD">
55+
<Variables>
56+
<Variable name="D0" />
57+
<Variable name="C0" />
58+
<Variable name="C1" />
59+
<Variable name="D1" />
60+
</Variables>
61+
<TopVariable name="D0" />
62+
<Branches>
63+
<Branch>
64+
<States>
65+
<State name="no" />
66+
</States>
67+
<Potential type="Tree/ADD">
68+
<TopVariable name="C1" />
69+
<Branches>
70+
<Branch>
71+
<Potential type="MixtureOfExponentials">
72+
<Variables>
73+
<Variable name="C0" />
74+
<Variable name="C1" />
75+
</Variables>
76+
<Subpotentials>
77+
<Potential type="Exponential">
78+
<Potential type="Table">
79+
<Values>3</Values>
80+
</Potential>
81+
</Potential>
82+
<Potential type="Exponential">
83+
<Coefficients>4 -1</Coefficients>
84+
<Potential type="Table">
85+
<Values>-1</Values>
86+
</Potential>
87+
<NumericVariables>
88+
<Variable name="C0" />
89+
<Variable name="C1" />
90+
</NumericVariables>
91+
</Potential>
92+
</Subpotentials>
93+
</Potential>
94+
<Thresholds>
95+
<Threshold value="-Infinity" />
96+
<Threshold belongsTo="Left" value="0" />
97+
</Thresholds>
98+
</Branch>
99+
<Branch>
100+
<Potential type="MixtureOfExponentials">
101+
<Variables>
102+
<Variable name="C1" />
103+
<Variable name="D1" />
104+
</Variables>
105+
<Subpotentials>
106+
<Potential type="Exponential">
107+
<Coefficients>0.25</Coefficients>
108+
<Potential type="Table">
109+
<Variables>
110+
<Variable name="D1" />
111+
</Variables>
112+
<Values>10 5</Values>
113+
</Potential>
114+
<NumericVariables>
115+
<Variable name="C1" />
116+
</NumericVariables>
117+
</Potential>
118+
</Subpotentials>
119+
</Potential>
120+
<Thresholds>
121+
<Threshold belongsTo="Left" value="0" />
122+
<Threshold value="+Infinity" />
123+
</Thresholds>
124+
</Branch>
125+
</Branches>
126+
</Potential>
127+
</Branch>
128+
<Branch>
129+
<States>
130+
<State name="yes" />
131+
</States>
132+
<Potential type="MixtureOfExponentials">
133+
<Variables>
134+
<Variable name="C0" />
135+
</Variables>
136+
<Subpotentials>
137+
<Potential type="Exponential">
138+
<Coefficients>1</Coefficients>
139+
<Potential type="Table">
140+
<Values>0.3</Values>
141+
</Potential>
142+
<NumericVariables>
143+
<Variable name="C0" />
144+
</NumericVariables>
145+
</Potential>
146+
<Potential type="Exponential">
147+
<Potential type="Table">
148+
<Values>0.7</Values>
149+
</Potential>
150+
</Potential>
151+
</Subpotentials>
152+
</Potential>
153+
</Branch>
154+
</Branches>
155+
</Potential>
156+
</Potentials>
157+
<AdditionalConstraints>
158+
<Constraint name="MaxNumParents">
159+
<Argument name="numParents" value="5" />
160+
</Constraint>
161+
</AdditionalConstraints>
162+
<Language>English</Language>
163+
<Comment>Student example model from Probabilistic Graphical Models: Principles and Techniques by Daphne Koller</Comment>
164+
</ProbNet>
165+
<AdditionalProperties>
166+
<Property name="elvira.title" value="X ray result" />
167+
</AdditionalProperties>
168+
<DecisionCriteria>
169+
<Criterion name="cost">
170+
<AdditionalProperties />
171+
</Criterion>
172+
<Criterion name="effectiveness">
173+
<AdditionalProperties />
174+
</Criterion>
175+
</DecisionCriteria>
176+
</ProbModelXML>

0 commit comments

Comments
 (0)