Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # lecture 7, quantitative analysis with Python
- from scipy.stats import f_oneway
- #enter exam scores for each group
- group1 = [85, 86, 88, 75, 78, 94, 98, 79, 71, 80]
- group2 = [91, 92, 93, 85, 87, 84, 82, 88, 95, 96]
- group3 = [49, 28, 48, 34, 52, 65, 33, 25, 42, 51]
- #perform one-way ANOVA
- result = f_oneway(group1, group2, group3)
- # 0.05 is a common "error margin" or alpha value that is
- # considered "statistically significant" in MOST CASES
- # if pValue is less than 0.05 => GROUPS ARE SIMILAR
- # if pValue is more than 0.05 => GROUPS ARE DIFFERENT
- print(result)
- # NEW FILE -> ANCOVA testing
- import numpy as np
- import pandas as pd
- from pingouin import ancova
- #create data
- df = pd.DataFrame({'technique': np.repeat(['Traditional', 'Video lessons', 'Self-study'], 5),
- 'current_grade': [97, 78, 75, 77, 85,
- 92, 69, 77, 74, 88,
- 96, 91, 88, 82, 80],
- 'exam_score': [77, 89, 82, 84, 89,
- 78, 88, 93, 94, 90,
- 85, 81, 83, 88, 89]})
- #perform ANCOVA
- result = ancova(data=df, dv='exam_score', covar='current_grade', between='technique')
- # p-unc, if value is less than 0.05 => different study techniques affect the grade scores
- # otherwise, the study technique does not matter!
- print(result)
- # NEW FILE -> CHI SQUARE
- # TWO DIFFERENT TESTS -> INDEPENDENCE, OR GOODNESS OF FIT
- import pandas as pd
- import numpy as np
- from scipy.stats import chi2_contingency
- df = pd.DataFrame({'Gender' : ['M', 'M', 'M', 'F', 'F'] * 10,
- 'isSmoker' : ['Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker', 'Smoker'] * 10
- })
- contigency = pd.crosstab(df['Gender'], df['isSmoker'])
- contigency_pct = pd.crosstab(df['Gender'], df['isSmoker'], normalize='index')
- # Chi-square test of independence.
- c, p, dof, expected = chi2_contingency(contigency)
- # shop owner claims this
- expected = [50, 50, 50, 50, 50]
- # the researcher counted these
- observed = [50, 60, 40, 47, 53]
- import scipy.stats as stats
- #perform Chi-Square Goodness of Fit Test
- result = stats.chisquare(f_obs=observed, f_exp=expected)
- print(result)
- # NEW FILE
- # k-means demo
- import matplotlib.pyplot as plt
- import numpy as np
- from sklearn.cluster import KMeans
- X = np.array([[5,3],
- [10,15],
- [15,12],
- [24,10],
- [30,45],
- [35,43],
- [45,42],
- [85,70],
- [71,80],
- [60,78],
- [55,52],
- [80,91],])
- plt.clf()
- plt.scatter(X[:,0],X[:,1], label='True Position')
- plt.figure()
- kmeans = KMeans(n_clusters=3)
- kmeans.fit(X)
- plt.clf()
- plt.scatter(X[:,0],X[:,1], c=kmeans.labels_, cmap='rainbow')
- plt.figure()
- # test if the clusters (groups) work with test values
- print(kmeans.predict([[10,10], [42, 45], [82,87]]))
- # NEW FILE
- # Student's t-test, individual
- import pandas as pd
- import numpy as np
- import random
- from numpy.random import seed
- from numpy.random import randn
- from scipy.stats import ttest_ind
- from scipy.stats import t
- seed(1)
- df=pd.DataFrame({"female":np.random.randint(10, 100, size=10),"male":np.random.randint(10, 140, size=10)})
- t_stat, p = ttest_ind(df['male'], df['female'])
- # scipy makes a paired t-test by default
- # to get the individual t-test p-value:
- p = p/2
- print(f't={t_stat}, p={p}')
Add Comment
Please, Sign In to add comment