-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chi-Square Analysis Example.py
46 lines (34 loc) · 1.5 KB
/
Chi-Square Analysis Example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import scipy.stats as stats
from scipy.stats import chi2_contingency
## Feature Selection
testColumns = traindata.columns[np.where(traindata.dtypes==object)]
## Introduce ChiSquare Class
class ChiSquare:
def __init__(self, dataframe):
self.df = dataframe
self.p = None #P-Value
self.chi2 = None #Chi Test Statistic
self.dof = None
self.dfObserved = None
self.dfExpected = None
def _print_chisquare_result(self, colX, alpha):
result = ""
if self.p<alpha:
result="{0} - IMPORTANT predictor. Chi-Statistic:{1}, P-value:{2}".format(colX,self.chi2,self.p)
else:
result="{0} - NOT important predictor. Chi-Statistic:{1}, P-value:{2}".format(colX,self.chi2,self.p)
print(result)
def TestIndependence(self, colX, colY, alpha=0.05):
X = self.df[colX].astype(str)
Y = self.df[colY].astype(str)
self.dfObserved = pd.crosstab(Y,X)
chi2, p, dof, expected = stats.chi2_contingency(self.dfObserved.values)
self.p = p
self.chi2 = chi2
self.dof = dof
self.dfExpected = pd.DataFrame(expected, columns=self.dfObserved.columns, index = self.dfObserved.index)
self._print_chisquare_result(colX,alpha)
## Initialize ChiSquare Class
cT = ChiSquare(traindata)
for var in testColumns:
cT.TestIndependence(colX=var, colY='label')