Advertisement
brandblox

numpy lab(11/03/2024)

Mar 10th, 2024
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 11 09:30:09 2024
  4.  
  5. @author: lab
  6. """
  7.  
  8. import numpy as np
  9. import pandas as pd
  10. import seaborn as sns
  11. # Example dataset
  12. diamonds = sns.load_dataset("diamonds")
  13. diamond_prices = diamonds["price"]
  14. mean_price = diamond_prices.mean()
  15. median_price = diamond_prices.median()
  16. std = diamond_prices.std()
  17. skewness = (3 * (mean_price - median_price)) / std
  18. print(f"The Pierson's second skewness score of diamond prices distribution is {skewness:.5f}")
  19. #The Pierson's second skewness score of diamond prices distribution is 1.15189
  20. def moment_based_skew(distribution):
  21.  n = len(distribution)
  22.  mean = np.mean(distribution)
  23.  std = np.std(distribution)
  24.  
  25.  # Divide the formula into two parts
  26.  first_part = n / ((n - 1) * (n - 2))
  27.  second_part = np.sum(((distribution - mean) / std) ** 3)
  28.  
  29.  skewness = first_part * second_part
  30.  return skewness
  31. skew = moment_based_skew(diamond_prices)
  32. print("The moment_based skewness score of diamond prices distribution is ", skew)
  33. # Using Libraries
  34. # Pandas version
  35. print("The moment_based skewness skewness score of diamond prices distribution is ",
  36. diamond_prices.skew())
  37. # SciPy version
  38. from scipy.stats import skew
  39. print("The moment_based skewness skewness score of diamond prices distribution is ",
  40. skew(diamond_prices))
  41. # Visualization
  42. import matplotlib.pyplot as plt
  43. sns.kdeplot(diamond_prices)
  44. plt.title("Plot of diamond prices")
  45. plt.xlabel("Price ($)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement