# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd

# +
# Load Insolver

from insolver.frame import InsolverDataFrame
from insolver.transforms import InsolverTransform
from insolver.transforms import (
    TransformPolynomizer,
    TransformAge,
    TransformExp,
    TransformMapValues,
    TransformGetDummies,
    TransformAgeGender,
)

# +
# Load Data

df = pd.read_csv('freMPL-R.csv', low_memory=False)
# -

# # InsolverDataFrame

ins_df = InsolverDataFrame(df)

ins_df.head()

ins_df.info()

ins_df.get_meta_info()


# # InsolverTransforms

# +
# User changes for the existing transform

@staticmethod
def exp_new(exp, exp_max):
    if pd.isnull(exp):
        exp = None
    elif exp < 0:
        exp = None
    else:
        exp = exp * 30 // 365
    if exp > exp_max:
        exp = exp_max
    return exp

TransformExp._exp = exp_new


# +
# New user transform

class TransformSocioCateg:
    def __init__(self, column_socio_categ):
        self.priority = 0
        self.column_socio_categ = column_socio_categ

    def __call__(self, df):
        df[self.column_socio_categ] = df[self.column_socio_categ].str.slice(0,4)
        return df


# +
# Initialising transforms class

ins_transf = InsolverTransform(df, [
    TransformPolynomizer('DrivAge'),
    TransformAge('DrivAge', 18, 75),
    TransformExp('LicAge', 57),
    TransformMapValues('Gender', {'Male':0, 'Female':1}),
    TransformMapValues('MariStat', {'Other':0, 'Alone':1}),
    TransformSocioCateg('SocioCateg'),
    TransformGetDummies('SocioCateg'),
    TransformAgeGender('DrivAge', 'Gender', 'Age_m', 'Age_f', age_default=18, gender_male=0, gender_female=1),
    TransformPolynomizer('Age_m'),
    TransformPolynomizer('Age_f'),
    ])

# +
# Applying transforms

ins_transf.ins_transform()
# -

ins_transf.save('transforms_freMPL-R.pkl')

ins_transf.head(7)

ins_transf.info()



StarcoderdataJupyterScriptsDedupFiltered