<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何用Python添加AB實驗顯著性標(biāo)注

          共 567字,需瀏覽 2分鐘

           ·

          2021-11-15 17:19

          你們好,我是寶器!

          在常規(guī)的實驗統(tǒng)計分析過程中,我們經(jīng)常需要在組間數(shù)據(jù)間添加顯著性標(biāo)注,而在統(tǒng)計學(xué)中,數(shù)據(jù)間的差異顯著性檢驗則是統(tǒng)計假設(shè)檢驗的一種,是一種對組間數(shù)據(jù)或組內(nèi)不同數(shù)據(jù)之間是否有差異以及差異是否明顯判斷方法。在R語言中,我們可以使用ggpupr包中stat_pvalue_manual()函數(shù)或者ggsignif包進(jìn)行繪制。

          今天,小編給大家?guī)淼氖侨绾问褂?span style="color: rgb(0, 87, 255);margin: 1px;background: rgb(242, 242, 242);padding: 0.5px 2.5px;border-radius: 4px;line-height: 1.5em;">Python-Seaborn進(jìn)行顯著性統(tǒng)計圖表繪制,詳細(xì)內(nèi)容如下:

          • Python-Seaborn自定義函數(shù)繪制
          • Python-statannotations庫添加顯著性標(biāo)注

          Python-Seaborn 自定義函數(shù)繪制

          我們可以通過自定義繪圖函數(shù)的方式在統(tǒng)計圖表中添加顯著性標(biāo)注,這里我們直接使用Seaborn自帶的iris數(shù)據(jù)集進(jìn)行繪制,具體內(nèi)容如下:

          自定義P值和星號對應(yīng)關(guān)系

          由于是完全的自定義,這里需要定義一個函數(shù)將P值結(jié)果和對應(yīng)星號進(jìn)行轉(zhuǎn)化,代碼如下:

          def?convert_pvalue_to_asterisks(pvalue):
          ????if?pvalue?<=?0.0001:
          ????????return?"****"
          ????elif?pvalue?<=?0.001:
          ????????return?"***"
          ????elif?pvalue?<=?0.01:
          ????????return?"**"
          ????elif?pvalue?<=?0.05:
          ????????return?"*"
          ????return?"ns"

          scipy.stats 計算顯著性指標(biāo)

          由于scipy.stats部分中提供多種顯著性檢驗方法,如T-test、ANOVA等,由于篇幅有限,這里只介紹scipy.stats.ttest_ind() t檢驗方法,詳細(xì)結(jié)算過程如下:

          iris?=?sns.load_dataset("iris")
          data_p?=?iris[["sepal_length","species"]]
          stat,p_value?=?scipy.stats.ttest_ind(data_p[data_p["species"]=="setosa"]["sepal_length"],
          ?????????????????????????????????????data_p[data_p["species"]=="versicolor"]["sepal_length"],
          ?????????????????????????????????????equal_var=False)

          上述結(jié)果就將P值計算,然后再通過之前定義的convert_pvalue_to_asterisks函數(shù)進(jìn)行轉(zhuǎn)換,得到星號表示形式。

          可視化繪制

          這一步主要使用自定義的繪圖方法進(jìn)行顯著性標(biāo)注繪制,詳細(xì)繪制代碼如下:

          plt.rcParams['font.family']?=?['Times?New?Roman']
          plt.rcParams["axes.labelsize"]?=?18
          palette=['#0073C2FF','#EFC000FF','#868686FF']

          fig,ax?=?plt.subplots(figsize=(5,4),dpi=100,facecolor="w")
          ax?=?sns.barplot(x="species",y="sepal_length",data=iris,palette=palette,
          ?????????????????estimator=np.mean,ci="sd",?capsize=.1,errwidth=1,errcolor="k",
          ?????????????????ax=ax,
          ?????????????????**{"edgecolor":"k","linewidth":1})
          #?添加P值
          x1,?x2?=?0,?1
          y,h?=?data_p["sepal_length"].mean()+1,.2
          #繪制橫線位置
          ax.plot([x1,?x1,?x2,?x2],?[y,?y+h,?y+h,?y],?lw=1,?c="k")?
          #添加P值
          ax.text((x1+x2)*.5,?y+h,?"T-test:?"+?p_value_cov,?ha='center',?va='bottom',?color="k")

          ax.tick_params(which='major',direction='in',length=3,width=1.,labelsize=14,bottom=False)
          for?spine?in?["top","left","right"]:
          ????ax.spines[spine].set_visible(False)
          ax.spines['bottom'].set_linewidth(2)
          ax.grid(axis='y',ls='--',c='gray')
          ax.set_axisbelow(True)
          Python-Seaborn 自定義顯著性標(biāo)注繪制

          「注意」:這里由于是自定義繪制,在添加其他組間數(shù)據(jù)的顯著性編著時也采用相同方法。

          在涉及較多組之間繪制P值是有沒有較為方便便捷的方法呢?下面小編介紹一個優(yōu)秀的第三方拓展庫進(jìn)行繪制。

          Python-statannotations庫添加顯著性標(biāo)注

          Python-statannotations庫則是針對Seaborn繪圖對象進(jìn)行顯著性標(biāo)注的專用庫,其可以提供柱形圖、箱線圖、小提琴圖等統(tǒng)計圖表的顯著性標(biāo)注繪制,計算P值方法基于scipy.stats方法,這里我們簡單列舉幾個示例演示即可,更多詳細(xì)內(nèi)容可參看:Python-statannotations官網(wǎng)[1]

          「樣例一」

          import?seaborn?as?sns

          from?statannotations.Annotator?import?Annotator

          df?=?sns.load_dataset("tips")
          x?=?"day"
          y?=?"total_bill"
          order?=?['Sun',?'Thur',?'Fri',?'Sat']
          fig,ax?=?plt.subplots(figsize=(5,4),dpi=100,facecolor="w")
          ax?=?sns.boxplot(data=df,?x=x,?y=y,?order=order,ax=ax)

          pairs=[("Thur",?"Fri"),?("Thur",?"Sat"),?("Fri",?"Sun")]
          annotator?=?Annotator(ax,?pairs,?data=df,?x=x,?y=y,?order=order)
          annotator.configure(test='Mann-Whitney',?text_format='star',line_height=0.03,line_width=1)
          annotator.apply_and_annotate()

          ax.tick_params(which='major',direction='in',length=3,width=1.,labelsize=14,bottom=False)
          for?spine?in?["top","left","right"]:
          ????ax.spines[spine].set_visible(False)
          ax.spines['bottom'].set_linewidth(2)
          ax.grid(axis='y',ls='--',c='gray')
          ax.set_axisbelow(True)
          Example01 Of Python-statannotations plot make

          「樣例二」

          import?seaborn?as?sns
          import?matplotlib.pyplot?as?plt
          plt.rcParams['font.family']?=?['Times?New?Roman']
          plt.rcParams["axes.labelsize"]?=?18
          #palette=['#0073C2FF','#EFC000FF']
          palette=['#E59F01','#56B4E8']
          #palette?=?["white","black"]


          fig,ax?=?plt.subplots(figsize=(5,4),dpi=100,facecolor="w")
          ax?=?sns.barplot(x="order",y="value",hue="class",data=group_data_p,palette=palette,ci="sd",
          ?????????????????capsize=.1,errwidth=1,errcolor="k",ax=ax,
          ?????????????????**{"edgecolor":"k","linewidth":1})

          #?添加P值
          box_pairs?=?[(("one","type01"),("two","type01")),
          ?????????????(("one","type02"),("two","type02")),
          ?????????????(("one","type01"),("three","type01")),
          ?????????????(("one","type02"),("three","type02")),
          ?????????????(("two","type01"),("three","type01")),
          ?????????????(("two","type02"),("three","type02"))]


          annotator?=??Annotator(ax,?data=group_data_p,?x="order",y="value",hue="class",
          ??????????????????????pairs=box_pairs)
          annotator.configure(test='t-test_ind',?text_format='star',line_height=0.03,line_width=1)
          annotator.apply_and_annotate()
          Example02 Of Python-statannotations plot make

          當(dāng)然,還可以設(shè)置灰色系顏色,如下:

          Example02-2 Of Python-statannotations plot make

          「樣例三」如果針對組間數(shù)據(jù)進(jìn)行統(tǒng)計分析,可以設(shè)置pairs參數(shù)據(jù)如下:

          box_pairs?=?[(("one","type01"),("one","type02")),
          ?????????????(("two","type01"),("two","type02")),
          ?????????????(("three","type01"),("three","type02"))]
          Example03 Of Python-statannotations plot make

          其他比如抖動圖、橫向分組柱形圖等,可視化結(jié)果如下:

          抖動圖顯著性標(biāo)注
          橫向分組柱形圖顯著性圖外添加

          更多使用方法和樣例可參考文末參考資料~~

          總結(jié)

          本期的這篇推文,小編簡單的介紹了如何使用Python繪制顯著性標(biāo)注統(tǒng)計圖表,有自定義方法和優(yōu)秀的第三方包方法,希望對使用Python繪制統(tǒng)計圖表的小伙伴一些靈感~~

          ·················END·················

          推薦閱讀

          1. 我在字節(jié)做了哪些事

          2. 寫給所有數(shù)據(jù)人。

          3. 從留存率業(yè)務(wù)案例談0-1的數(shù)據(jù)指標(biāo)體系

          4. 數(shù)據(jù)分析師的一周

          5. 超級菜鳥如何入門數(shù)據(jù)分析?


          歡迎長按掃碼關(guān)注「數(shù)據(jù)管道」

          瀏覽 240
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  曰本无码人妻丰满熟妇啪啪 | 苍井空无码视频在线观看 | 狠狠干天天日 | 青青草无码 | 在线免费看逼视频 |