Как создать коробочный график из данных с весами? - PullRequest
0 голосов
/ 23 сентября 2019

У меня есть данные: a Name количество раз, когда имя появлялось (Count), и Score для каждого имени.Я хочу создать коробку с усами Score, взвешивая каждое имя Score на Count.

Результат должен быть таким же, как если бы у меня были данные в необработанном (не частотном) виде.Но я не хочу на самом деле преобразовывать данные в такую ​​форму, потому что они очень быстро увеличиваются в размере.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {
    "Name":['Sara', 'John', 'Mark', 'Peter', 'Kate'],
    "Count":[20, 10, 5, 2, 5], 
    "Score": [2, 4, 7, 8, 7]
}
df = pd.DataFrame(data)
print(df)
   Count   Name  Score
0     20   Sara      2
1     10   John      4
2      5   Mark      7
3      2  Peter      8
4      5   Kate      7

Я не уверен, как решить эту проблему в Python.Любая помощь приветствуется!

1 Ответ

0 голосов
/ 23 сентября 2019

Вот два пути к вопросу.Вы можете ожидать первого, однако это может быть не очень хорошим решением для вычисления confidence intervals of the median, у него есть следующий код, который использует примеры данных, ссылаясь на matplotlib/cbook/__init__.py.Поэтому Second намного лучше, чем любые другие, поскольку он хорошо протестирован, чем любой другой пользовательский код.

def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None,
                  autorange=False):
    def _bootstrap_median(data, N=5000):
        # determine 95% confidence intervals of the median
        M = len(data)
        percentiles = [2.5, 97.5]

        bs_index = np.random.randint(M, size=(N, M))
        bsData = data[bs_index]
        estimate = np.median(bsData, axis=1, overwrite_input=True)

Первый:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = {
    "Name": ['Sara', 'John', 'Mark', 'Peter', 'Kate'],
    "Count": [20, 10, 5, 2, 5],
    "Score": [2, 4, 7, 8, 7]
}

df = pd.DataFrame(data)
print(df)


def boxplot(values, freqs):
    values = np.array(values)
    freqs = np.array(freqs)
    arg_sorted = np.argsort(values)
    values = values[arg_sorted]
    freqs = freqs[arg_sorted]
    count = freqs.sum()
    fx = values * freqs
    mean = fx.sum() / count
    variance = ((freqs * values ** 2).sum() / count) - mean ** 2
    variance = count / (count - 1) * variance  # dof correction for sample variance
    std = np.sqrt(variance)
    minimum = np.min(values)
    maximum = np.max(values)
    cumcount = np.cumsum(freqs)

    print([std, variance])
    Q1 = values[np.searchsorted(cumcount, 0.25 * count)]
    Q2 = values[np.searchsorted(cumcount, 0.50 * count)]
    Q3 = values[np.searchsorted(cumcount, 0.75 * count)]

    '''
    interquartile range (IQR), also called the midspread or middle 50%, or technically
    H-spread, is a measure of statistical dispersion, being equal to the difference
    between 75th and 25th percentiles, or between upper and lower quartiles,[1][2]
    IQR = Q3 −  Q1. In other words, the IQR is the first quartile subtracted from
    the third quartile; these quartiles can be clearly seen on a box plot on the data.
    It is a trimmed estimator, defined as the 25% trimmed range, and is a commonly used
    robust measure of scale.
    '''

    IQR = Q3 - Q1

    '''
    The whiskers add 1.5 times the IQR to the 75 percentile (aka Q3) and subtract
    1.5 times the IQR from the 25 percentile (aka Q1).  The whiskers should include
    99.3% of the data if from a normal distribution.  So the 6 foot tall man from
    the example would be inside the whisker but my 6 foot 2 inch girlfriend would
    be at the top whisker or pass it.
    '''
    whishi = Q3 + 1.5 * IQR
    whislo = Q1 - 1.5 * IQR

    stats = [{
        'label': 'Scores',  # tick label for the boxplot
        'mean': mean,  # arithmetic mean value
        'iqr': Q3 - Q1,  # 5.0,
#         'cilo': 2.0,  # lower notch around the median
#         'cihi': 4.0,  # upper notch around the median
        'whishi': maximum,  # end of the upper whisker
        'whislo': minimum,  # end of the lower whisker
        'fliers': [],  # '\array([], dtype=int64)',  # outliers
        'q1': Q1,  # first quartile (25th percentile)
        'med': Q2,  # 50th percentile
        'q3': Q3  # third quartile (75th percentile)
    }]

    fs = 10  # fontsize
    _, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
    axes.bxp(stats)
    axes.set_title('Default', fontsize=fs)
    plt.show()


boxplot(df['Score'], df['Count'])

Второй:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


data = {
    "Name": ['Sara', 'John', 'Mark', 'Peter', 'Kate'],
    "Count": [20, 10, 5, 2, 5],
    "Score": [2, 4, 7, 8, 7]
}

df = pd.DataFrame(data)
print(df)

labels = ['Scores']

data = df['Score'].repeat(df['Count']).tolist()

# compute the boxplot stats
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)

print(['stats :', stats])

fs = 10  # fontsize

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), sharey=True)
axes.bxp(stats)
axes.set_title('Boxplot', fontsize=fs)

plt.show()

Ссылки:

...