Мне нужно изучить байесовскую сеть и собрать из нее кучу синтетических данных.
Я смоделировал из нее кадр данных и изучил сеть.
Однако почему этот фрагмент кода выдает ошибку?
Вот ошибка:
File "c:\Users\a-rotalintiy\PhD\correlations\import pandas as pd.py", line 38, in
synthetic_data = sampler.forward_sample(size=synthetic_data_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\a-rotalintiy\.virtualenvs\correlations-k4Gxn6_V\Lib\site-packages\pgmpy\sampling\Sampling.py", line 125, in forward_sample
sampled[node] = sample_discrete_maps(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\a-rotalintiy\.virtualenvs\correlations-k4Gxn6_V\Lib\site-packages\pgmpy\utils\mathext.py", line 194, in sample_discrete_maps
samples[(weight_indices == weight_index)] = np.random.choice(
~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: too many indices for array: array is 1-dimensional, but 3 were indexed
Вот код:
import pandas as pd
from pgmpy.models import BayesianNetwork
from pgmpy.estimators import HillClimbSearch, BicScore, BayesianEstimator
from pgmpy.sampling import BayesianModelSampling
# Sample DataFrame
data = {
'A': [1, 0, 1, 0, 1],
'B': [0, 1, 0, 1, 0],
'C': [1, 1, 0, 0, 1]
}
df = pd.DataFrame(data)
# Ensure the DataFrame is correctly structured
print("DataFrame shape:", df.shape)
print("DataFrame head:\n", df.head())
# Estimate the model structure
hc = HillClimbSearch(df)
scoring_method = BicScore(df)
best_model = hc.estimate(scoring_method=scoring_method)
# Print learned edges
print("Learned edges:", best_model.edges())
# Create and fit the Bayesian model
bn_model = BayesianNetwork(best_model.edges())
bn_model.fit(df, estimator=BayesianEstimator, prior_type="BDeu")
# Ensure the model is fitted correctly
for cpd in bn_model.get_cpds():
print(f"CPD of {cpd.variable}:")
print(cpd)
# Sample synthetic data
sampler = BayesianModelSampling(bn_model)
synthetic_data_size = len(df) # You can adjust this size as needed
synthetic_data = sampler.forward_sample(size=synthetic_data_size)
print("Synthetic Data Sample:\n", synthetic_data)
Подробнее здесь: https://stackoverflow.com/questions/787 ... y-array-is