falta extra

This commit is contained in:
Tiago Sousa 2023-02-18 17:19:46 +00:00
parent d402e05d37
commit d7f38904f4

View file

@ -1,14 +1,15 @@
import matplotlib.pyplot as plt
# idade -> i, sexo -> s, tensao -> t, colesterol -> c, batimento -> b, doenca -> d # idade -> i, sexo -> s, tensao -> t, colesterol -> c, batimento -> b, doenca -> d
# estrutura de dados: [{"i": 1 , "s" : F | M}] # estrutura de dados: [{"i": 1 , "s" : F | M}]
data = [] data = []
def readFile(filename): def readFile(filename):
f = open(filename, "r") f = open(filename, 'r')
lines = f.readlines() lines = f.readlines()
lines.pop(0) lines.pop(0)
for line in lines: for line in lines:
values = line.split(",") values = line.split(',')
idade = int(values[0]) idade = int(values[0])
sexo = values[1] sexo = values[1]
tensao = int(values[2]) tensao = int(values[2])
@ -16,29 +17,97 @@ def readFile(filename):
batimento = int(values[4]) batimento = int(values[4])
temDoenca = bool(values[5]) temDoenca = bool(values[5])
data_entry = { data_entry = {
"i": idade, 'i': idade,
"s": sexo, 's': sexo,
"t": tensao, 't': tensao,
"c": colesterol, 'c': colesterol,
"b": batimento, 'b': batimento,
"d": temDoenca 'd': temDoenca
} }
data.append(data_entry) data.append(data_entry)
# distrib colesterol: fazer uma lista em que o index dos elementos e a divisao inteira dos valores def distrib_sexo():
# distribuicoes vao ser dicionarios dist = {}
size_of_data = len(data)
for entry in data:
if entry['d']:
if dist.keys().__contains__(entry['s']):
dist[entry['s']] += 1
else:
dist[entry['s']] = 1
for (k,v) in dist.items():
percentage = v/size_of_data
dist[k] = percentage
return dist
def distrib_etaria():
dist = {}
size_of_data = len(data)
for entry in data:
if entry['i'] >= 30 and entry['d']:
lim_inf = (entry['i'] // 5) * 5
lim_sup = lim_inf + 4
position = str(lim_inf) + '-' + str(lim_sup)
if dist.keys().__contains__(position):
dist[position] += 1
else:
dist[position] = 1
for (k,v) in dist.items():
percentage = v/size_of_data
dist[k] = percentage
return dist
def distrib_colesterol():
dist = {}
size_of_data = len(data)
for entry in data:
lim_inf = (entry['c'] // 10) * 10
lim_sup = lim_inf + 9
position = str(lim_inf) + '-' + str(lim_sup)
if dist.keys().__contains__(position):
dist[position] += 1
else:
dist[position] = 1
for (k,v) in dist.items():
percentage = v/size_of_data
dist[k] = percentage
return dist
def show_distrib(type,dist):
title_key = "Key"
title_val = "Valor"
match type:
case 0:
title_key = "Sexo"
case 1:
title_key = "Faixa Etaria"
case 2:
title_key = "Colesterol"
max_key_len = max(max(len(str(k)) for k in dist.keys()),len(title_key))
max_val_len = max(max(len(str(v)) for v in dist.values()),len(title_val))
print('+' + '-'*(max_key_len+2) + '+' + '-'*(max_val_len+2) + '+')
print('| {:{}} | {:{}} |'.format(title_key, max_key_len, title_val, max_val_len))
print('+' + '-'*(max_key_len+2) + '+' + '-'*(max_val_len+2) + '+')
for title_key, title_val in sorted(dist.items()):
print('| {:{}} | {:{}} |'.format(title_key, max_key_len, title_val, max_val_len))
print('+' + '-'*(max_key_len+2) + '+' + '-'*(max_val_len+2) + '+')
def main(): def main():
readFile("myheart.csv") readFile("myheart.csv")
min = 200 dist_sexo = distrib_sexo()
for entry in data: dist_etaria = distrib_etaria()
if entry['i'] < min: dist_colesterol = distrib_colesterol()
min = entry["i"] show_distrib(1,dist_etaria)
print(f"idade min -> {min}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()