<jupyter_start><jupyter_text>ExamplesThe core function of this package is `design_matrices()`. It returns an object of class `DesignMatrices` that contains information about the response, common effects and group specific effects.<jupyter_code>import pandas as pd
import numpy as np

from formulae import design_matrices
np.random.seed(1234)
SIZE = 20
CNT = 20
data = pd.DataFrame(
    {
        'x': np.random.normal(size=SIZE), 
        'y': np.random.normal(size=SIZE),
        'z': np.random.normal(size=SIZE),
        '$2#abc': np.random.normal(size=SIZE),
        'g1': np.random.choice(['a', 'b', 'c'], SIZE),
        'g2': np.random.choice(['YES', 'NO'], SIZE)
    }
)<jupyter_output><empty_output><jupyter_text>We can use both functions taht are loaded in the top environment as well as non-syntactic names passed within \`\`. Specification of group specific effects is much like what you have in R package `lme4`.<jupyter_code>design = design_matrices("y ~ np.exp(x) + `$2#abc` + (z|g1)", data)
print(design.response)
print(design.response.design_vector)
print(design.common)
print(design.common.design_matrix)
print(design.common['$2#abc'])
print(design.group)
print(design.group.design_matrix) # note it is a sparse matrix<jupyter_output>GroupEffectsMatrix(
  shape: (40, 6),
  terms: {
    '1|g1': {type=Intercept, idxs=(slice(0, 20, None), slice(0, 3, None))},
    'z|g1': {type=numeric, idxs=(slice(20, 40, None), slice(3, 6, None))}
  }
)
  (0, 0)	1.0
  (1, 0)	1.0
  (7, 0)	1.0
  (9, 0)	1.0
  (13, 0)	1.0
  (14, 0)	1.0
  (16, 0)	1.0
  (17, 0)	1.0
  (18, 0)	1.0
  (2, 1)	1.0
  (3, 1)	1.0
  (6, 1)	1.0
  (10, 1)	1.0
  (11, 1)	1.0
  (12, 1)	1.0
  (15, 1)	1.0
  (19, 1)	1.0
  (4, 2)	1.0
  (5, 2)	1.0
  (8, 2)	1.0
  (20, 3)	0.07619958783723642
  (21, 3)	-0.5664459304649568
  (27, 3)	0.018289191349219306
  (29, 3)	0.2152685809694434
  (33, 3)	-0.10091819994891389
  (34, 3)	-0.5482424491868549
  (36, 3)	0.3540203321992379
  (37, 3)	-0.0355130252781402
  (38, 3)	0.5657383060625951
  (22, 4)	0.036141936684072715
  (23, 4)	-2.0749776006900293
  (26, 4)	-0.1367948332613474
  (30, 4)	0.841008794931391
  (31, 4)	-1.4458100770443063
  (32, 4)	-1.4019732815008439
  (35, 4)	-0.14461950836938436
  (39, 4)	1.5456588046255575
  (24, 5)	0.24779[...]<jupyter_text>$Z$ matrix can be subsetted by passing the name of the group specific term.<jupyter_code>print(design.group['z|g1'])<jupyter_output>[[ 0.07619959  0.          0.        ]
 [-0.56644593  0.          0.        ]
 [ 0.          0.03614194  0.        ]
 [ 0.         -2.0749776   0.        ]
 [ 0.          0.          0.2477922 ]
 [ 0.          0.         -0.89715678]
 [ 0.         -0.13679483  0.        ]
 [ 0.01828919  0.          0.        ]
 [ 0.          0.          0.75541398]
 [ 0.21526858  0.          0.        ]
 [ 0.          0.84100879  0.        ]
 [ 0.         -1.44581008  0.        ]
 [ 0.         -1.40197328  0.        ]
 [-0.1009182   0.          0.        ]
 [-0.54824245  0.          0.        ]
 [ 0.         -0.14461951  0.        ]
 [ 0.35402033  0.          0.        ]
 [-0.03551303  0.          0.        ]
 [ 0.56573831  0.          0.        ]
 [ 0.          1.5456588   0.        ]]<jupyter_text>Reference class exampleThis feature is taken from current Bambi behavior (you don't find it in Patsy or formulaic)<jupyter_code>design = design_matrices('g2[YES] ~ x', data)
print(design.response)
print(design.response.design_vector)<jupyter_output>ResponseVector(name=g2, type=categoric, length=20, refclass=YES)
[[0]
 [1]
 [1]
 [0]
 [0]
 [0]
 [1]
 [0]
 [0]
 [0]
 [0]
 [0]
 [0]
 [1]
 [1]
 [0]
 [1]
 [0]
 [0]
 [1]]
{'fraction_non_alphanumeric': 0.16348018841784429, 'fraction_numerical': 0.19894707675256304, 'mean_word_length': 2.2848043676069154, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '456228', 'license': 'MIT', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1563, 'n_words': 367, 'path': 'formulae/examples/Examples.ipynb', 'repo_name': 'aloctavodia/formulae', 'size': '3609'}