-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpsi.py
More file actions
165 lines (133 loc) · 6.36 KB
/
Copy pathpsi.py
File metadata and controls
165 lines (133 loc) · 6.36 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import pandas as pd
import numpy as np
import random
def calculate_psi(expected, actual, buckettype='bins', buckets=10, axis=0):
'''Calculate the PSI (population stability index) across all variables
Args:
expected: numpy matrix of original values
actual: numpy matrix of new values, same size as expected
buckettype: type of strategy for creating buckets, bins splits into even splits, quantiles splits into quantile buckets
buckets: number of quantiles to use in bucketing variables
axis: axis by which variables are defined, 0 for vertical, 1 for horizontal
Returns:
psi_values: ndarray of psi values for each variable
Author:
Matthew Burke
github.com/mwburke
worksofchart.com
'''
def psi(expected_array, actual_array, buckets):
'''Calculate the PSI for a single variable
Args:
expected_array: numpy array of original values
actual_array: numpy array of new values, same size as expected
buckets: number of percentile ranges to bucket the values into
Returns:
psi_value: calculated PSI value
'''
def scale_range (input, min, max):
input += -(np.min(input))
input /= np.max(input) / (max - min)
input += min
return input
breakpoints = np.arange(0, buckets + 1) / (buckets) * 100
if buckettype == 'bins':
breakpoints = scale_range(breakpoints, np.min(expected_array), np.max(expected_array))
elif buckettype == 'quantiles':
breakpoints = np.stack([np.percentile(expected_array, b) for b in breakpoints])
expected_percents = np.histogram(expected_array, breakpoints)[0] / len(expected_array)
actual_percents = np.histogram(actual_array, breakpoints)[0] / len(actual_array)
def sub_psi(e_perc, a_perc):
'''Calculate the actual PSI value from comparing the values.
Update the actual value to a very small number if equal to zero
'''
if a_perc == 0:
a_perc = 0.0001
if e_perc == 0:
e_perc = 0.0001
value = (e_perc - a_perc) * np.log(e_perc / a_perc)
return(value)
psi_value = sum(sub_psi(expected_percents[i], actual_percents[i]) for i in range(0, len(expected_percents)))
return(psi_value)
if len(expected.shape) == 1:
psi_values = np.empty(len(expected.shape))
else:
psi_values = np.empty(expected.shape[axis])
for i in range(0, len(psi_values)):
if len(psi_values) == 1:
psi_values = psi(expected, actual, buckets)
elif axis == 0:
psi_values[i] = psi(expected[:,i], actual[:,i], buckets)
elif axis == 1:
psi_values[i] = psi(expected[i,:], actual[i,:], buckets)
return(psi_values)
def dataset_validation(initial_df, new_df):
initial = pd.DataFrame(initial_df)
new = pd.DataFrame(new_df)
if initial.shape[1] == new.shape[1]:
df = pd.DataFrame()
for col in initial.columns:
try:
col_initial = pd.to_numeric(initial[col], errors='raise')
col_new = pd.to_numeric(new[col], errors='raise')
if min(col_initial) == max(col_initial):
col_initial = random.choices([-0.000001, 0.000001], k=len(col_initial)) + col_initial
if min(col_new) == max(col_new):
col_new = random.choices([-0.000001, 0.000001], k=len(col_new)) + col_new
psi = calculate_psi(col_initial, col_new, buckettype='bins', buckets=10, axis=0)
initial_desc_stat = '{} ({})'.format(round(np.mean(col_initial), 2),round(np.std(col_initial), 2))
new_desc_stat = '{} ({})'.format(round(np.mean(col_new), 2),round(np.std(col_new), 2))
if psi < 0.1:
conclusion = 'Very slight change'
elif psi < 0.2:
conclusion = 'Some minor change'
else:
conclusion = 'Significant change'
df = df.append(pd.DataFrame(data={
'column name': col
,'baseline mean (std dev)': initial_desc_stat
,'new mean (std dev)': new_desc_stat
,'stability index': round(psi, 2)
,'conclusion': conclusion
}, index = [0]))
except ValueError: continue
df = df.reset_index(drop = True)
else:
df = pd.DataFrame()
print('The 2 csv do not have the same number of columns')
return df
def dataset_validation_csv(initial_path, new_path):
initial = pd.read_csv(initial_path)
new = pd.read_csv(new_path)
if initial.shape[1] == new.shape[1]:
df = pd.DataFrame()
for col in initial.columns:
try:
col_initial = pd.to_numeric(initial[col], errors='raise')
col_new = pd.to_numeric(new[col], errors='raise')
if min(col_initial) == max(col_initial):
col_initial = random.choices([-0.000001, 0.000001], k=len(col_initial)) + col_initial
if min(col_new) == max(col_new):
col_new = random.choices([-0.000001, 0.000001], k=len(col_new)) + col_new
psi = calculate_psi(col_initial, col_new, buckettype='bins', buckets=10, axis=0)
initial_desc_stat = '{} ({})'.format(round(np.mean(col_initial), 2),round(np.std(col_initial), 2))
new_desc_stat = '{} ({})'.format(round(np.mean(col_new), 2),round(np.std(col_new), 2))
if psi < 0.1:
conclusion = 'Very slight change'
elif psi < 0.2:
conclusion = 'Some minor change'
else:
conclusion = 'Significant change'
df = df.append(pd.DataFrame(data={
'column name': col
,'baseline mean (std dev)': initial_desc_stat
,'new mean (std dev)': new_desc_stat
,'stability index': round(psi, 2)
,'conclusion': conclusion
}, index = [0]))
except ValueError: continue
df = df.reset_index(drop = True)
else:
df = pd.DataFrame()
print('The 2 csv do not have the same number of columns')
return df