python skewness of distribution
import numpy as np from scipy import stats import matplotlib.pyplot as plt import seaborn as sns
# Set the random seed for reproducibility np.random.seed(11)
# Generate Right-Skewed Data right_skewed_data = np.random.exponential(scale=2, size=1000)
# Generate Left-Skewed Data left_skewed_data = np.random.exponential(scale=2, size=1000) * -1 + 8
# Generate Normal Data normal_data = np.random.normal(loc=5, scale=1.5, size=1000)
# Calculate Mean, Median, Mode for Right-Skewed Data mean_right = np.mean(right_skewed_data) median_right = np.median(right_skewed_data) mode_right = stats.mode(right_skewed_data, keepdims=True)[0][0] #This argument specifies whether the dimensions of the output should be kept #the same as the input. If set to True, the output will have the same shape as the input array
# Calculate Mean, Median, Mode for Left-Skewed Data mean_left = np.mean(left_skewed_data) median_left = np.median(left_skewed_data) mode_left = stats.mode(left_skewed_data, keepdims=True)[0][0]
# Calculate Mean, Median, Mode for Normal Data mean_normal = np.mean(normal_data) median_normal = np.median(normal_data) mode_normal = stats.mode(normal_data, keepdims=True)[0][0]
# Plotting the distributions plt.figure(figsize=(21, 6)) # Plot Right-Skewed Distribution plt.subplot(1, 3, 1) sns.histplot(right_skewed_data, kde=True, color='skyblue') plt.axvline(mean_right, color='r', linestyle='--', label=f'Mean: {mean_right:.2f}') plt.axvline(median_right, color='g', linestyle='-', label=f'Median: {median_right:.2f}') plt.axvline(mode_right, color='b', linestyle='-', label=f'Mode: {mode_right:.2f}') plt.title('Right-Skewed Distribution') plt.legend() # Plot Left-Skewed Distribution plt.subplot(1, 3, 2) sns.histplot(left_skewed_data, kde=True, color='lightgreen') plt.axvline(mean_left, color='r', linestyle='--', label=f'Mean: {mean_left:.2f}') plt.axvline(median_left, color='g', linestyle='-', label=f'Median: {median_left:.2f}') plt.axvline(mode_left, color='b', linestyle='-', label=f'Mode: {mode_left:.2f}') plt.title('Left-Skewed Distribution') plt.legend() # Plot Normal Distribution plt.subplot(1, 3, 3) sns.histplot(normal_data, kde=True, color='lightcoral') plt.axvline(mean_normal, color='r', linestyle='--', label=f'Mean: {mean_normal:.2f}') plt.axvline(median_normal, color='g', linestyle='-', label=f'Median: {median_normal:.2f}') plt.axvline(mode_normal, color='b', linestyle='-', label=f'Mode: {mode_normal:.2f}') plt.title('Normal Distribution') plt.legend() plt.tight_layout() plt.show()

#box plots
plt.subplot(1, 3, 1) sns.boxplot(y=right_skewed_data, color='skyblue') plt.title('Right-Skewed Distribution') # Boxplot for Left-Skewed Data plt.subplot(1, 3, 2) sns.boxplot(y=left_skewed_data, color='lightgreen') plt.title('Left-Skewed Distribution') # Boxplot for Normal Data plt.subplot(1, 3, 3) sns.boxplot(y=normal_data, color='lightcoral') plt.title('Normal Distribution') plt.tight_layout() plt.show()

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.