mckinsey / causalnex

A Python library that helps data scientists to infer causation rather than observing correlation.
http://causalnex.readthedocs.io/
Other
2.24k stars 258 forks source link

Tutorial raises error on conditional probability distributions #21

Closed ogencoglu closed 3 years ago

ogencoglu commented 4 years ago

Description

Following the tutorial raises the following:

bn = bn.fit_cpds(train, method="BayesianEstimator", bayes_prior="K2")

Would be great to have a fully working jupyter notebook as an example.

Steps to Reproduce

/usr/local/lib/python3.7/site-packages/causalnex/network/network.py in fit_cpds(self, data, method, bayes_prior, equivalent_sample_size)
    344 
    345         transformed_data = data.copy(deep=True)  # type: pd.DataFrame
--> 346         transformed_data = self._state_to_index(transformed_data[self.nodes])
    347 
    348         if method == "MaximumLikelihoodEstimator":

/usr/local/lib/python3.7/site-packages/causalnex/network/network.py in _state_to_index(self, df, nodes)
    307         cols = nodes if nodes else df.columns
    308         for col in cols:
--> 309             df[col] = df[col].map(self._node_states[col])
    310         df.is_copy = True
    311         return df

TypeError: 'NoneType' object is not subscriptable

Your Environment

benhorsburgh commented 4 years ago

Hi @ogencoglu thanks for your issue. This is a known bug when calling fit_cpds if there are states in the training set that are not in the test set, or the other way around. I'm hoping to release a fix for this today, when I will let you know it is resolved.

In the mean time, you can either run the tutorial without the train / test split, or try re-running it a few times until it works (that is, until you randomly get all states in both train and test).

Ben

ogencoglu commented 4 years ago

Thanks for the swift reply!

oentaryorj commented 3 years ago

Fixed in the new release. Full running notebooks are available at: https://github.com/quantumblacklabs/causalnex/tree/develop/docs/source/03_tutorial