Logo Studenta

main

Esta es una vista previa del archivo. Inicie sesión para ver el archivo original

{"cells":[{"cell_type":"code","execution_count":18,"metadata":{},"outputs":[],"source":["from functools import reduce\n","from math import sqrt\n","from statistics import stdev, mean"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["%%writefile credito.csv\n","id_vendedor,valor_emprestimos,quantidade_emprestimos,data\n","104271,448.0,1,20161208\n","21476,826.7,3,20161208\n","87440,313.6,3,20161208\n","15980,-8008.0,6,20161208\n","215906,2212.0,5,20161208\n","33696,2771.3,2,20161208\n","33893,2240.0,3,20161208\n","214946,-4151.0,18,20161208\n","123974,2021.95,2,20161208\n","225870,4039.0,2,20161208"]},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":["emprestimos = []\n","\n","with open(file='./credito.csv', mode='r', encoding='utf8') as fp:\n"," fp.readline() # cabeçalho\n"," linha = fp.readline()\n"," while linha:\n"," linha_emprestimo = {}\n"," linha_elementos = linha.strip().split(sep=',')\n"," linha_emprestimo['id_vendedor'] = linha_elementos[0]\n"," linha_emprestimo['valor_emprestimos'] = linha_elementos[1]\n"," linha_emprestimo['quantidade_emprestimos'] = linha_elementos[2]\n"," linha_emprestimo['data'] = linha_elementos[3]\n"," emprestimos.append(linha_emprestimo)\n"," linha = fp.readline()"]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["{'id_vendedor': '104271', 'valor_emprestimos': '448.0', 'quantidade_emprestimos': '1', 'data': '20161208'}\n","{'id_vendedor': '21476', 'valor_emprestimos': '826.7', 'quantidade_emprestimos': '3', 'data': '20161208'}\n","{'id_vendedor': '87440', 'valor_emprestimos': '313.6', 'quantidade_emprestimos': '3', 'data': '20161208'}\n","{'id_vendedor': '15980', 'valor_emprestimos': '-8008.0', 'quantidade_emprestimos': '6', 'data': '20161208'}\n","{'id_vendedor': '215906', 'valor_emprestimos': '2212.0', 'quantidade_emprestimos': '5', 'data': '20161208'}\n","{'id_vendedor': '33696', 'valor_emprestimos': '2771.3', 'quantidade_emprestimos': '2', 'data': '20161208'}\n","{'id_vendedor': '33893', 'valor_emprestimos': '2240.0', 'quantidade_emprestimos': '3', 'data': '20161208'}\n","{'id_vendedor': '214946', 'valor_emprestimos': '-4151.0', 'quantidade_emprestimos': '18', 'data': '20161208'}\n","{'id_vendedor': '123974', 'valor_emprestimos': '2021.95', 'quantidade_emprestimos': '2', 'data': '20161208'}\n","{'id_vendedor': '225870', 'valor_emprestimos': '4039.0', 'quantidade_emprestimos': '2', 'data': '20161208'}\n"]}],"source":["for emprestimo in emprestimos:\n"," print(emprestimo)"]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[448.0, 826.7, 313.6, -8008.0, 2212.0, 2771.3, 2240.0, -4151.0, 2021.95, 4039.0]\n"]}],"source":["valor_emprestimos_lista = []\n","coluna_valor_emprestimos = list(map(lambda col: valor_emprestimos_lista.append(float(col['valor_emprestimos'])), emprestimos))\n","\n","print(valor_emprestimos_lista)"]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["[448.0, 826.7, 313.6, 2212.0, 2771.3, 2240.0, 2021.95, 4039.0]\n"]}],"source":["valor_emprestimos_lista_filtrada = []\n","coluna_valor_emprestimos_filtrada = list(filter(lambda num: valor_emprestimos_lista_filtrada.append(num) if num > 0 else False, valor_emprestimos_lista))\n","\n","print(valor_emprestimos_lista_filtrada)"]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["14872.550000000001\n"]}],"source":["soma_valor_emprestimos = reduce(lambda x, y: x + y, valor_emprestimos_lista_filtrada)\n","\n","print(soma_valor_emprestimos)"]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1859.0687500000001\n"]}],"source":["media_valor_emprestimos = reduce(lambda a, b: a + b, valor_emprestimos_lista_filtrada) / len(valor_emprestimos_lista_filtrada)\n","\n","print(media_valor_emprestimos)"]},{"cell_type":"code","execution_count":19,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1859.0687500000001\n"]}],"source":["print(mean(valor_emprestimos_lista_filtrada)) # professor acho mais simples fazer assim."]},{"cell_type":"code","execution_count":20,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1271.997271149785\n"]}],"source":["desvio_padrao_valor_emprestimos = stdev(valor_emprestimos_lista_filtrada) # professor acho mais simples fazer assim.\n","print(desvio_padrao_valor_emprestimos)\n"]},{"cell_type":"code","execution_count":21,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["1189.8444963884722\n"]}],"source":["desvio = sqrt(float(reduce(lambda x, y: x + y, map(lambda x: (x - media_valor_emprestimos) ** 2, valor_emprestimos_lista_filtrada))) / len(valor_emprestimos_lista_filtrada))\n","print(desvio)"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.8"}},"nbformat":4,"nbformat_minor":2}

Continuar navegando