programing

matplotlib의 상자 그림 : 마커 및 특이 치

goodcopy 2021. 1. 17. 11:57
반응형

matplotlib의 상자 그림 : 마커 및 특이 치


matplotlib의 상자 그림대한 몇 가지 질문이 있습니다 .

문의 내용 . Q1 , Q2 , Q3로 아래에 강조 표시 한 마커는 무엇을 나타 냅니까? Q1 은 최대이고 Q3 은 이상치 라고 생각 하지만 Q2 는 무엇 입니까?

                       여기에 이미지 설명 입력

질문 B matplotlib는 특이 치를 어떻게 식별 합니까? (즉, 그들이 진실 maxmin가치 가 아니라는 것을 어떻게 알 수 있습니까?)


사진은 천 단어의 가치가 있습니다. 특이 치 ( +플롯 마커)는 단순히 아래 의 넓은 여백을 벗어난 지점 [(Q1-1.5 IQR), (Q3+1.5 IQR)]입니다.

    여기에 이미지 설명 입력

그러나 그림은 정규 분포 데이터 세트의 예일뿐입니다. matplotlib는 먼저 정규 분포를 추정 하지 않고 위에 표시된 것처럼 추정 된 분포 모수에서 사 분위수를 계산 한다는 것을 이해하는 것이 중요합니다 .

대신 중앙값과 사 분위수는 데이터에서 직접 계산됩니다. 따라서 상자 그림은 데이터 분포와 표본 크기에 따라 다르게 보일 수 있습니다 (예 : 비대칭 및 더 많거나 적은 특이 치 포함).


상자는 1 사분 위와 3 사 분위수를 나타내며 빨간색 선은 중앙값 (2 사 분위수)입니다. 문서는 1.5 IQR의 기본 수염을 제공합니다 :

boxplot(x, notch=False, sym='+', vert=True, whis=1.5,
        positions=None, widths=None, patch_artist=False,
        bootstrap=None, usermedians=None, conf_intervals=None)

whis : [기본값 1.5]

내부 사 분위수 범위의 함수로 수염의 길이를 정의합니다. (whis * (75 % -25 %)) 데이터 범위 내에서 가장 극단적 인 데이터 포인트로 확장됩니다.

다른 상자 그림 표현에 대해 혼란 스러우면 wikipedia의 설명을 읽어보십시오 .


seth 답변 외에도 (문서가 이에 대해 매우 정확하지 않기 때문에) : Q1 (위스커)은 75 % + 1.5 IQR 미만의 최대 값에 배치됩니다.

(최소값 25 %-1.5 IQR)

다음은 수염 위치를 계산하는 코드입니다.

        # get high extreme
        iq = q3 - q1
        hi_val = q3 + whis * iq
        wisk_hi = np.compress(d <= hi_val, d)
        if len(wisk_hi) == 0 or np.max(wisk_hi) < q3:
            wisk_hi = q3
        else:
            wisk_hi = max(wisk_hi)

        # get low extreme
        lo_val = q1 - whis * iq
        wisk_lo = np.compress(d >= lo_val, d)
        if len(wisk_lo) == 0 or np.min(wisk_lo) > q1:
            wisk_lo = q1
        else:
            wisk_lo = min(wisk_lo)

아래 이미지는 상자 그림의 여러 부분을 보여줍니다.

여기에 이미지 설명 입력

분위수 1 / Q1 : 25 번째 백분위 수

사 분위수 범위 (IQR) : 25 번째 백분위 수에서 75 번째 백분위 수까지.

중앙값 (분위수 2 / Q2) : 50 번째 백분위 수.

분위수 3 / Q3 : 75 번째 백분위 수.

파란색 부분은 상자 그림의 수염입니다.

The image below compares the box plot of a normal distribution against the probability density function. It should help explain the "Minimum", "Maximum", and outliers.

여기에 이미지 설명 입력

"Minimum": (Q1-1.5 IQR)

"Maximum": (Q3+1.5 IQR)

As zelusp said, 99.3% of data is contained within 2.698σ (standard deviations) for a normal distribution. The green circles (outliers) in the image below are the remaining .7% of the data. Here is a derivation of how those numbers came to be.


Just in case this can benefit anyone else, I needed to put a legend on one of my box plot graphs so I made this little .png in Inkscape and thought I'd share it.

여기에 이미지 설명 입력


Here's a graphic that illustrates the components of the box from a stats.stackexchange answer. Note that k=1.5 if you don't supply the whis keyword in Pandas.

여기에 이미지 설명 입력

The boxplot function in Pandas is a wrapper for matplotlib.pyplot.boxplot. The matplotlib docs explain the components of the boxes in detail:

Question A:

The box extends from the lower to upper quartile values of the data, with a line at the median.

i. e. a quarter of the input data values is below the box and a quarter above the box.

Question B:

whis : float, sequence, or string (default = 1.5)

As a float, determines the reach of the whiskers to the beyond the first and third quartiles. In other words, where IQR is the interquartile range (Q3-Q1), the upper whisker will extend to last datum less than Q3 + whis*IQR). Similarly, the lower whisker will extend to the first datum greater than Q1 - whis*IQR. Beyond the whiskers, data are considered outliers and are plotted as individual points.

Matplotlib (and Pandas) also gives you a lot of options to change this default definition of the whiskers:

Set this to an unreasonably high value to force the whiskers to show the min and max values. Alternatively, set this to an ascending sequence of percentile (e.g., [5, 95]) to set the whiskers at specific percentiles of the data. Finally, whis can be the string 'range' to force the whiskers to the min and max of the data.

참조 URL : https://stackoverflow.com/questions/17725927/boxplots-in-matplotlib-markers-and-outliers

반응형