<jupyter_start><jupyter_text>QuickstartIn this short quickstart, we will demonstrate the basics of using the **Copulas** library to generate a synthetic dataset.  1. Load the dataWe'll start by loading a built-in dataset from the `copulas.datasets` module.<jupyter_code>import warnings

warnings.filterwarnings('ignore')
from copulas.datasets import sample_trivariate_xyz

data = sample_trivariate_xyz()
data.head()<jupyter_output><empty_output><jupyter_text>We can visualize this dataset using a 3D scatter plot.<jupyter_code>from copulas.visualization import scatter_3d

scatter_3d(data)<jupyter_output><empty_output><jupyter_text>2. Create a Copula instanceNext, we need to import Copulas and create an instance of the `GaussianMultivariate` class with the default arguments. This will allow us to model this dataset using a Gaussian copula where the marginal distributions will be automatically estimated.<jupyter_code>from copulas.multivariate import GaussianMultivariate

copula = GaussianMultivariate()<jupyter_output><empty_output><jupyter_text>3. Fit the modelOnce we have a `GaussianMultivariate` instance, we can call the `fit` method with `data`. This will estimate the marginal and joint distributions, allowing us to model the dataset.<jupyter_code>copula.fit(data)<jupyter_output><empty_output><jupyter_text>4. Sample new dataOnce the model has been fitted, we are ready to generate new samples by calling the `sample` method of the `Copulas` instance and specifying the number of data points we want to sample.<jupyter_code>num_samples = 1000

synthetic_data = copula.sample(num_samples)
synthetic_data.head()<jupyter_output><empty_output><jupyter_text>The returned object, `synthetic_data`, is a `pandas.DataFrame` containing a table of synthetic data with the same format as the input data and 1000 rows as we requested. We can plot the synthetic data and visually compare it to the real data.<jupyter_code>from copulas.visualization import compare_3d

compare_3d(data, synthetic_data)<jupyter_output><empty_output><jupyter_text>5. Load and save a modelFor some copula models the fitting process can take a lot of time, so we probably would like toavoid having to fit every we want to generate samples. Instead we can fit a model once, save it,and load it every time we want to sample new data.If we have a fitted model, we can save it by calling it's `save` method, that only takesas argument the path where the model will be stored. Similarly, the `load` allows to loada model stored on disk by passing as argument the path where the model is stored.<jupyter_code>model_path = 'mymodel.pkl'
copula.save(model_path)<jupyter_output><empty_output><jupyter_text>Once the model is saved, it can be loaded back as a **Copulas** instance by using the `load`method. Note that you need to load it using the same class that was used to save it.<jupyter_code>new_copula = GaussianMultivariate.load(model_path)<jupyter_output><empty_output><jupyter_text>At this point we could use this model instance to generate more samples.<jupyter_code>new_samples = new_copula.sample(num_samples)<jupyter_output><empty_output><jupyter_text>6. Extract and set parametersIn some cases it's more useful to obtain the parameters from a fitted copula than to save and load from disk. Once our copula is fitted, we can extract it's parameters using the `to_dict` method:<jupyter_code>copula_params = copula.to_dict()<jupyter_output><empty_output><jupyter_text>This will return a dictionary containing all the copula parameters: Once we have all the parameters we can create a new identical **Copula** instance by using the method `from_dict`:<jupyter_code>new_copula = GaussianMultivariate.from_dict(copula_params)<jupyter_output><empty_output><jupyter_text>At this point we could use this model instance to generate more samples.<jupyter_code>new_samples = new_copula.sample(num_samples)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06371545547594677, 'fraction_numerical': 0.004861821903787103, 'mean_word_length': 6.74059405940594, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '349963', 'license': 'MIT', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1105, 'n_words': 444, 'path': 'tutorials/00_Quickstart.ipynb', 'repo_name': 'ppeddada97/Copulas', 'size': '3908'}