BayesianNetwork: save and load model #1600
-
Hello. I want to save my model and then load it whenever I need to run it on the data. Up until now I ran then model and then run the code for calculating probabilities. I am looking for a way to save model in a file and just load it whenever I need. I have found this (link): It saves my model perfectly but when I try to load it, an error appears: Does anyone know why this is like this? |
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment · 6 replies
-
@jonasvit Would it be possible to share your bif file? It seems to be working for the alarm model in the example above. |
Beta Was this translation helpful? Give feedback.
All reactions
-
It still doesn't work... Here is the model
|
Beta Was this translation helpful? Give feedback.
All reactions
-
@ankurankan Were you able to have a look at it? |
Beta Was this translation helpful? Give feedback.
All reactions
-
@jonasvit Seems to be because of spaces in the state names. As bif writes the state names as a list, if there is a space in the name, there is no way to know if they were two separate states or a single state with a space. Here's the fixed model, replaced spaces with underscore: from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete import TabularCPD
# Defining the model structure. We can define the network by just passing a list of edges (direction is important).
model = BayesianNetwork(
[
("DESIRED", "AAA"),
("DESIRED", "BBB"),
("DESIRED", "CCC"),
("DESIRED", "DDD"),
("DESIRED", "EEE"),
("DESIRED", "FFF"),
("DESIRED", "GGG"),
("DESIRED", "HHH"),
("DESIRED", "III"),
("DESIRED", "JJJ"),
("DESIRED", "KKK"),
("DESIRED", "LLL"),
("DESIRED", "MMM"),
("DESIRED", "NNN"),
("DESIRED", "OOO"),
("DESIRED", "PPP"),
("DESIRED", "RRR"),
("DESIRED", "SSS"),
("DESIRED", "TTT"),
("LLL", "PPP"),
("LLL", "RRR"),
("LLL", "OOO"),
("NNN", "SSS"),
("EEE", "JJJ"),
("DESIRED", "UUU"),
("DESIRED", "VVV"),
("DESIRED", "ZZZ"),
("DESIRED", "XXX"),
]
)
# Defining individual CPDs.
cpd_DESIRED = TabularCPD(
variable="DESIRED",
variable_card=2,
values=[[0.1], [0.9]],
state_names={"DESIRED": ["Yes", "No"]},
)
cpd_AAA = TabularCPD(
variable="AAA",
variable_card=2,
values=[[0.0672, 0.0219], [0.9328, 0.9781]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "AAA": ["true", "false"]},
)
cpd_BBB = TabularCPD(
variable="BBB",
variable_card=2,
values=[[0.4835, 0.4852], [0.5165, 0.5148]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "BBB": ["Male", "Female"]},
)
cpd_CCC = TabularCPD(
variable="CCC",
variable_card=6,
values=[
[0.5411, 0.7309],
[0.1813, 0.1133],
[0.1308, 0.0819],
[0.0838, 0.0424],
[0.0430, 0.0215],
[0.0200, 0.0100],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"CCC": ["0", "1", "2", "3", "4", "5"],
},
)
cpd_DDD = TabularCPD(
variable="DDD",
variable_card=10,
values=[
[0.06392, 0.0529],
[0.06772, 0.06772],
[0.1128, 0.11245],
[0.20322, 0.18615],
[0.1642, 0.14784],
[0.1168, 0.14631],
[0.08484, 0.08947],
[0.07084, 0.07475],
[0.05923, 0.06492],
[0.05642, 0.05749],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"DDD": [
"0-18",
"18-25",
"25-30",
"30-40",
"40-50",
"50-60",
"60-70",
"70-80",
"80-90",
"90<",
],
},
)
cpd_EEE = TabularCPD(
variable="EEE",
variable_card=7,
values=[
[0.03537, 0.01541],
[0.0428, 0.02957],
[0.10335, 0.07777],
[0.15299, 0.12446],
[0.1372, 0.11919],
[0.16215, 0.14027],
[0.36614, 0.49331],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"EEE": [
"<=30",
"30-60",
"60-182",
"182-365",
"365-486",
"486-730",
"730<",
],
},
)
cpd_FFF = TabularCPD(
variable="FFF",
variable_card=2,
values=[[0.1849, 0.1286], [0.8151, 0.8714]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "FFF": ["true", "false"]},
)
cpd_GGG = TabularCPD(
variable="GGG",
variable_card=7,
values=[
[0.12685, 0.13063],
[0.15069, 0.18188],
[0.27069, 0.34866],
[0.2698, 0.20979],
[0.10181, 0.07144],
[0.06097, 0.04332],
[0.0192, 0.01427],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"GGG": ["A", "B", "C", "D", "E", "F", "G"],
},
)
cpd_HHH = TabularCPD(
variable="HHH",
variable_card=2,
values=[[0.12497, 0.07957], [0.87503, 0.92043]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"HHH": ["true", "false"],
},
)
cpd_XXX = TabularCPD(
variable="XXX",
variable_card=2,
values=[[0.1535, 0.1242], [0.8465, 0.8758]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "XXX": ["true", "false"],},
)
cpd_III = TabularCPD(
variable="III",
variable_card=7,
values=[
[0.05063, 0.03436],
[0.05098, 0.04126],
[0.13258, 0.11574],
[0.17313, 0.16817],
[0.14329, 0.13826],
[0.16529, 0.17389],
[0.2841, 0.32832],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"III": [
"<=30",
"30-60",
"60-182",
"182-365",
"365-486",
"486-730",
"730<",
],
},
)
cpd_JJJ = TabularCPD(
variable="JJJ",
variable_card=2,
values=[
[
0.1931,
0.1931,
0.1931,
0.1931,
0.1931,
0.5,
0.5,
0.1198,
0.1198,
0.1198,
0.1198,
0.1198,
0.5,
0.5,
],
[
0.8069,
0.8069,
0.8069,
0.8069,
0.8069,
0.5,
0.5,
0.8802,
0.8802,
0.8802,
0.8802,
0.8802,
0.5,
0.5,
],
],
evidence=["DESIRED", "EEE"],
evidence_card=[2, 7],
state_names={
"DESIRED": ["Yes", "No"],
"JJJ": ["true", "false"],
"EEE": [
"<=30",
"30-60",
"60-182",
"182-365",
"365-486",
"486-730",
"730<",
],
},
)
cpd_KKK = TabularCPD(
variable="KKK",
variable_card=2,
values=[
[0.190895145370873, 0.111067998447945],
[0.809104854629127, 0.888932001552055],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"KKK": ["true", "false"],
},
)
cpd_LLL = TabularCPD(
variable="LLL",
variable_card=20,
values=[
[0.05899, 0.0327],
[0.03085, 0.03868],
[0.08544, 0.22305],
[0.08228, 0.04386],
[0.13027, 0.07118],
[0.1097, 0.0599],
[0.05485, 0.06649],
[0.04114, 0.04383],
[0.04114, 0.04739],
[0.0519, 0.03925],
[0.06856, 0.08396],
[0.05485, 0.06236],
[0.03428, 0.01071],
[0.03397, 0.0494],
[0.03777, 0.05492],
[0.04766, 0.02074],
[0.03428, 0.0486],
[0.00069, 0.001],
[0.00069, 0.001],
[0.00069, 0.001],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"LLL": [
"Fire",
"Lightning_strike",
"Electronics",
"Burglary",
"Theft",
"Robbery_Theft_Assault",
"Outflow",
"Cloudburst",
"Goods_in_Refrigerator_freezer_damage",
"Traffic_accidents",
"Sudden_damage",
"Storm",
"Vandalism",
"Liability",
"Legal_Aid",
"Travel_cover",
"Glass_and_bowl",
"Theft_or_damage_to_bicycle",
"Accident_in_boat",
"Other",
],
},
)
cpd_MMM = TabularCPD(
variable="MMM",
variable_card=7,
values=[
[0.127, 0.15845],
[0.35573, 0.4321],
[0.11577, 0.11381],
[0.10126, 0.07723],
[0.0922, 0.06447],
[0.09958, 0.07599],
[0.10846, 0.07795],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"MMM": [
"<=1",
"1-10",
"10-30",
"30-60",
"60-90",
"90-180",
"180<",
],
},
)
cpd_NNN = TabularCPD(
variable="NNN",
variable_card=2,
values=[[0.14546, 0.06571], [0.85454, 0.93429]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "NNN": ["true", "false"]},
)
cpd_OOO = TabularCPD(
variable="OOO",
variable_card=2,
values=[
[
0.5,
0.5,
0.5,
0.7397,
0.7397,
0.7397,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.7397,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.8981,
0.8981,
0.8981,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.8981,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
[
0.5,
0.5,
0.5,
0.2603,
0.2603,
0.2603,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.2603,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.1019,
0.1019,
0.1019,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.1019,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
],
evidence=["DESIRED", "LLL"],
evidence_card=[2, 20],
state_names={
"DESIRED": ["Yes", "No"],
"OOO": ["true", "false"],
"LLL": [
"Fire",
"Lightning_strike",
"Electronics",
"Burglary",
"Theft",
"Robbery_Theft_Assault",
"Outflow",
"Cloudburst",
"Goods_in_Refrigerator_freezer_damage",
"Traffic_accidents",
"Sudden_damage",
"Storm",
"Vandalism",
"Liability",
"Legal_Aid",
"Travel_cover",
"Glass_and_bowl",
"Theft_or_damage_to_bicycle",
"Accident_in_boat",
"Other",
],
},
)
cpd_PPP = TabularCPD(
variable="PPP",
variable_card=2,
values=[
[
0.5,
0.5,
0.5,
0.2603,
0.2603,
0.2603,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.2603,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.1019,
0.1019,
0.1019,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.1019,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
[
0.5,
0.5,
0.5,
0.7397,
0.7397,
0.7397,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.7397,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.8981,
0.8981,
0.8981,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.8981,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
],
evidence=["DESIRED", "LLL"],
evidence_card=[2, 20],
state_names={
"DESIRED": ["Yes", "No"],
"PPP": ["true", "false"],
"LLL": [
"Fire",
"Lightning_strike",
"Electronics",
"Burglary",
"Theft",
"Robbery_Theft_Assault",
"Outflow",
"Cloudburst",
"Goods_in_Refrigerator_freezer_damage",
"Traffic_accidents",
"Sudden_damage",
"Storm",
"Vandalism",
"Liability",
"Legal_Aid",
"Travel_cover",
"Glass_and_bowl",
"Theft_or_damage_to_bicycle",
"Accident_in_boat",
"Other",
],
},
)
cpd_RRR = TabularCPD(
variable="RRR",
variable_card=2,
values=[
[
0.5,
0.5,
0.5,
0.0909,
0.0909,
0.0909,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.0909,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.0364,
0.0364,
0.0364,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.0364,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
[
0.5,
0.5,
0.5,
0.9091,
0.9091,
0.9091,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.9091,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.9636,
0.9636,
0.9636,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.9636,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
],
],
evidence=["DESIRED", "LLL"],
evidence_card=[2, 20],
state_names={
"DESIRED": ["Yes", "No"],
"RRR": ["true", "false"],
"LLL": [
"Fire",
"Lightning_strike",
"Electronics",
"Burglary",
"Theft",
"Robbery_Theft_Assault",
"Outflow",
"Cloudburst",
"Goods_in_Refrigerator_freezer_damage",
"Traffic_accidents",
"Sudden_damage",
"Storm",
"Vandalism",
"Liability",
"Legal_Aid",
"Travel_cover",
"Glass_and_bowl",
"Theft_or_damage_to_bicycle",
"Accident_in_boat",
"Other",
],
},
)
cpd_SSS = TabularCPD(
variable="SSS",
variable_card=2,
values=[[0.0909, 0.5, 0.001347, 0.5], [0.9091, 0.5, 0.998653, 0.5]],
evidence=["DESIRED", "NNN"],
evidence_card=[2, 2],
state_names={
"DESIRED": ["Yes", "No"],
"SSS": ["true", "false"],
"NNN": ["true", "false"],
},
)
cpd_TTT = TabularCPD(
variable="TTT",
variable_card=3,
values=[[0.07272, 0.04179], [0.1961, 0.17937], [0.73119, 0.77884]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"TTT": ["<5", "5-15", "15<"],
},
)
cpd_UUU = TabularCPD(
variable="UUU",
variable_card=6,
values=[
[0.71140, 0.75101],
[0.20408, 0.18553],
[0.05894, 0.04534],
[0.02251, 0.01608],
[0.00294, 0.00196],
[0.00013, 0.00008],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"UUU": ["<=1", "1-2", "2-3", "3-5", "5-8", "8<"],
},
)
cpd_VVV = TabularCPD(
variable="VVV",
variable_card=7,
values=[
[0.0344, 0.0229023068706921],
[0.0126, 0.00900090270027081],
[0.0145, 0.011201103360331],
[0.0150, 0.0130013039003912],
[0.0118, 0.010701103210331],
[0.1182, 0.118212035463611],
[0.7935, 0.814981244494373],
],
evidence=["DESIRED"],
evidence_card=[2],
state_names={
"DESIRED": ["Yes", "No"],
"VVV": [
"<=1",
"1-10",
"10-30",
"30-60",
"60-90",
"90-99998",
"99998<",
],
},
)
cpd_ZZZ = TabularCPD(
variable="ZZZ",
variable_card=2,
values=[[0.005, 0.0013], [0.995, 0.9987]],
evidence=["DESIRED"],
evidence_card=[2],
state_names={"DESIRED": ["Yes", "No"], "ZZZ": ["true", "false"]},
)
# Associating the CPDs with the network
model.add_cpds(
cpd_DESIRED,
cpd_AAA,
cpd_BBB,
cpd_CCC,
cpd_DDD,
cpd_EEE,
cpd_FFF,
cpd_GGG,
cpd_HHH,
cpd_III,
cpd_JJJ,
cpd_KKK,
cpd_LLL,
cpd_MMM,
cpd_NNN,
cpd_OOO,
cpd_PPP,
cpd_RRR,
cpd_SSS,
cpd_TTT,
cpd_UUU,
cpd_VVV,
cpd_ZZZ,
cpd_XXX,
)
# check_model checks for the network structure and CPDs and verifies that the CPDs are correctly
# defined and sum to 1.
model.check_model()
model.save("test.bif", filetype="bif")
upload_model = BayesianNetwork.load('test.bif', filetype='bif') |
Beta Was this translation helpful? Give feedback.
All reactions
-
@ankurankan Thanks! I didn't know that and yes, it fixed the problem. Thank you! |
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, it wasn't clear from the documentation, and I have added a line mentioning this. |
Beta Was this translation helpful? Give feedback.
@jonasvit Would it be possible to share your bif file? It seems to be working for the alarm model in the example above.