Advertisement
SapioiT

uni proj

May 26th, 2022
662
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. import numpy as np # for creating an array
  3. import pandas as pd # for dataframe
  4. import scipy # for statistical cal
  5. from scipy.stats import chi2
  6.  
  7. np.random.seed(10)
  8.  
  9. type_bottle = np.random.choice(a= ["paper","cans","glass","others","plastic"],
  10.                               p = [0.05, 0.15 ,0.25, 0.05, 0.5],
  11.                               size=1000)
  12.  
  13. month = np.random.choice(a= ["January","February","March"],
  14.                               p = [0.4, 0.2, 0.4],
  15.                               size=1000)
  16.  
  17. bottles = pd.DataFrame({"types":type_bottle,
  18.                        "months":month})
  19.  
  20. bottles_tab = pd.crosstab(bottles.types, bottles.months, margins = True)
  21.  
  22. bottles_tab.columns = ["January","February","March","row_totals"]
  23.  
  24. bottles_tab.index = ["paper","cans","glass","others","plastic","col_totals"]
  25.  
  26. observed = bottles_tab.iloc[0:5,0:3]  
  27. bottles_tab
  28.  
  29. expected =  np.outer(bottles_tab["row_totals"][0:5],
  30.                      bottles_tab.loc["col_totals"][0:3]) / 1000
  31.  
  32. expected = pd.DataFrame(expected)
  33.  
  34. expected.columns = ["Janurary","Feburary","March"]
  35. expected.index = ["paper","cans","glass","others","plastic"]
  36.  
  37. expected
  38.  
  39. chi_squared_stat = (((observed-expected)**2)/expected).sum().sum()
  40.  
  41. print(chi_squared_stat)
  42.  
  43. critical_value = chi2.ppf(q = 0.95,
  44.                       df = 8)   # *
  45. print("Critical value:",critical_value)
  46.  
  47. p_value = 1 - chi2.cdf(x=chi_squared_stat,
  48.                              df=8)
  49. print("P value:",p_value)
  50.  
  51. scipy.stats.chi2_contingency(observed= observed)
  52.  
Advertisement
RAW Paste Data Copied
Advertisement