# How to use get_group_counts method in autotest

Best Python code snippet using autotest_python

featsel.py

Source:featsel.py

`...9 N01 = np.sum(td[t,in_c] == 0)10 N10 = np.sum(td[t,not_in_c] > 0)11 N00 = np.sum(td[t,not_in_c] == 0)12 return (N11,N01,N10,N00)13def get_group_counts(N11,N01,N10,N00):14 N1_ = N10 + N1115 N_1 = N11 + N0116 N0_ = N01 + N0017 N_0 = N10 + N0018 N = N10 + N11 + N01 + N0019 return (N1_,N_1,N0_,N_0,N)20def mutual_information(td, delta):21 """22 td: a term-document V x |X| matrix23 delta: |X| x M matrix where delta(i,j) = 1 if document i belongs to class j24 output: a V x M matrix of scores I(t,c)25 """26 V,X = td.shape27 X_,M = delta.shape28 assert(X==X_)29 I = np.zeros((V,M), dtype=np.double)30 for t in range(V):31 for c in range(M):32 N11,N01,N10,N00 = get_counts(td, delta, t, c)33 N1_,N_1,N0_,N_0,N = get_group_counts(N11,N01,N10,N00)34 # FIXME: how to deal with log2(0) when the numerator is 0?35 I[t,c] = N11/N * log2((N*N11)/(N1_*N_1)) + \36 N01/N * log2((N*N01)/(N0_*N_1)) + \37 N10/N * log2((N*N10)/(N1_*N_0)) + \38 N00/N * log2((N*N00)/(N0_*N_0))39 return I40def chi2(td, delta):41 """42 td: a term-document V x |X| matrix43 delta: |X| x M matrix where delta(i,j) = 1 if document i belongs to class j44 output: a V x M matrix of scores chi2(t,c)45 """46 V,X = td.shape47 X_,M = delta.shape48 assert(X==X_)49 chi2m = np.zeros((V,M), dtype=np.double)50 for t in range(V):51 for c in range(M):52 N11,N01,N10,N00 = get_counts(td, delta, t, c)53 N1_,N_1,N0_,N_0,N = get_group_counts(N11,N01,N10,N00)54 chi2m[t,c] = (N*(N11*N00-N10*N01)**2)/(N1_*N_1*N0_*N_0)55 return chi2m56def select_max(td, vocab, A, K):57 """58 Select the best K/M features for each of the M classes59 td: a term-document V x |X| matrix60 delta: |X| x M matrix where delta(i,j) = 1 if document i belongs to class j61 A: matrix returned by chi2 or mutual_information62 output: the new reduced term-document matrix and the new vocabulary dict63 """64 V, M = A.shape65 d = {}66 for m in range(M):67 k = 1...`

GroupBasedMetric.py

Source:GroupBasedMetric.py

`...8 def calculate(self, sensitive_attributes, df, debug=True):9 sum_ratio = 010 sum_difference = 011 for sensitive_attribute in sensitive_attributes:12 numerators = self.group_numerator.get_group_counts(sensitive_attribute, df)13 denominators = self.group_denominator.get_group_counts(sensitive_attribute, df)14 if 0 in numerators or 0 in denominators:15 sum_ratio += 016 sum_difference += 117 else:18 privileged = numerators[0] / (numerators[0] + denominators[0])19 unprivileged = numerators[1] / (numerators[1] + denominators[1])20 #print("{} - {}-{}".format(self.name, round(privileged, 2), round(unprivileged, 2)))21 sum_ratio += min(unprivileged / privileged, privileged / unprivileged)22 sum_difference += abs(privileged - unprivileged)23 ratio = round(sum_ratio / len(sensitive_attributes), 2)24 difference = round(sum_difference / len(sensitive_attributes), 2)25 self.ratios.append(ratio)26 self.differences.append(difference)27 if debug:28 print("{}_ratio - {}".format(self.name, ratio))29 print("{}_diff - {}".format(self.name, difference))30 return ratio, difference31class TP:32 @staticmethod33 def get_group_counts(sensitive_attribute, df):34 return [35 len(df[(df[sensitive_attribute] == 1) & (df["y"] == 1) & (df["y_pred"] == 1)]),36 len(df[(df[sensitive_attribute] == 0) & (df["y"] == 1) & (df["y_pred"] == 1)])37 ]38class FP:39 @staticmethod40 def get_group_counts(sensitive_attribute, df):41 return [42 len(df[(df[sensitive_attribute] == 1) & (df["y"] == 0) & (df["y_pred"] == 1)]),43 len(df[(df[sensitive_attribute] == 0) & (df["y"] == 0) & (df["y_pred"] == 1)])44 ]45class TN:46 @staticmethod47 def get_group_counts(sensitive_attribute, df):48 return [49 len(df[(df[sensitive_attribute] == 1) & (df["y"] == 0) & (df["y_pred"] == 0)]),50 len(df[(df[sensitive_attribute] == 0) & (df["y"] == 0) & (df["y_pred"] == 0)])51 ]52class FN:53 @staticmethod54 def get_group_counts(sensitive_attribute, df):55 return [56 len(df[(df[sensitive_attribute] == 1) & (df["y"] == 1) & (df["y_pred"] == 0)]),57 len(df[(df[sensitive_attribute] == 0) & (df["y"] == 1) & (df["y_pred"] == 0)])58 ]59class Sens:60 @staticmethod61 def get_group_counts(sensitive_attribute, df):62 return [63 len(df[df[sensitive_attribute] == 1]),64 len(df[df[sensitive_attribute] == 0])65 ]66class PosSens:67 @staticmethod68 def get_group_counts(sensitive_attribute, df):69 return [70 len(df[(df[sensitive_attribute] == 1) & (df["y_pred"] == 1)]),71 len(df[(df[sensitive_attribute] == 0) & (df["y_pred"] == 1)])...`

solution.py

Source:solution.py

`...9 )10 for group in groups11 ]12 return sum(intersections)13def get_group_counts():14 path = os.path.join(os.path.dirname(__file__), 'inputs.txt')15 with open(path, 'r') as file_handle:16 groups = file_handle.read().split('\n\n')17 return [18 get_unique_answers(groups),19 get_duplicate_answers(groups)20 ]21if __name__ == "__main__":22 unique, duplicate = get_group_counts()23 print(f'Solution to part a: {unique}')...`

## Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.