Skip to content

Instantly share code, notes, and snippets.

@codecademydev

codecademydev/script.py Secret

Created Jan 30, 2021
Embed
What would you like to do?
Codecademy export
import numpy as np
import pandas as pd
import codecademylib3
from scipy.stats import chi2_contingency
npi = pd.read_csv("npi_sample.csv")
#print(npi.head())
def contingency_analisis(dataframe, column1, column2):
frequency = pd.crosstab(npi[column1], npi[column2])
#print(frequency, '\n')
proportion = frequency / len(dataframe)
#print(proportion, '\n')
chi2, pval, dof, expected = chi2_contingency(frequency)
#print('The chi2 is:', str(chi2))
#print('The pval is:', str(pval))
#print('The expected array is:')
#print(np.round(expected))
return dataframe, chi2
chi2_dict = {}
columns = npi.columns
for column in columns:
for i in range(len(columns)):
if column == columns[i]:
continue
else:
key = column, columns[i]
dataframe, chi2 = contingency_analisis(npi, column, columns[i])
chi2_dict[key] = chi2
#contingency_analisis(npi, 'leader', 'authority')
index = list(range(len(chi2_dict)))
df = pd.DataFrame(chi2_dict, index = [0])
print(df.max())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment