日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python所有算法_Python实现的各种常见分布算法示例

發(fā)布時間:2023/12/19 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python所有算法_Python实现的各种常见分布算法示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實(shí)例講述了Python實(shí)現(xiàn)的各種常見分布算法。分享給大家供大家參考,具體如下:

#-*- encoding:utf-8 -*-

import numpy as np

from scipy import stats

import matplotlib.pyplot as plt

#####################

#二項(xiàng)分布

#####################

def test_binom_pmf():

'''

為離散分布

二項(xiàng)分布的例子:拋擲10次硬幣,恰好兩次正面朝上的概率是多少?

'''

n = 10#獨(dú)立實(shí)驗(yàn)次數(shù)

p = 0.5#每次正面朝上概率

k = np.arange(0,11)#0-10次正面朝上概率

binomial = stats.binom.pmf(k,n,p)

print binomial#概率和為1

print sum(binomial)

print binomial[2]

plt.plot(k, binomial,'o-')

plt.title('Binomial: n=%i , p=%.2f' % (n,p),fontsize=15)

plt.xlabel('Number of successes')

plt.ylabel('Probability of success',fontsize=15)

plt.show()

def test_binom_rvs():

'''

為離散分布

使用.rvs函數(shù)模擬一個二項(xiàng)隨機(jī)變量,其中參數(shù)size指定你要進(jìn)行模擬的次數(shù)。我讓Python返回10000個參數(shù)為n和p的二項(xiàng)式隨機(jī)變量

進(jìn)行10000次實(shí)驗(yàn),每次拋10次硬幣,統(tǒng)計有幾次正面朝上,最后統(tǒng)計每次實(shí)驗(yàn)正面朝上的次數(shù)

'''

binom_sim = data = stats.binom.rvs(n=10,p=0.3,size=10000)

print len(binom_sim)

print "mean: %g" % np.mean(binom_sim)

print "SD: %g" % np.std(binom_sim,ddof=1)

plt.hist(binom_sim,bins=10,normed=True)

plt.xlabel('x')

plt.ylabel('density')

plt.show()

#####################

#泊松分布

#####################

def test_poisson_pmf():

'''

泊松分布的例子:已知某路口發(fā)生事故的比率是每天2次,那么在此處一天內(nèi)發(fā)生4次事故的概率是多少?

泊松分布的輸出是一個數(shù)列,包含了發(fā)生0次、1次、2次,直到10次事故的概率。

'''

rate = 2

n = np.arange(0,10)

y = stats.poisson.pmf(n,rate)

print y

plt.plot(n, y, 'o-')

plt.title('Poisson: rate=%i' % (rate), fontsize=15)

plt.xlabel('Number of accidents')

plt.ylabel('Probability of number accidents', fontsize=15)

plt.show()

def test_poisson_rvs():

'''

模擬1000個服從泊松分布的隨機(jī)變量

'''

data = stats.poisson.rvs(mu=2, loc=0, size=1000)

print "mean: %g" % np.mean(data)

print "SD: %g" % np.std(data, ddof=1)

rate = 2

n = np.arange(0,10)

y = stats.poisson.rvs(n,rate)

print y

plt.plot(n, y, 'o-')

plt.title('Poisson: rate=%i' % (rate), fontsize=15)

plt.xlabel('Number of accidents')

plt.ylabel('Probability of number accidents', fontsize=15)

plt.show()

#####################

#正態(tài)分布

#####################

def test_norm_pmf():

'''

正態(tài)分布是一種連續(xù)分布,其函數(shù)可以在實(shí)線上的任何地方取值。

正態(tài)分布由兩個參數(shù)描述:分布的平均值μ和方差σ2 。

'''

mu = 0#mean

sigma = 1#standard deviation

x = np.arange(-5,5,0.1)

y = stats.norm.pdf(x,0,1)

print y

plt.plot(x, y)

plt.title('Normal: $\mu$=%.1f, $\sigma^2$=%.1f' % (mu,sigma))

plt.xlabel('x')

plt.ylabel('Probability density', fontsize=15)

plt.show()

#####################

#beta分布

#####################

def test_beta_pmf():

'''

β分布是一個取值在 [0, 1] 之間的連續(xù)分布,它由兩個形態(tài)參數(shù)α和β的取值所刻畫。

β分布的形狀取決于α和β的值。貝葉斯分析中大量使用了β分布。

'''

a = 0.5#

b = 0.5

x = np.arange(0.01,1,0.01)

y = stats.norm.pdf(x,a,b)

print y

plt.plot(x, y)

plt.title('Beta: a=%.1f, b=%.1f' % (a,b))

plt.xlabel('x')

plt.ylabel('Probability density', fontsize=15)

plt.show()

#####################

#指數(shù)分布(Exponential Distribution)

#####################

def test_exp():

'''

指數(shù)分布是一種連續(xù)概率分布,用于表示獨(dú)立隨機(jī)事件發(fā)生的時間間隔。

比如旅客進(jìn)入機(jī)場的時間間隔、打進(jìn)客服中心電話的時間間隔、中文維基百科新條目出現(xiàn)的時間間隔等等。

'''

lambd = 0.5#

x = np.arange(0,15,0.1)

y =lambd * np.exp(-lambd *x)

print y

plt.plot(x, y)

plt.title('Exponential: $\lambda$=%.2f' % (lambd))

plt.xlabel('x')

plt.ylabel('Probability density', fontsize=15)

plt.show()

def test_expon_rvs():

'''

指數(shù)分布下模擬1000個隨機(jī)變量。scale參數(shù)表示λ的倒數(shù)。函數(shù)np.std中,參數(shù)ddof等于標(biāo)準(zhǔn)偏差除以 $n-1$ 的值。

'''

data = stats.expon.rvs(scale=2, size=1000)

print "mean: %g" % np.mean(data)

print "SD: %g" % np.std(data, ddof=1)

plt.hist(data, bins=20, normed=True)

plt.xlim(0,15)

plt.title('Simulating Exponential Random Variables')

plt.show()

test_expon_rvs()

測試運(yùn)行結(jié)果如下:

希望本文所述對大家Python程序設(shè)計有所幫助。

總結(jié)

以上是生活随笔為你收集整理的python所有算法_Python实现的各种常见分布算法示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。