亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python 生成正態分布數據,并繪圖和解析

瀏覽:62日期:2022-07-01 15:22:04
1、生成正態分布數據并繪制概率分布圖

import pandas as pdimport numpy as npimport matplotlib.pyplot as plt# 根據均值、標準差,求指定范圍的正態分布概率值def normfun(x, mu, sigma): pdf = np.exp(-((x - mu)**2)/(2*sigma**2)) / (sigma * np.sqrt(2*np.pi)) return pdf# result = np.random.randint(-65, 80, size=100) # 最小值,最大值,數量result = np.random.normal(15, 44, 100) # 均值為0.5,方差為1print(result)x = np.arange(min(result), max(result), 0.1)# 設定 y 軸,載入剛才的正態分布函數print(result.mean(), result.std())y = normfun(x, result.mean(), result.std())plt.plot(x, y) # 這里畫出理論的正態分布概率曲線# 這里畫出實際的參數概率與取值關系plt.hist(result, bins=10, rwidth=0.8, density=True) # bins個柱狀圖,寬度是rwidth(0~1),=1沒有縫隙plt.title(’distribution’)plt.xlabel(’temperature’)plt.ylabel(’probability’)# 輸出plt.show() # 最后圖片的概率和不為1是因為正態分布是從負無窮到正無窮,這里指截取了數據最小值到最大值的分布

python 生成正態分布數據,并繪圖和解析

根據范圍生成正態分布:

result = np.random.randint(-65, 80, size=100) # 最小值,最大值,數量

根據均值、方差生成正態分布:

result = np.random.normal(15, 44, 100) # 均值為0.5,方差為12、判斷一個序列是否符合正態分布

import numpy as npfrom scipy import statspts = 1000np.random.seed(28041990)a = np.random.normal(0, 1, size=pts) # 生成1個正態分布,均值為0,標準差為1,100個點b = np.random.normal(2, 1, size=pts) # 生成1個正態分布,均值為2,標準差為1, 100個點x = np.concatenate((a, b)) # 把兩個正態分布連接起來,所以理論上變成了非正態分布序列k2, p = stats.normaltest(x)alpha = 1e-3print('p = {:g}'.format(p))# 原假設:x是一個正態分布if p < alpha: # null hypothesis: x comes from a normal distribution print('The null hypothesis can be rejected') # 原假設可被拒絕,即不是正態分布else: print('The null hypothesis cannot be rejected') # 原假設不可被拒絕,即使正態分布3、求置信區間、異常值

import numpy as npimport matplotlib.pyplot as pltfrom scipy import statsimport pandas as pd# 求列表數據的異常點def get_outer_data(data_list): df = pd.DataFrame(data_list, columns=[’value’]) df = df.iloc[:, 0] # 計算下四分位數和上四分位 Q1 = df.quantile(q=0.25) Q3 = df.quantile(q=0.75) # 基于1.5倍的四分位差計算上下須對應的值 low_whisker = Q1 - 1.5 * (Q3 - Q1) up_whisker = Q3 + 1.5 * (Q3 - Q1) # 尋找異常點 kk = df[(df > up_whisker) | (df < low_whisker)] data1 = pd.DataFrame({’id’: kk.index, ’異常值’: kk}) return data1N = 100result = np.random.normal(0, 1, N)# result = np.random.randint(-65, 80, size=N) # 最小值,最大值,數量mean, std = result.mean(), result.std(ddof=1) # 求均值和標準差# 計算置信區間,這里的0.9是置信水平conf_intveral = stats.norm.interval(0.9, loc=mean, scale=std) # 90%概率print(’置信區間:’, conf_intveral)x = np.arange(0, len(result), 1)# 求異常值outer = get_outer_data(result)print(outer, type(outer))x1 = outer.iloc[:, 0]y1 = outer.iloc[:, 1]plt.scatter(x1, y1, marker=’x’, color=’r’) # 所有離散點plt.scatter(x, result, marker=’.’, color=’g’) # 異常點plt.plot([0, len(result)], [conf_intveral[0], conf_intveral[0]])plt.plot([0, len(result)], [conf_intveral[1], conf_intveral[1]])plt.show()

python 生成正態分布數據,并繪圖和解析

4、采樣點離散圖和概率圖

