Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Mon Mar 11 09:30:09 2024
- @author: lab
- """
- import numpy as np
- import pandas as pd
- import seaborn as sns
- # Example dataset
- diamonds = sns.load_dataset("diamonds")
- diamond_prices = diamonds["price"]
- mean_price = diamond_prices.mean()
- median_price = diamond_prices.median()
- std = diamond_prices.std()
- skewness = (3 * (mean_price - median_price)) / std
- print(f"The Pierson's second skewness score of diamond prices distribution is {skewness:.5f}")
- #The Pierson's second skewness score of diamond prices distribution is 1.15189
- def moment_based_skew(distribution):
- n = len(distribution)
- mean = np.mean(distribution)
- std = np.std(distribution)
- # Divide the formula into two parts
- first_part = n / ((n - 1) * (n - 2))
- second_part = np.sum(((distribution - mean) / std) ** 3)
- skewness = first_part * second_part
- return skewness
- skew = moment_based_skew(diamond_prices)
- print("The moment_based skewness score of diamond prices distribution is ", skew)
- # Using Libraries
- # Pandas version
- print("The moment_based skewness skewness score of diamond prices distribution is ",
- diamond_prices.skew())
- # SciPy version
- from scipy.stats import skew
- print("The moment_based skewness skewness score of diamond prices distribution is ",
- skew(diamond_prices))
- # Visualization
- import matplotlib.pyplot as plt
- sns.kdeplot(diamond_prices)
- plt.title("Plot of diamond prices")
- plt.xlabel("Price ($)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement