.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/causal_inf.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_causal_inf.py: =========================== LinGAM =========================== https://lingam.readthedocs.io https://www.kaggle.com/code/sasakitetsuya/water-quality-causal-inference-by-lingam https://github.com/KJMAN678/casual_reasoning_lignum/blob/master/LIGNAM.ipynb .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python from IPython.display import Image import lingam from lingam.utils import make_dot, make_prior_knowledge from sklearn.linear_model import LinearRegression from sklearn.ensemble import RandomForestRegressor from utils import prepare_data .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python df, _, _ = prepare_data() .. GENERATED FROM PYTHON SOURCE LINES 27-34 .. code-block:: Python df_list = df.columns.to_list() df_dict = {} for i, column in zip(range(len(df_list)), df_list): df_dict[column] = i .. GENERATED FROM PYTHON SOURCE LINES 35-50 .. code-block:: Python prior_knowledge = make_prior_knowledge( n_variables=15, exogenous_variables = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, ], sink_variables=[df_dict['Efficiency (%)']], # paths = [ # [df_dict["pH (Max)"], df_dict["pH (Min)"],[df_dict['Temperature\n?C (Min)'],df_dict['Temperature\n?C (Max)'],df_dict['Dissolved Oxygen (mg/L) (Min)'],df_dict['Dissolved Oxygen (mg/L) (Max)']]], # ], no_paths=[(df_dict['Efficiency (%)'], df_dict['Ci (mg/L)']), ], ) .. GENERATED FROM PYTHON SOURCE LINES 51-60 .. code-block:: Python model = lingam.DirectLiNGAM( random_state=42, measure = 'pwling', prior_knowledge = prior_knowledge ) model.fit(df) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 61-73 .. code-block:: Python dot = make_dot( model.adjacency_matrix_, labels=df.columns.to_list(), prediction_target_label='Efficiency (%)' ) dot.format = 'png' #dot.render('dag') #Image("dag1.png") .. GENERATED FROM PYTHON SOURCE LINES 74-95 .. code-block:: Python prior_knowledge = make_prior_knowledge( n_variables=15, exogenous_variables = [ df_dict["Catalyst type"], df_dict['Catalyst loading (g/L)'], df_dict['Light intensity (W)'], df_dict['time (min)'], df_dict['solution pH'], df_dict['Ci (mg/L)'], df_dict['Anions'], ], sink_variables=[df_dict['Efficiency (%)']], # paths = [ # [df_dict["pH (Max)"], df_dict["pH (Min)"],[df_dict['Temperature\n?C (Min)'],df_dict['Temperature\n?C (Max)'],df_dict['Dissolved Oxygen (mg/L) (Min)'],df_dict['Dissolved Oxygen (mg/L) (Max)']]], # ], no_paths=[(df_dict['Efficiency (%)'], df_dict['Ci (mg/L)']), ], ) .. GENERATED FROM PYTHON SOURCE LINES 96-105 .. code-block:: Python model = lingam.DirectLiNGAM( random_state=42, measure = 'pwling', prior_knowledge = prior_knowledge ) model.fit(df) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 106-118 .. code-block:: Python dot = make_dot( model.adjacency_matrix_, labels=df.columns.to_list(), prediction_target_label='Efficiency (%)' ) dot.format = 'png' # dot.render('dag1') #Image("dag1.png") .. GENERATED FROM PYTHON SOURCE LINES 119-138 .. code-block:: Python reg = LinearRegression(fit_intercept=True) reg.fit(df.drop(["Efficiency (%)"], axis=1), df["Efficiency (%)"]) features = [i for i in range(df.drop(["Efficiency (%)"], axis=1).shape[1])] print("features: ", features) dot = make_dot( model.adjacency_matrix_, labels=df.columns.to_list(), prediction_feature_indices=features, prediction_target_label='Efficiency (%)', prediction_coefs=reg.coef_ ) dot.format = 'png' # dot.render('dag_lr') dot .. rst-class:: sphx-glr-script-out .. code-block:: none features: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] .. GENERATED FROM PYTHON SOURCE LINES 139-158 .. code-block:: Python reg = RandomForestRegressor() reg.fit(df.drop(["Efficiency (%)"], axis=1), df["Efficiency (%)"]) features = [i for i in range(df.drop(["Efficiency (%)"], axis=1).shape[1])] print("features: ", features) dot = make_dot( model.adjacency_matrix_, labels=df.columns.to_list(), prediction_feature_indices=features, prediction_target_label='Efficiency (%)', prediction_coefs=reg.feature_importances_ ) dot.format = 'png' # dot.render('dag_rf') dot .. rst-class:: sphx-glr-script-out .. code-block:: none features: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.118 seconds) .. _sphx_glr_download_auto_examples_causal_inf.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: causal_inf.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: causal_inf.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: causal_inf.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_