import numpy as npimport matplotlib.pyplot as pltfrom scipy import statsimport pandas as pdimport timeprint(time.strftime(’%Y-%m-%D %H:%M:%S’))# 根據均值、標準差,求指定范圍的正態分布概率值def _normfun(x, mu, sigma): pdf = np.exp(-((x - mu)**2)/(2*sigma**2)) / (sigma * np.sqrt(2*np.pi)) return pdf# 求列表數據的異常點def get_outer_data(data_list): df = pd.DataFrame(data_list, columns=[’value’]) df = df.iloc[:, 0] # 計算下四分位數和上四分位 Q1 = df.quantile(q=0.25) Q3 = df.quantile(q=0.75) # 基于1.5倍的四分位差計算上下須對應的值 low_whisker = Q1 - 1.5 * (Q3 - Q1) up_whisker = Q3 + 1.5 * (Q3 - Q1) # 尋找異常點 kk = df[(df > up_whisker) | (df < low_whisker)] data1 = pd.DataFrame({’id’: kk.index, ’異常值’: kk}) return data1N = 100result = np.random.normal(0, 1, N)# result = np.random.randint(-65, 80, size=N) # 最小值,最大值,數量# result = [100]*100 # 取值全相同# result = np.array(result)mean, std = result.mean(), result.std(ddof=1) # 求均值和標準差# 計算置信區間,這里的0.9是置信水平if std == 0: # 如果所有值都相同即標準差為0則無法計算置信區間 conf_intveral = [min(result)-1, max(result)+1]else: conf_intveral = stats.norm.interval(0.9, loc=mean, scale=std) # 90%概率# print(’置信區間:’, conf_intveral)# 求異常值outer = get_outer_data(result)# 繪制離散圖fig = plt.figure()fig.add_subplot(2, 1, 1)plt.subplots_adjust(hspace=0.3)x = np.arange(0, len(result), 1)plt.scatter(x, result, marker=’.’, color=’g’) # 畫所有離散點plt.scatter(outer.iloc[:, 0], outer.iloc[:, 1], marker=’x’, color=’r’) # 畫異常離散點plt.plot([0, len(result)], [conf_intveral[0], conf_intveral[0]]) # 置信區間線條plt.plot([0, len(result)], [conf_intveral[1], conf_intveral[1]]) # 置信區間線條plt.text(0, conf_intveral[0], ’{:.2f}’.format(conf_intveral[0])) # 置信區間數字顯示plt.text(0, conf_intveral[1], ’{:.2f}’.format(conf_intveral[1])) # 置信區間數字顯示info = ’outer count:{}’.format(len(outer.iloc[:, 0]))plt.text(min(x), max(result)-((max(result)-min(result)) / 2), info) # 異常點數顯示plt.xlabel(’sample count’)plt.ylabel(’value’)# 繪制概率圖if std != 0: # 如果所有取值都相同 fig.add_subplot(2, 1, 2) x = np.arange(min(result), max(result), 0.1) y = _normfun(x, result.mean(), result.std()) plt.plot(x, y) # 這里畫出理論的正態分布概率曲線 plt.hist(result, bins=10, rwidth=0.8, density=True) # bins個柱狀圖,寬度是rwidth(0~1),=1沒有縫隙 info = ’mean:{:.2f}nstd:{:.2f}nmode num:{:.2f}’.format(mean, std, np.median(result)) plt.text(min(x), max(y) / 2, info) plt.xlabel(’value’) plt.ylabel(’Probability’)else: fig.add_subplot(2, 1, 2) info = ’non-normal distribution!!nmean:{:.2f}nstd:{:.2f}nmode num:{:.2f}’.format(mean, std, np.median(result)) plt.text(0.5, 0.5, info) plt.xlabel(’value’) plt.ylabel(’Probability’)plt.savefig(’./distribution.jpg’)plt.show()print(time.strftime(’%Y-%m-%D %H:%M:%S’))

python 生成正態分布數據,并繪圖和解析

以上就是python 生成正態分布數據,并繪圖和解析的詳細內容,更多關于python 正態分布的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
主站蜘蛛池模板: 亚洲精品一区二区深夜福利 | 日韩视频在线观看视频 | 久久久午夜影院 | 成人久久久精品乱码一区二区三区 | 日本wwwwwxxxxx| 国产成人精品1024在线 | 天天综合色网 | 亚洲国产精品久久精品怡红院 | 亚洲欧美久久久久久久久久爽网站 | 日韩欧美黄色大片 | 日本黄色小说网站 | 91福利视频合集 | 亚洲综合黄色 | 黄色a一级 | a一区二区三区视频 | 全免费a级毛片免费看不卡 全免费a级毛片免费看视频免 | 亚洲4kk44kk在线 | 国产亚洲精品欧美一区 | 成人免费一级在线播放 | 91网站在线播放 | 国产日韩欧美精品一区 | 免费a级特黄国产大片 | 亚洲国产综合网 | 在线观看91香蕉国产免费 | 国产精品亚洲精品 | 中文字幕亚洲国产 | 国产精品视频九九九 | www.黄色网址| 免费看黄网址 | 欧美久久一区二区 | 综合欧美亚洲 | 国产一级爱c片免费播放 | 国产视频黄色 | 精品美女在线观看 | 天天影视色香欲综合网天天录日日录 | 国内精品51视频在线观看 | 免费一级特黄a | 国产美女视频做爰 | 国产叼嘿久久精品久久 | 在线观看日本污污ww网站 | 日韩久久一级毛片 |