import numpy as np import pandas as pd
# set seed for reproducibility, this will ensure that the random numbers generated are the same each time the code is run
np.random.seed(0)
# generate skewed variable
x = np.random.lognormal(mean=1, sigma=1, size=998)
# add two zeros for the exercise
x = np.concatenate([x, np.zeros(2)])
# into a data frame for ease of visual
df = pd.DataFrame({
"x": x
})
df.head()| x | |
|---|---|
| 0 | 15.863999 |
| 1 | 4.055838 |
| 2 | 7.233608 |
| 3 | 25.556539 |
| 4 | 17.594001 |
# lets see what that looks like df['x'].hist(bins=30);
def my_function(): # define inputs here # function task pass # what do you want it to return, you can use "return" here, pass statement is often used when developing, allowing you to define the structure first and implement details later
def transf_log( variable ): # one vector input variable_logged = np.log( variable ) # perform the log transformation pass # what do you want it to return, you can use "return" here, pass statement is often used when developing, allowing you to define the structure first and implement details later
transf_log(x )
def transf_log( variable ): # define inputs here # handle non-positive values # 1. variable = variable + 1 # 2. variable = np.where( variable = 0, np.nan, variable ) # 3. variable = np.where( variable = 0, 0.01, variable ) variable_logged = np.log( variable ) # perform the log transformation pass # one vector output
transf_log(x )
def transf_log( variable ): # define inputs here # handle non-positive values # 1. variable = variable + 1 # 2. variable = np.where( variable = 0, np.nan, variable ) # 3. variable = np.where( variable = 0, 0.01, variable ) variable_logged = np.log( variable ) # perform the log transformation return variable_logged # one vector output
df['x_transformed'] = transf_log( df['x'] ) # lets see what that looks like df['x_transformed'].hist(bins=30);
def transf_log( variable, method_zero_correction = "add_one" ): # define inputs here
"""
This is a space for docstring, you can describe what the function does, its inputs and outputs or any otehr details
Description: This function performs log transformation on a given variable, handling zero values based on the specified method.
Inputs:
variable: one vector input
method_zero_correction: method to handle zero values, options are "add_one", "replace_nan", "replace_small_value"
"add_one" : adds 1 to all values before log transformation
"replace_nan" : replaces zero values with NaN before log transformation
"replace_small_value" : replaces zero values with a small value (0.01) before log transformation
Outputs:
variable_logged: log-transformed vector output
"""
# we can add a condition using if-elif-else statements, be careful with indentation here
if method_zero_correction == "add_one":
variable = variable + 1
elif method_zero_correction == "replace_nan":
variable = np.where( variable == 0, np.nan, variable )
elif method_zero_correction == "replace_small_value":
variable = np.where( variable == 0, 0.01, variable )
variable_logged = np.log( variable ) # perform the log transformation
return variable_logged # one vector outputdf['x_transformed_replaced_with_nan'] = transf_log( variable = df['x'], method_zero_correction="replace_nan" ) # let's see what that looks like df['x_transformed_replaced_with_nan'].hist(bins=30);
df.info()