Log Transformation and visualizing it using Python

Tarique Akhtar
2 min readJan 13, 2023

--

Log transformation is a common technique used to reduce skewness in a distribution and make it more symmetric. Skewness is a measure of the asymmetry of a probability distribution, and a right-skewed distribution has a long tail on the right side of the distribution. Log transformation can help to “stretch out” the tail of a right-skewed distribution, making it more symmetric and easier to analyze.

In Python, the log transformation can be applied using the numpy library. The numpy library provides a natural logarithm function, np.log(), which can be used to apply the log transformation to an array of data.

Here is an example of how to apply a log transformation to right-skewed data using Python:

import numpy as np

# Generate 1000 samples from a right-skewed distribution
data = np.random.exponential(scale=2, size=1000)

# Plot the data to visualize the skew
import matplotlib.pyplot as plt
plt.hist(data, bins=50)
plt.show()
Right-skewed data distribution

The above code generates 1000 samples from an exponential distribution with a scale parameter of 2, which produces a right-skewed distribution.

# Apply log transformation to the data
log_data = np.log(data)

# Plot the original data and the log-transformed data
import matplotlib.pyplot as plt
#plt.hist(data, bins=50, label='Original Data')
plt.hist(log_data, bins=50, label='Log-Transformed Data')
plt.legend()
plt.show()

Then log_data = np.log(data) applies the natural logarithm to each element of the data array.

Log-transformed on the Original data

Conclusion

It’s important to note that the log transformation should only be applied to data that is positive, as the natural logarithm is not defined for values less than or equal to zero. Additionally, it’s important to keep in mind that the log transformation does not make the data normal, it just remove the skewness. There are other techniques to make data normal such as Box-Cox transformation.

Thanks for reading

--

--

Tarique Akhtar

Data Science Professional, Love to learn new things!!!We can get connected through LinkedIn (https://www.linkedin.com/in/tarique-akhtar-6b902651/)