text_html_output_1.png
from scipy.stats import chi2

import numpy as np

import numpy as np

import pandas as pd

import pandas as pd

nodes = pd.read_csv('../input/tolokers/nodes.tsv', sep='\t', index_col='id')
nodes

edges = pd.read_csv('../input/tolokers/edges.tsv', sep='\t')
edges

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('../input/tolokers/nodes.tsv', sep='\t', index_col='id')
corr_matrix = df.corr()
corr_matrix

from scipy.stats import chi2
def chi2_p_return(col1, col2):
    col1_vals = col1.unique()
    col2_vals = col2.unique()
    contingency_matrix = {}
    for i in col1_vals:
        row = {}
        for j in col2_vals:
            count = len(df[(col1 == i) & (col2 == j)])
            row[j] = count
        contingency_matrix[i] = row
    contingency_matrix_df = pd.DataFrame(contingency_matrix)
    contingency_matrix_df.fillna(0, inplace=True)
    actual = contingency_matrix_df.values
    row_sums = actual.sum(axis=1)
    col_sums = actual.sum(axis=0)
    total = actual.sum()
    expected = np.outer(row_sums, col_sums) / total
    chi_squared = np.sum((actual - expected) ** 2 / expected)
    degree_freedom = (len(contingency_matrix_df) - 1) * (len(contingency_matrix_df.columns) - 1)
    p_value_sci = '{:.3e}'.format(1 - chi2.cdf(chi_squared, degree_freedom))
    return (chi_squared, p_value_sci)

chi_squared, p_value = chi2_p_return(df['education'], df['banned'])
alpha = 0.01

chi_squared, p_value = chi2_p_return(df['expired_rate'], df['banned'])
print('Chi_squared:', chi_squared, ', p_value:', p_value, '(very close to zero)')
alpha = 0.01
if float(p_value) <= alpha:
    print('Dependent (reject H0)')
else:
    print('Independent (H0 holds true)')