<jupyter_start><jupyter_text>Training and Evaluating Simple Model  Generate data<jupyter_code>random.seed(131)
dates = pd.DataFrame( {"score_date": pd.date_range("2016-01-01", "2016-12-31")} )
ids = pd.DataFrame( {"id": np.arange(0, 100)} )

# make keys for cross join
dates["key"] = 1
ids["key"] = 1

data = pd.merge(ids, dates).drop("key", axis=1)

data["x1"] = 23*random.randn(data.shape[0]) + 500
data["x2"] = 59*random.randn(data.shape[0]) + 235
# Noise Feature
data["x3"] = 73*random.randn(data.shape[0]) + 793
data["y"] = 0.37 * data["x1"] +\
            0.97*data["x2"] + 0.32*data["x2"]**2 -\
            5 * data["id"] * 0.2 +\
            np.cos(pd.to_datetime(data["score_date"]).astype(int) * 200) * 20
            
# inject NaNs
nan_idx = np.random.randint(0, data.shape[0], size=100)
data.loc[nan_idx, "x1"] = np.nan

nan_idx = np.random.randint(0, data.shape[0], size=100)
data.loc[nan_idx, "x2"] = np.nan
data.head()
data["y"][data["id"] == 0].plot()
plt.show()<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>from fklearn.preprocessing.splitting import space_time_split_dataset
train_start = "2016-01-01"
train_end = "2016-06-30"
holdout_end = "2016-12-31"

split_fn = space_time_split_dataset(train_start_date=train_start,
                                     train_end_date=train_end,
                                     holdout_end_date=holdout_end,
                                     split_seed=42, space_holdout_percentage=.05,
                                     space_column="id", time_column="score_date")
train_set, intime_outspace_hdout, outime_inspace_hdout, outime_outspace_hdout = split_fn(data)

train_set.shape, intime_outspace_hdout.shape, outime_inspace_hdout.shape, outime_outspace_hdout.shape<jupyter_output><empty_output><jupyter_text>Define learner function<jupyter_code>FEATURES = ["x1", "x2", "x3"]
TARGET = ["y"]
from fklearn.training.imputation import imputer
my_imputer = imputer(columns_to_impute=FEATURES, impute_strategy="median")
from fklearn.training.transformation import standard_scaler
my_scaler = standard_scaler(columns_to_scale=FEATURES)
from fklearn.training.regression import catboost_regressor_learner
my_model = catboost_regressor_learner(features=['x1', 'x2', 'x3'], target='y', prediction_column='prediction', extra_params={'random_seed': 139, 'thread_count': 8})
reload_all('fklearn')
from fklearn.training.transformation import ecdfer
my_ecdefer = ecdfer(prediction_column="prediction", ecdf_column="prediction_ecdf")
my_learner = build_pipeline(my_imputer, my_scaler, my_model, my_ecdefer)<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>(prediction_function, _, logs) = my_learner(train_set)<jupyter_output><empty_output><jupyter_text>Evaluate<jupyter_code>my_evaluator = combined_evaluators(evaluators=[
    mean_prediction_evaluator(prediction_column='prediction'),
    r2_evaluator(prediction_column='prediction', target_column='y'),
    mse_evaluator(prediction_column='prediction', target_column='y'),
])
test_predictions = prediction_function(outime_outspace_hdout)
# first argument (training_data) is optional. Only some kinds of evaluators need it. (e.g. learning curve)
# I should probably get rid of it and come up with a better way to pass training information for evaluation
my_evaluator(test_predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13412816691505217, 'fraction_numerical': 0.03576751117734724, 'mean_word_length': 5.392380952380952, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '639121', 'license': 'Apache-2.0', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1197, 'n_words': 193, 'path': 'notebooks/catboost_demo.ipynb', 'repo_name': 'erickisos/fklearn', 'size': '3355'}