tuomasvaltanen

Untitled

Mar 30th, 2021 (edited)
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. # lecture 7, quantitative analysis with Python
  2.  
  3. from scipy.stats import f_oneway
  4.  
  5. #enter exam scores for each group
  6. group1 = [85, 86, 88, 75, 78, 94, 98, 79, 71, 80]
  7. group2 = [91, 92, 93, 85, 87, 84, 82, 88, 95, 96]
  8. group3 = [49, 28, 48, 34, 52, 65, 33, 25, 42, 51]
  9.  
  10. #perform one-way ANOVA
  11. result = f_oneway(group1, group2, group3)
  12.  
  13. # 0.05 is a common "error margin" or alpha value that is
  14. # considered "statistically significant" in MOST CASES
  15. # if pValue is less than 0.05 => GROUPS ARE SIMILAR
  16. # if pValue is more than 0.05 => GROUPS ARE DIFFERENT
  17. print(result)
  18.  
  19. # NEW FILE -> ANCOVA testing
  20.  
  21. import numpy as np
  22. import pandas as pd
  23.  
  24. from pingouin import ancova
  25.  
  26.  
  27. #create data
  28. df = pd.DataFrame({'technique': np.repeat(['Traditional', 'Video lessons', 'Self-study'], 5),
  29.                    'current_grade': [97, 78, 75, 77, 85,
  30.                                      92, 69, 77, 74, 88,
  31.                                      96, 91, 88, 82, 80],
  32.                    'exam_score': [77, 89, 82, 84, 89,
  33.                                   78, 88, 93, 94, 90,
  34.                                   85, 81, 83, 88, 89]})
  35.  
  36.  
  37.  
  38. #perform ANCOVA
  39. result = ancova(data=df, dv='exam_score', covar='current_grade', between='technique')
  40.  
  41. # p-unc, if value is less than 0.05 => different study techniques affect the grade scores
  42. # otherwise, the study technique does not matter!
  43. print(result)
  44.  
  45. # NEW FILE -> CHI SQUARE
  46. # TWO DIFFERENT TESTS -> INDEPENDENCE, OR GOODNESS OF FIT
  47.  
  48. import pandas as pd
  49. import numpy as np
  50. from scipy.stats import chi2_contingency
  51.  
  52. df = pd.DataFrame({'Gender' : ['M', 'M', 'M', 'F', 'F'] * 10,
  53.                    'isSmoker' : ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker', 'Smoker'] * 10
  54.                   })
  55.  
  56.  
  57. contigency = pd.crosstab(df['Gender'], df['isSmoker'])
  58.  
  59. contigency_pct = pd.crosstab(df['Gender'], df['isSmoker'], normalize='index')
  60.  
  61.  
  62. # Chi-square test of independence.
  63. c, p, dof, expected = chi2_contingency(contigency)
  64.  
  65. # shop owner claims this
  66. expected = [50, 50, 50, 50, 50]
  67.  
  68. # the researcher counted these
  69. observed = [50, 60, 40, 47, 53]
  70.  
  71. import scipy.stats as stats
  72.  
  73. #perform Chi-Square Goodness of Fit Test
  74. result = stats.chisquare(f_obs=observed, f_exp=expected)
  75. print(result)
  76.  
  77. # NEW FILE
  78. # k-means demo
  79.  
  80. import matplotlib.pyplot as plt
  81. import numpy as np
  82. from sklearn.cluster import KMeans
  83.  
  84. X = np.array([[5,3],
  85.      [10,15],
  86.      [15,12],
  87.      [24,10],
  88.      [30,45],
  89.      [35,43],
  90.      [45,42],
  91.      [85,70],
  92.      [71,80],
  93.      [60,78],
  94.      [55,52],
  95.      [80,91],])
  96.  
  97. plt.clf()
  98. plt.scatter(X[:,0],X[:,1], label='True Position')
  99. plt.figure()
  100.  
  101. kmeans = KMeans(n_clusters=3)
  102. kmeans.fit(X)
  103.  
  104. plt.clf()
  105. plt.scatter(X[:,0],X[:,1], c=kmeans.labels_, cmap='rainbow')
  106. plt.figure()
  107.  
  108. # test if the clusters (groups) work with test values
  109. print(kmeans.predict([[10,10], [42, 45], [82,87]]))
  110.  
  111. # NEW FILE
  112. # Student's t-test, individual
  113.  
  114. import pandas as pd
  115. import numpy as np
  116. import random
  117. from numpy.random import seed
  118. from numpy.random import randn
  119. from scipy.stats import ttest_ind
  120. from scipy.stats import t
  121.  
  122. seed(1)
  123.  
  124. df=pd.DataFrame({"female":np.random.randint(10, 100, size=10),"male":np.random.randint(10, 140, size=10)})
  125.  
  126. t_stat, p = ttest_ind(df['male'], df['female'])
  127.  
  128. # scipy makes a paired t-test by default
  129. # to get the individual t-test p-value:
  130. p = p/2
  131.  
  132. print(f't={t_stat}, p={p}')
Add Comment
Please, Sign In to add comment