尝试用python解概率题,并祝大小朋友儿童节快乐

简介: 尝试用python解概率题,并祝大小朋友儿童节快乐

实题操作


1. 三个人独立地去破译一份密码,每人能独立译出这份密码的概率分别为1/5, 1/3, 1/4。则这份密码被译出的概率为(3/5)。

def success():
    p = 1/5,1/3,1/4
    t = 1
    for i in p:
        t *= 1-i
    return 1-t
print(f'成功概率:{success():.3f}')



2. 甲、乙、丙三人向同一飞机射击,假设他们的命中率都是0.4;又若只有一人命中时,飞机坠毁的概率为 0.2;恰有两人命中时,飞机坠毁的概率为 0.6;若三人同时命中,飞机必坠毁。求飞机坠毁的概率为(202/625)。

p1 = [1-0.4,0.4]        # 不中和击种的概率
p2 = [0,0.2,0.6,1.0]    # 不同人数击中的坠机概率
p0 = 0                  # 初始总概率
for i in range(2):
  for j in range(2):
    for k in range(2):
      t = p1[i] * p1[j] * p1[k] * p2[i+j+k]
      p0 += t
      print(f'{i} {j} {k} : {p1[i]} * {p1[j]} * {p1[k]} * {p2[i+j+k]:.1f} = {t:.4f}')
import fractions 
print(f'\nTotal : {fractions.Fraction(str(0.3232))}')
'''
0 0 0 : 0.6 * 0.6 * 0.6 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.6 * 0.4 * 0.2 = 0.0288
0 1 0 : 0.6 * 0.4 * 0.6 * 0.2 = 0.0288
0 1 1 : 0.6 * 0.4 * 0.4 * 0.6 = 0.0576
1 0 0 : 0.4 * 0.6 * 0.6 * 0.2 = 0.0288
1 0 1 : 0.4 * 0.6 * 0.4 * 0.6 = 0.0576
1 1 0 : 0.4 * 0.4 * 0.6 * 0.6 = 0.0576
1 1 1 : 0.4 * 0.4 * 0.4 * 1.0 = 0.0640
Total : 202/625
'''



3. 甲、乙、丙三人向同一飞机射击,假设他们的命中率分别是:0.4, 0.5, 0.7;又若只有一人命中时,飞机坠毁的概率为 0.2;恰有两人命中时,飞机坠毁的概率为 0.6;若三人同时命中,飞机必坠毁。求飞机坠毁的概率为(229/500)。

继上题,只是3人的命中率不同;代码稍作修改即可:


p1 = [[1-0.4,0.4],[1-0.5,0.5],[1-0.7,0.7]]
p2 = [0,0.2,0.6,1.0]    # 不同人数击中的坠机概率
p0 = 0                  # 初始总概率
for i in range(2):
  for j in range(2):
    for k in range(2):
      t = p1[0][i] * p1[1][j] * p1[2][k] * p2[i+j+k]
      p0 += t
      print(f'{i} {j} {k} : {p1[0][i]:.1f} * {p1[1][j]:.1f} * {p1[2][k]:.1f} * {p2[i+j+k]:.1f} = {t:.4f}')
import fractions 
print(f'\nTotal : {fractions.Fraction(str(round(p0,5)))}')
'''
0 0 0 : 0.6 * 0.5 * 0.3 * 0.0 = 0.0000
0 0 1 : 0.6 * 0.5 * 0.7 * 0.2 = 0.0420
0 1 0 : 0.6 * 0.5 * 0.3 * 0.2 = 0.0180
0 1 1 : 0.6 * 0.5 * 0.7 * 0.6 = 0.1260
1 0 0 : 0.4 * 0.5 * 0.3 * 0.2 = 0.0120
1 0 1 : 0.4 * 0.5 * 0.7 * 0.6 = 0.0840
1 1 0 : 0.4 * 0.5 * 0.3 * 0.6 = 0.0360
1 1 1 : 0.4 * 0.5 * 0.7 * 1.0 = 0.1400
Total : 229/500
'''




实用模块之类方法函数


小数转分数(以下基本是为了凑字数,不喜勿喷忽略即可)


