<jupyter_start><jupyter_text>DAGRegressors  Predict a set of house prices given a small set of features<jupyter_code>import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
print(load_boston(return_X_y=False)["DESCR"])
X, y = load_boston(return_X_y=True)
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
X = ss.fit_transform(X) # z-score features
y = (y - y.mean()) / y.std() # z-score target<jupyter_output><empty_output><jupyter_text>Linear DAG regressor<jupyter_code>from causalnex.structure import DAGRegressor

from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score

X, y = load_boston(return_X_y=True)

reg = DAGRegressor(
                alpha=0.1,  # L1 regularizer
                beta=0.9,  # L2 regularizer
                fit_intercept=True,  
                hidden_layer_units=None,  # linear model
                dependent_target=True,  # enforce y to be a child node
                enforce_dag=True,  # threshold the model structure to be a DAG (visualization only)
    standardize=True,
                 )
X_pd = pd.DataFrame(X, columns=load_boston(return_X_y=False)["feature_names"])
y_pd = pd.Series(y, name="price")
reg.fit(X_pd, y_pd)
print(pd.Series(reg.coef_, index=load_boston(return_X_y=False)["feature_names"]))
reg.plot_dag(True)
scores = cross_val_score(reg, X, y, cv=KFold(shuffle=True, random_state=42))
print(f'MEAN R2: {np.mean(scores).mean():.3f}')<jupyter_output>MEAN R2: 0.706<jupyter_text>Nonlinear DAGRegressor<jupyter_code>X, y = load_boston(return_X_y=True)

reg = DAGRegressor(threshold=0.0,
                    alpha=0.0,
                    beta=0.5,
                    fit_intercept=True,
                    hidden_layer_units=[5], # 1 hidden layer with 5 neurons
                    standardize=True,
                   dependent_target=True,
                 )
X_pd = pd.DataFrame(X, columns=load_boston(return_X_y=False)["feature_names"])
y_pd = pd.Series(y, name="price")
reg.fit(X_pd, y_pd)

reg.plot_dag(True)
scores = cross_val_score(reg, X, y, cv=KFold(shuffle=True, random_state=42))
print(f'MEAN R2: {np.mean(scores).mean():.3f}')<jupyter_output>MEAN R2: 0.842<jupyter_text>`causalnex` provides `.coef_` and `.feature_importances_`, but the documentation is unclear how these are defined<jupyter_code>reg.coef_
reg.feature_importances_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11217282924802659, 'fraction_numerical': 0.013710012463647694, 'mean_word_length': 3.8548387096774195, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '202553', 'license': 'MIT', 'n_tokens_mistral': 888, 'n_tokens_neox': 856, 'n_words': 165, 'path': 'Notebooks/causalnex-DAG-regressors.ipynb', 'repo_name': 'jaryaman/ML_demos', 'size': '2407'}