<jupyter_start><jupyter_text>Quartis  Dados Não agrupados<jupyter_code>np.median(dados_impar)
posicao_mediana = math.floor(len(dados_impar) / 2)
posicao_mediana
esquerda = dados_impar[0:posicao_mediana]
esquerda
np.median(esquerda)
direita = dados_impar[posicao_mediana:]
direita
np.median(direita)<jupyter_output><empty_output><jupyter_text>Numpy, Scipy, Pandas<jupyter_code>import pandas as pd
from scipy import stats
np.quantile(dados_impar, 0.5)
np.quantile(dados_impar, 0.25), np.quantile(dados_impar, 0.75)
stats.scoreatpercentile(dados_impar, 50)
stats.scoreatpercentile(dados_impar, 25), stats.scoreatpercentile(dados_impar, 75)
dataset = pd.DataFrame(dados_impar)
dataset
dataset.quantile([0.25,0.5,0.75])
dataset.describe()<jupyter_output><empty_output><jupyter_text>Dados agrupados<jupyter_code>dados = {
    'inferior': [150,154,158,162,166,170],
    'superior': [154,158,162,165,170,174],
    'fi': [5,9,11,7,5,3]
}
dataset = pd.DataFrame(dados)
dataset
dataset['xi'] = (dataset['superior'] + dataset['inferior']) / 2

dataset['fi_xi'] = dataset['fi'] * dataset['xi']

dataset['Fi'] = 0

frequencia_acumulada = []
acumulado = 0
for i in range(len(dataset)):
    acumulado += dataset.values[i, 2]
    frequencia_acumulada.append(acumulado)

dataset['Fi'] = frequencia_acumulada
dataset
def get_quartil(dataframe, quartil=50):
    if quartil == 25:
        fi_4 = dataset['fi'].sum() / 4
    elif quartil == 50:
        fi_4 = dataset['fi'].sum() / 2
    elif quartil == 75:
        fi_4 = (3 * dataset['fi'].sum()) / 4
    else:
        raise Exception('Quartil inválido.')

    limite_inferior, frequencia_classe, id_freq_anterior = 0, 0, 0
    for linha in dataset.iterrows():
        limite_inferior = linha[1][0]
        frequencia_classe = linha[1][2]
        id_freq_anterior = linha[0]

        if linha[1][5] >= fi_4:
            id_freq_anterior -= 1
            break
    
    Fi_anterior = dataset.iloc[[id_freq_anterior]]['Fi'].values[0]
    q = limite_inferior + ((fi_4 - Fi_anterior) * 4) / frequencia_classe

    return q
quartil1 = get_quartil(dataset, 25)
quartil1
quartil2 = get_quartil(dataset, 50)
quartil2
quartil3 = get_quartil(dataset, 75)
quartil3<jupyter_output><empty_output><jupyter_text>Percentis<jupyter_code>import numpy as np
import statistics
from scipy import stats
import pandas as pd
dados = np.array([
    160,165,167,164,160,166,160,161,150,152,173,160,155,
    164,168,162,161,168,163,156,155,169,151,170,164,
    155,152,163,160,155,157,156,158,158,161,154,161,156,172,153
])
np.median(dados)
np.quantile(dados, 0.5)
np.percentile(dados, 50)
np.percentile(dados, 5), np.percentile(dados, 10), np.percentile(dados, 90)
stats.scoreatpercentile(dados, 5), stats.scoreatpercentile(dados, 10), stats.scoreatpercentile(dados, 90)
dataset = pd.DataFrame(dados)
dataset.head()
dataset.quantile([0.05, 0.10, 0.90])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14587664587664587, 'fraction_numerical': 0.0887040887040887, 'mean_word_length': 5.317286652078774, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '84169', 'license': 'MIT', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1163, 'n_words': 186, 'path': '5_medidas_posicao_dispersao/quartis e percentis.ipynb', 'repo_name': 'AlissonRaphael/statistics_for_data_science_and_machine_learning', 'size': '2886'}