Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#python: Can't have the same variables in both variables and evidence. Found in both: {'sri3'} #1706

Open
Ramoking90 opened this issue Nov 21, 2023 · 0 comments

Comments

@Ramoking90
Copy link

Subject of the issue

Describe your issue here.
from pgmpy.models import BayesianModel
from pgmpy.estimators import ParameterEstimator
from pgmpy.inference import VariableElimination
import pandas as pd
import numpy as np

Your data

data = np.array([
[-1.08686015, -0.530364815, 0.299166617],
[-0.580628786, -0.451385489, -0.120357876],
[-0.874023552, -0.738927083, -1.271648386],
[-0.579576457, -0.961157333, -1.781973874],
[-0.706610556, -0.672701556, -1.300238383],
[-0.407183586, -0.332663923, -0.093809005],
[-0.854222989, -0.798132005, -0.026577348],
[-0.236578751, -0.127636263, -0.494304277],
[0.184535812, 0.230669242, -1.137907389],
[0.806986105, 0.69534769, -0.073109508],
[0.279503287, 0.071437308, 0.379095467],
[-0.29583904, -0.505550236, 0.924486107]
])

Convert your data to a dataframe

df = pd.DataFrame(data, columns=['sri3', 'spi3', 'swli3'])

Define your Bayesian Network structure

chess_players_model = BayesianModel([('sri3', 'spi3'), ('sri3', 'swli3'), ('spi3', 'swli3')])

Fit the data to the model

chess_players_model.fit(df, estimator=ParameterEstimator)

Create VariableElimination object after fitting the model

inference = VariableElimination(chess_players_model)

Define the variable names

variables = ['sri3', 'spi3', 'swli3']

Iterate over each row

for index, row in df.iterrows():
print(f"Row {index + 1} - Data: {row.values}")

# Create a query dictionary
query_data = {variable: value for variable, value in zip(variables, row.values)}

# Query the conditional probability
cpd_matrix = pd.DataFrame(index=row.index, columns=row.index)
for var1 in variables:
    for var2 in variables:
        query_data_cp = query_data.copy()
        query_data_cp[var1] = query_data_cp[var2] = 'value'
        phi_query = inference.map_query(variables=variables, evidence=query_data_cp, show_progress=False) #this where i am getting error
        cpd_matrix.loc[var1, var2] = phi_query.values[0]

print("Conditional Probability Matrix:")
print(cpd_matrix)
print()

Your environment

  • pgmpy version
  • Python version
  • Operating System

Steps to reproduce

Tell us how to reproduce this issue. Please provide a minimal reproducible code of
the issue you are facing if possible.

Expected behaviour

Tell us what should happen
the code shoulg give the conditional probability of spi3, sri3 and swli3 in matrix form

Actual behaviour

#python: Can't have the same variables in both variables and evidence. Found in both: {'sri3'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant