如何在python程序中使用概率

如何在python程序中使用概率,python,if-statement,probability,Python,If Statement,Probability,在python中如何计算概率 给定具有以下值的数据集A:[40,10,10,20,10] 我如何计算得到10的概率?(10总共出现3次,3/5=0.6 以下是我编写的代码: #!/usr/bin/env python3 """reducer.py""" import sys # Create a dictionary to map marks Marksprob = {} # Get input from stdin for line in sys.stdin: #Remove sp

在python中如何计算概率

给定具有以下值的数据集
A
[40,10,10,20,10]

我如何计算得到10的概率?(10总共出现3次,3/5=0.6

以下是我编写的代码:

#!/usr/bin/env python3
"""reducer.py"""
import sys

# Create a dictionary to map marks
Marksprob = {}

# Get input from stdin
for line in sys.stdin:
    #Remove spaces from beginning and end of the line
    line = line.strip()

    # parse the input from mapper.py
    ClassA, Marks = line.split('\t', 1)


# Create function that returns probability percent rounded to one decimal place
def event_probability(event_outcomes, sample_space):
    probability = (event_outcomes / sample_space) * 100
    return round(probability, 1)


# Sample Space
ClassA = 25

# Determine the probability of drawing a heart
Marks = 12
grade_probability = event_probability(Marks, ClassA)

# Print each probability
print(str(grade_probability) + '%')

有很多方法可以做到这一点,但这里有一个可能的解决方案:

import numpy as np
import collections

npArray= np.array([40, 10, 10, 20, 10])
c=collections.Counter(npArray) # Generate a dictionary {"value":"nbOfOccurrences"}

arraySize=npArray.size
nbOfOccurrences=c[10] #assuming you want the proba to get 10

proba=(nbOfOccurrences/arraySize)*100
print(proba) #print 60.0

有很多方法可以做到这一点,但这里有一个可能的解决方案:

import numpy as np
import collections

npArray= np.array([40, 10, 10, 20, 10])
c=collections.Counter(npArray) # Generate a dictionary {"value":"nbOfOccurrences"}

arraySize=npArray.size
nbOfOccurrences=c[10] #assuming you want the proba to get 10

proba=(nbOfOccurrences/arraySize)*100
print(proba) #print 60.0

您是否只对该数据集中的值感兴趣?或者该数据集中的值是否演变和变异?不仅是该数据集中的值。还有不同的值。您是否只对该数据集中的值感兴趣?或者该数据集中的值是否演变和变异?不仅是该数据集中的值。还有不同的值。