I want to be able to analyze data with Python (Part 3)

(Part 3)

1 set of coin throws = 3 times

This wasn't in the book, but I simply changed "1 set 2 throws" to "1 set 3 throws" and changed the total from "4 throws" to "6 throws".

code

There is no commentary. The contents are the same as the previous (1) and (2).

from random import randint
from decimal import Decimal
from prettytable import PrettyTable
import numpy as np

def tossBiasedCoin():
    """ Returns 0 or 1 with 0 having 2/3 chance """
    return randint(0,2) % 2

# Make a 4x4 array
counts = [[0 for j in range(4)] for i in range(4)]

# Toss a coin many times to get counts
sampleCount = 500000
for num in range(sampleCount):    
    firstSet = [tossBiasedCoin(),tossBiasedCoin(),tossBiasedCoin()] # 0, 1, 2, 3
    secondSet = [tossBiasedCoin(),tossBiasedCoin(),tossBiasedCoin()]
    counts[sum(secondSet)][sum(firstSet)] += 1

# Conert all counts to perentage
TWOPLACES = Decimal(10) ** -2 
for i in range(4):
    for j in range(4):
        value = counts[i][j]        
        counts[i][j] = (100 * Decimal(counts[i][j])/Decimal(sampleCount)).quantize(TWOPLACES)
        print("Converted the value {} to percentage {}".format(value, counts[i][j]))

# Make summaries of number of heads.
keys = np.arange(7)
values = [counts[0][0], # 0
          counts[0][1]+counts[1][0], # 1
          counts[0][2]+counts[2][0]+counts[1][1],
          counts[0][3]+counts[1][2]+counts[2][1]+counts[3][0],
          counts[1][3]+counts[2][2]+counts[3][1],
          counts[2][3]+counts[3][2],
          counts[3][3]]
          
# Add row descriptions
counts[0].insert(0, '2nd set 0 head')
counts[1].insert(0, '2nd set 1 head')
counts[2].insert(0, '2nd set 2 heads')
counts[3].insert(0, '2nd set 3 heads')

# Create table with column descriptions, add rows, then show it.
table = PrettyTable(["", "1st set 0 head", "1st set 1 head", "1st set 2 heads", "1st set 3 heads"])
table.padding_width = 1
table.add_row(counts[0])
table.add_row(counts[1])
table.add_row(counts[2])
table.add_row(counts[3])
print table

# Draw a bar chart
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
rects = plt.bar(keys,
                 values, 
                 0.5,
                 alpha=0.4,
                 align="center", 
                 color='b')

plt.xlabel('Number of heads')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(keys, np.arange(7))

plt.tight_layout()
plt.show()

result

It has become more like a standard deviation.

image

The number of cards that can be thrown at 6 is "6 x 1/3", which means that it is easy to get 2 cards.

(Part 4) is continued.

Recommended Posts

I want to be able to analyze data with Python (Part 3)
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to analyze logs with Python
I tried to analyze J League data with Python
I want to debug with Python
I want to be able to run Python in VS Code
I want to play with aws with python
☆ Professor Anzai… !! I want to analyze the data …… Part 1 Data preparation ☆ Let's analyze the NBA player stats (results) with Python. basketball
[Pandas] I tried to analyze sales data with Python [For beginners]
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I tried to get CloudWatch data with Python
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to handle optimization with python and cplex
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to run a quantum computer with Python
I want to specify another version of Python with pyvenv
I tried to make various "dummy data" with Python faker
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
I want to do ○○ with Pandas
I'm tired of Python, so I tried to analyze the data with nehan (I want to go live even with corona sickness-Part 2)
I'm tired of Python, so I tried to analyze the data with nehan (I want to go live even with corona sickness-Part 1)
I know? Data analysis using Python or things you want to use when you want with numpy
I want to use a wildcard that I want to shell with Python remove
I want to know the weather with LINE bot feat.Heroku + Python
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to output the beginning of the next month with Python
Try to analyze online family mahjong using Python (PART 1: Take DATA)
I want to do a full text search with elasticsearch + python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to detect objects with OpenCV
Process Pubmed .xml data with python [Part 2]
I want to handle the rhyme part1
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
Convert Excel data to JSON with python
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to pip install with PythonAnywhere
Convert FX 1-minute data to 5-minute data with Python
[Part1] Scraping with Python → Organize to csv!
I wanted to solve ABC172 with Python
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to do it with Python lambda Django, but I will stop