您好,登錄后才能下訂單哦!
小編給大家分享一下Python箱型圖怎么繪制與特征值獲取過程,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
它主要用于反映原始數(shù)據(jù)分布的特征,還可以進(jìn)行多組數(shù)據(jù)分布特征的比較
如何利用Python繪制箱型圖
需要的import的包
import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties import numpy as np import pandas as pd
該函數(shù)是繪制多箱型圖,且數(shù)據(jù)長(zhǎng)度不一致的情況,input_dict = {filename1:[a1,a2,...,an],filename2:[b1,b2,...,bn]...} Y_label = 'Img_name'
def DrawMultBoxPic(input_dict,Y_label): dict_list_length = [] for item in input_dict: temp_length = len(input_dict[item]) dict_list_length.append(temp_length) # 獲取最長(zhǎng)列表長(zhǎng)度 max_length = max(dict_list_length) # 每個(gè)列表在后面追加None for item in input_dict: diff_length = max_length - len(input_dict[item]) if diff_length > 0: for i in range(diff_length): input_dict[item].append(None) # else: # print('{}文件列表長(zhǎng)度最長(zhǎng)'.format(item)) # 繪制箱型圖 zhfont = FontProperties(fname='C:/Windows/Fonts/simsun.ttc', size=16) data = pd.DataFrame.from_dict(input_dict) data.boxplot(widths=0.3,figsize=(30,15),fontsize=16) plt.xlabel(u'煤質(zhì)文件名稱', fontproperties=zhfont) plt.ylabel(Y_label, fontproperties=zhfont) plt.title(Y_label, fontproperties=zhfont) # plt.axis([0, 6, 0, 90]) plt.grid(axis='y', ls='--', lw=2, color='gray', alpha=0.4) plt.grid(axis='x', ls='--', lw=2, color='gray', alpha=0.4) imgname = 'E:\\' + Y_label + '.png' plt.savefig(imgname, bbox_inches = 'tight') # plt.show()
結(jié)果顯示
如何獲取箱型圖特征
""" 【函數(shù)說明】獲取箱體圖特征 【輸入】 input_list 輸入數(shù)據(jù)列表 【輸出】 out_list:列表的特征[下限,Q1,Q2,Q3,上限] 和 Error_Point_num:異常值數(shù)量 【版本】 V1.0.0 【日期】 2019 10 16 """ def BoxFeature(input_list): # 獲取箱體圖特征 percentile = np.percentile(input_list, (25, 50, 75), interpolation='linear') #以下為箱線圖的五個(gè)特征值 Q1 = percentile[0]#上四分位數(shù) Q2 = percentile[1] Q3 = percentile[2]#下四分位數(shù) IQR = Q3 - Q1#四分位距 ulim = Q3 + 1.5*IQR#上限 非異常范圍內(nèi)的最大值 llim = Q1 - 1.5*IQR#下限 非異常范圍內(nèi)的最小值 # llim = 0 if llim < 0 else llim # out_list = [llim,Q1,Q2,Q3,ulim] # 統(tǒng)計(jì)異常點(diǎn)個(gè)數(shù) # 正常數(shù)據(jù)列表 right_list = [] Error_Point_num = 0 value_total = 0 average_num = 0 for item in input_list: if item < llim or item > ulim: Error_Point_num += 1 else: right_list.append(item) value_total += item average_num += 1 average_value = value_total/average_num # 特征值保留一位小數(shù) out_list = [average_value,min(right_list), Q1, Q2, Q3, max(right_list)] # print(out_list) out_list = Save1point(out_list) return out_list,Error_Point_num
python的數(shù)據(jù)類型:1. 數(shù)字類型,包括int(整型)、long(長(zhǎng)整型)和float(浮點(diǎn)型)。2.字符串,分別是str類型和unicode類型。3.布爾型,Python布爾類型也是用于邏輯運(yùn)算,有兩個(gè)值:True(真)和False(假)。4.列表,列表是Python中使用最頻繁的數(shù)據(jù)類型,集合中可以放任何數(shù)據(jù)類型。5. 元組,元組用”()”標(biāo)識(shí),內(nèi)部元素用逗號(hào)隔開。6. 字典,字典是一種鍵值對(duì)的集合。7. 集合,集合是一個(gè)無序的、不重復(fù)的數(shù)據(jù)組合。
以上是“Python箱型圖怎么繪制與特征值獲取過程”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。