jeudi 28 mars 2019

Why my if statement doesn't apply on all cases?

I would like to iterate through a file of rows which contains a column called consequents. Consequents contain a list of itemsets. I want to check if every item n in each consequent belongs to y. If it belongs, then create a dictionary of consequents. If not, exclude the row .

 y = ['CHOL = 220', 'CHOL = 300',  'CHOL = 200', 'CHOL = 240', 'CHOL = 350',
         'CHOL = 230', 'CHOL = 235', 'LM = 20', 'LM = 25', 'LM = 30', 'LM = 15', 'LM = 35' ]

This is the code I used

for i, row in ass_rules.iterrows():
    flag3 = False
    single_antecedent = False

    for l, n in enumerate(consequents):
        flag3 = False
    if len(consequents) >= 1 and n in y:  
        flag3 = True

    if flag3:
         consequents_list.append(consequents)
         rules['consequents'] = consequents_list

The problem is, after using the condition, it includes some consequents that has items n which doesn't belong to y, such as: frozenset({'DIAB = n', 'LM = 20'}) frozenset({'CHOL = 220', 'SEX = F'}). What is wrong with my code?

  frozenset({'CHOL = 230'}),
  frozenset({'LM = 20'}),
  frozenset({'LM = 20'}),
  frozenset({'LM = 20'}),
  frozenset({'LM = 20', 'SEX = F'}),
  frozenset({'LM = 20'}),
  frozenset({'CHOL = 230'}),
  frozenset({'CHOL = 230'}),
  frozenset({'AL = 0.0', 'CHOL = 230'}),
  frozenset({'CHOL = 220'}),
  frozenset({'CHOL = 220', 'DIAB = n'}),
  frozenset({'CHOL = 220', 'SEX = F'}),
  frozenset({'LM = 20'}),
  frozenset({'DIAB = n', 'LM = 20'})
  frozenset({'CHOL = 220', 'SEX = F'})```

Aucun commentaire:

Enregistrer un commentaire