Click here until yesterday
You will become an engineer in 100 days-Day 59-Programming-Algorithms
You will become an engineer in 100 days --- Day 53 --Git --About Git
You will become an engineer in 100 days --Day 42 --Cloud --About cloud services
You will become an engineer in 100 days --Day 36 --Database --About the database
You will be an engineer in 100 days-Day 24-Python-Basics of Python language 1
You will become an engineer in 100 days --Day 18 --Javascript --JavaScript basics 1
You will become an engineer in 100 days --Day 14 --CSS --CSS Basics 1
You will become an engineer in 100 days --Day 6 --HTML --HTML basics 1
Looking up in the dictionary, it seems to be the ratio to all phenomena
.
In programming, this probability calculation is also common.
Also, statistics as well as probability are one of the fields we deal with. If you go as a programmer, about terms and calculation methods I think it's better to keep it down.
I picked up the terms of probability that I want to suppress.
the term | English | meaning |
---|---|---|
probability | Probability | Rate of events |
permutation | Permutation | Arrange r pieces in a row in the order of taking out r pieces from n different pieces. |
combination | Combination | Extracting r out of n different ones |
Factorial | Factorial | The product of an integer from a positive integer to 1 |
Independence | The results of each other do not affect each other | |
Trial | The act of trying to find the probability | |
Repeated trial | An attempt to repeat the same action without changing the conditions | |
Expected value | Expected value | Mean of values obtained in one trial |
Event | Event | What happens |
Product event | Of the two events A and B, events where A and B occur at the same time | |
All events | A summary of all the consequences | |
Elementary event | Atomic event | Events that cannot be further subdivided |
Complementary event | Events other than one | |
Mutual exclusivity | Events that do not occur at the same time | |
Empty event | An event that does not exist | |
Venn diagram | A visual diagram of the relationships between multiple sets and the extent of the sets | |
Odds | odds | Numerical value showing probability in probability theory |
Probability distribution | probability distribution | Represents the probability of taking each value for a random variable |
Random variable | random variable | The value assigned to what can happen |
Discrete type(Random variable) | Discrete | とびとびの数になるRandom variable |
Continuous type(Random variable) | Continuous | 幾らでも細かく刻むことができるRandom variable |
Python has a library to help you do probability calculations You can calculate the probability relatively easily.
The factorial
can be calculated with factorial
methods such as the math
library.
Since the calculation method is the product of a certain positive integer to an integer from 1.
If it is the factorial of 4, it becomes 4 * 3 * 2 * 1 (4!)
And becomes 24
.
import math
print(math.factorial(4))
print(math.factorial(10))
24 3628800
Permutation
is the number when r are selected from n different ones and arranged in a row`.
Permutations can be generated and enumerated from lists (arrays).
ʻIt is found by permutations
in the itertools` library.
ʻItertools.permutations (list value, number to choose) `
In this case, the returned value itself is not a list If you want to see the values, you need to convert them to a list.
The following is the permutation when 2 out of 3 are selected and arranged.
import itertools
l = ['A', 'B', 'C']
p = itertools.permutations(l, 2)
print(list(p))
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
If the number is small, it can be listed, but if the number of permutations is too large, it cannot be displayed.
If you only want the total number, you can use math.factorial
to calculate it.
The formula is n! / (N --r)!
, So the total number of permutations to select 5
from 7
is
n , r = 7 , 5
math.factorial(n) // math.factorial(n - r)
2520
Now you can find it.
The combination
is the number when selecting r
from different n
, and does not consider the order like a permutation.
Permutations can be generated and enumerated from lists (arrays).
ʻIt is found in combinations
of the itertools` library.
ʻItertools.combinations (list value, number to choose) `
The enumeration of combinations to select 2 out of 5 is as follows.
l = ['a', 'b', 'c', 'd','e']
c = itertools.combinations(l, 2)
print(list(c))
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), ('c', 'd'), ('c', 'e'), ('d', 'e')]
In this case, there is no duplication between elements.
To make a combination by taking into account the duplication of elements
Use combinations_with_replacement
.
ʻItertools.combinations_with_replacement (list value, number to choose) `
l = ['a', 'b', 'c']
c = itertools.combinations_with_replacement(l, 2)
print(list(c))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
If you only want the total number, you can use math.factorial
to calculate it.
The formula is n! / (R! * (N --r)!)
, So the total number of combinations to select 3
from 7
is
It will be as follows.
n , r = 7 , 3
math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
35
To create a direct product of multiple lists
, use product
in the ʻitertools` library.
ʻItertools.product (list value, list value) `
l1 = ['a', 'b', 'c']
l2 = ['X', 'Y']
p = itertools.product(l1, l2)
print(list(p))
[('a', 'X'), ('a', 'Y'), ('b', 'X'), ('b', 'Y'), ('c', 'X'), ('c', 'Y')]
If you specify the number of repetitions in the argument repeat
, the iterable object is used repeatedly.
You can generate a direct product.
ʻItertools.product (list value, repeat = number of times) `
l1 = ['a', 'b', 'c']
p = itertools.product(l1, repeat=2)
print(list(p))
('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
I will touch on probability, but first let's learn terms. Tomorrow is about calculating expected value.
37 days until you become an engineer
Otsu py's HP: http://www.otupy.net/
Youtube: https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw
Twitter: https://twitter.com/otupython
Recommended Posts