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

(Part 2)

(Refer to the page that appears in the text Statistics is the strongest study [Practice] Thoughts and methods for data analysis-by Hiromu Nishiuchi This is the page of the book /9784478028230.html).

Probability of throwing 2 sets with 2 coins thrown as 1 set

In Part 2, consider the result of throwing two sets (that is, throwing a coin four times), assuming that "throw twice" = "one set" as in the example of P61. Since coins are thrown four times, the pattern that appears is

--Table 0 times --Table once --Table twice --Table 3 times --Table 4 times

5 ways.

code


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 3x3 array
counts = [[0 for j in range(3)] for i in range(3)]

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

# Conert all counts to perentage
TWOPLACES = Decimal(10) ** -2 
for i in range(3):
    for j in range(3):
        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(5)
values = [counts[0][0], # 0
          counts[0][1]+counts[1][0], # 1
          counts[0][2]+counts[2][0]+counts[1][1],
          counts[1][2]+counts[2][1],
          counts[2][2]]
          
# 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')

# Create table with column descriptions, add rows, then show it.
table = PrettyTable(["", "1st set 0 head", "1st set 1 head", "1st set 2 heads"])
table.padding_width = 1
table.add_row(counts[0])
table.add_row(counts[1])
table.add_row(counts[2])
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 with two sets')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(keys, np.arange(5))

plt.tight_layout()
plt.show()

The program structure is basically the same as last time.

More variables to store results

Create a 3x3 list to make it the same as Figure 1-19.

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

Throw 2 sets of coins (1 set = 2 times)

Loop 500,000 times and throw coins a total of 4 times each time.

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

Preparation of bar graph data

It's getting a little troublesome. The pattern that "table is twice (sheets)" is out of 2 sets 2 sheets for the 1st set, 0 sheets for the 2nd set 0 for the 1st set, 2 for the 2nd set 1 piece for the 1st set, 1 piece for the 2nd set There are 3 ways, so add them all.

# Make summaries of number of heads.
keys = np.arange(5)
values = [counts[0][0], # 0
          counts[0][1]+counts[1][0], # 1
          counts[0][2]+counts[2][0]+counts[1][1],
          counts[1][2]+counts[2][1],
          counts[2][2]]

Table data preparation

Add a third line to change from a 2x2 structure to a 3x3 structure.

# 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')

Make tables and bar charts

Basically, changes are made in response to the increase.

# Create table with column descriptions, add rows, then show it.
table = PrettyTable(["", "1st set 0 head", "1st set 1 head", "1st set 2 heads"])
table.padding_width = 1
table.add_row(counts[0])
table.add_row(counts[1])
table.add_row(counts[2])
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 with two sets')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(keys, np.arange(5))

plt.tight_layout()
plt.show()

result

image

It looks like a "standard deviation" graph! That's what this chapter is about to demonstrate in the first place.

Furthermore, it can be seen that the probability of "one table" is the highest. This means that the "probability of throwing 4 times and the table appears (1/3)" is "4 x 1/3" = 1.333. In other words, it means that "throwing four cards will usually result in one table."

Continue to (Part 3).

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 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 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
[Data science basics] I tried saving from csv to mysql with python
I want to tweet on Twitter with Python, but I'm addicted to it
Environment maintenance made with Docker (I want to post-process GrADS in Python