<jupyter_start><jupyter_text>Synthetic Data for Machine LearningIn this tutorial, we'll demonstrate how to generate a synthetic copy of the classic Boston housing prices dataset. We will train a simple linear model on the synthetic data and demonstrate that the model's performance is competitive not just on the synthetic dataset but also the real dataset.  Loading the datasetThe Boston housing prices dataset is available through `sklearn`. We'll import it here and divide it into a train/test set.<jupyter_code>import warnings

warnings.filterwarnings('ignore')

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split

X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)<jupyter_output><empty_output><jupyter_text>Generating synthetic dataNext, we'll use a Gaussian copula to generate a **synthetic training set**. This simulates a scenario where a company may be unwilling to share the real dataset but is willing to release a synthetic copy which preserves many of the real dataset's properties for researchers to use.<jupyter_code>import numpy as np

from copulas.multivariate import GaussianMultivariate

def create_synthetic(X, y):
    """
    This function combines X and y into a single dataset D, models it
    using a Gaussian copula, and generates a synthetic dataset S. It 
    returns the new, synthetic versions of X and y.
    """
    dataset = np.concatenate([X, np.expand_dims(y, 1)], axis=1)
    
    model = GaussianMultivariate()
    model.fit(dataset)
    
    synthetic = model.sample(len(dataset))
    
    X = synthetic.values[:, :-1]
    y = synthetic.values[:, -1]
    
    return X, y

X_synthetic, y_synthetic = create_synthetic(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Training a linear modelNow we can train a simple linear model using the synthetic dataset.<jupyter_code>from sklearn.linear_model import ElasticNet

model = ElasticNet()
model.fit(X_synthetic, y_synthetic)<jupyter_output><empty_output><jupyter_text>Now, we can take this model - which is trained on the synthetic training set - and evaluate it's performance on the real test set.<jupyter_code>print(model.score(X_test, y_test))<jupyter_output>0.574182672682398<jupyter_text>For comparison, here's a model that's trained on the real training set.<jupyter_code>model = ElasticNet()
model.fit(X_train, y_train);
print(model.score(X_test, y_test))<jupyter_output>0.5949580051414429
{'fraction_non_alphanumeric': 0.07220653489310205, 'fraction_numerical': 0.014925373134328358, 'mean_word_length': 5.342710997442455, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '289156', 'license': 'MIT', 'n_tokens_mistral': 771, 'n_tokens_neox': 722, 'n_words': 267, 'path': 'tutorials/04_Syntehtic_Data_for_Machine_Learning.ipynb', 'repo_name': 'ppeddada97/Copulas', 'size': '2479'}