Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

python - 如何在python程序中使用概率(how to use probability in python program)

I am new in python.

(我是python的新手。)

my question how to take probability in python.

(我的问题如何在python中采取概率。)

I have a data set like this

(我有这样的数据集)

Class A there is marks 40 10 10 20 10 How can i take probability from these marks like 10 is 3 times from total 5. 3/5 ?

(A级有分数40 10 10 20 10我如何从这些分数中得出概率,例如10是总数5的3倍。3/5?)

#!/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('', 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) + '%')
  ask by Humair Ali translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are many ways to do this but here is a possible solution:

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

import numpy as np
import collections

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...