fractions.Fraction

 |      Examples
 |      --------
 |      
 |      >>> Fraction(10, -8)
 |      Fraction(-5, 4)
 |      >>> Fraction(Fraction(1, 7), 5)
 |      Fraction(1, 35)
 |      >>> Fraction(Fraction(1, 7), Fraction(2, 3))
 |      Fraction(3, 14)
 |      >>> Fraction('314')
 |      Fraction(314, 1)
 |      >>> Fraction('-35/4')
 |      Fraction(-35, 4)
 |      >>> Fraction('3.1415') # conversion from numeric string
 |      Fraction(6283, 2000)
 |      >>> Fraction('-47e-2') # string may include a decimal exponent
 |      Fraction(-47, 100)
 |      >>> Fraction(1.47)  # direct construction from float (exact conversion)
 |      Fraction(6620291452234629, 4503599627370496)
 |      >>> Fraction(2.25)
 |      Fraction(9, 4)
 |      >>> Fraction(Decimal('1.47'))
 |      Fraction(147, 100)
 |      >>> Fraction('8.125')
 |      Fraction(65, 8)
 |      >>> print(Fraction('8.125'))
 |      65/8
 |      >>> print(Fraction(0.125))
 |      1/8

另外解决概率题经常要用到排列、组合函数:




itertools.combinations


Help on class combinations in module itertools:
class combinations(builtins.object)
 |  combinations(iterable, r)
 |  
 |  Return successive r-length combinations of elements in the iterable.
 |  
 |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.



itertools.permutations

Help on class permutations in module itertools:

class permutations(builtins.object)
 |  permutations(iterable, r=None)
 |  
 |  Return successive r-length permutations of elements in the iterable.
 |  
 |  permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.

还有一个可以取重复值的组合公式,知道的比较少:




tertools.combinations_with_replacement

Help on class combinations_with_replacement in module itertools:

class combinations_with_replacement(builtins.object)
 |  combinations_with_replacement(iterable, r)
 |  
 |  Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.
 |  
 |  combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __setstate__(...)
 |      Set state information for unpickling.
 |  
 |  __sizeof__(...)
 |      Returns size in memory, in bytes.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.



目录
相关文章
|
18小时前
|
机器学习/深度学习 数据可视化 TensorFlow
Python用线性回归和TensorFlow非线性概率神经网络不同激活函数分析可视化
Python用线性回归和TensorFlow非线性概率神经网络不同激活函数分析可视化
|
18小时前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
21 1
|
18小时前
|
数据可视化 Python
PYTHON 贝叶斯概率推断序列数据概率和先验、似然和后验图可视化
PYTHON 贝叶斯概率推断序列数据概率和先验、似然和后验图可视化
|
18小时前
|
数据可视化 Python
PYTHON贝叶斯推断计算:用BETA先验分布推断概率和可视化案例
PYTHON贝叶斯推断计算:用BETA先验分布推断概率和可视化案例
|
18小时前
|
机器学习/深度学习 算法 数据挖掘
python数据分析——在数据分析中有关概率论的知识
参数和统计量在数据分析中起着至关重要的作用。参数是对总体特征的描述,如均值、方差等,而统计量则是基于样本数据计算得出的,用于估计或推断总体参数的值。 在统计学中,参数通常被视为未知的固定值,而统计量则是随机变量,因为它们的值会随着样本的不同而变化。这种差异使得统计量在推断总体参数时具有重要意义。例如,我们可以通过计算样本均值来估计总体均值,这就是一个典型的统计量应用。
78 1
|
7月前
|
前端开发 测试技术 Linux
芯片人的快乐——python+systemverilog用波形祝你新春快乐 |献上祝福语波形生成器|
芯片人的快乐——python+systemverilog用波形祝你新春快乐 |献上祝福语波形生成器|
|
18小时前
|
机器学习/深度学习 数据采集 算法
GEE python——基于多源遥感影像和随机森林分类器进行洪水概率预测
GEE python——基于多源遥感影像和随机森林分类器进行洪水概率预测
60 0
|
18小时前
|
Linux 定位技术 iOS开发
【Python实现坦克大战,带你找回童年的快乐】附源码
【Python实现坦克大战,带你找回童年的快乐】附源码
51 0
|
18小时前
|
Python Java Go
Python每日一练(20230409) 字符串拆分数值求和、快乐数、格雷编码
Python每日一练(20230409) 字符串拆分数值求和、快乐数、格雷编码
45 0
Python每日一练(20230409) 字符串拆分数值求和、快乐数、格雷编码
|
18小时前
|
机器学习/深度学习 自然语言处理 算法
【Python自然语言处理】概率上下文无关文法(PCFG)及神经网络句法分析讲解(图文解释 超详细)
【Python自然语言处理】概率上下文无关文法(PCFG)及神经网络句法分析讲解(图文解释 超详细)
95 0