Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

二项分布


二项分布

二项分布是一种离散分布

它描述了二元场景的结果,例如抛硬币,结果要么是正面要么是反面。

它有三个参数

n - 试验次数。

p - 每次试验发生的概率(例如,抛硬币时,每次为 0.5)。

size - 返回数组的形状。

离散分布:分布定义在事件的离散集合上,例如,抛硬币的结果是离散的,因为它只能是正面或反面,而人的身高是连续的,因为它可以是 170、170.1、170.11 等。

示例

给定抛硬币 10 次试验,生成 10 个数据点

from numpy import random

x = random.binomial(n=10, p=0.5, size=10)

print(x)
自己试试 »

二项分布的可视化

示例

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.binomial(n=10, p=0.5, size=1000), hist=True, kde=False)

plt.show()

结果

自己试试 »

正态分布与二项分布的区别

主要区别在于正态分布是连续的,而二项分布是离散的,但如果数据点足够多,它将非常类似于具有特定位置和尺度的正态分布。

示例

from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot(random.normal(loc=50, scale=5, size=1000), hist=False, label='normal')
sns.distplot(random.binomial(n=100, p=0.5, size=1000), hist=False, label='binomial')

plt.show()

结果

自己试试 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.