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

(Part 4)

(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).

If you throw 16 coins

The result of throwing a coin 16 times, which says "For reference" on P63.

The author emphasizes that "the sum of the data converges to a normal distribution" even when throwing an asymmetric coin with a 1/3 chance of appearing in the table.

The reason why it is shaped like a "mountain" is that the easiest thing to understand is that if the data is represented as a 2x2, 3x3, or 8x8 table, "another pattern that produces the same result" from the upper right to the lower left. There are many "", and by adding them together, the closer to the diagonal, the larger the total value = the highest point of the mountain.

code

This time, I will make only a bar graph, so I will not prepare the data to make it a table.


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

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

# Prepare counters
tossCount = 16
counts = [0]*tossCount

# Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):    
    headCount = 0
    for i in range(tossCount): # Toss the coin 16 times
        headCount += tossBiasedCoin()
    counts[headCount] += 1

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

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

plt.xlabel('Number of heads for 16-toss')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(np.arange(tossCount))

plt.tight_layout()
plt.show()

Create a counter variable

Obediently, it creates a list with 16 elements.

# Prepare counters
tossCount = 16
counts = [0]*tossCount

Throw a coin

Throw 16 times and execute 500,000 times to see how many sheets are on the front (though you don't have to turn this much)

## Toss a coin many times to get counts
sampleCount = 50000
for num in range(sampleCount):    
    headCount = 0
    for i in range(tossCount): # Toss the coin 16 times
        headCount += tossBiasedCoin()
    counts[headCount] += 1

Convert to percentage

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

Make a bar graph

I haven't done anything special.

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

plt.xlabel('Number of heads for 16-toss')
plt.ylabel('Probability (%)')
plt.title('Probabilities heads with a biased coin')
plt.xticks(np.arange(tossCount))

plt.tight_layout()
plt.show()

result

A graph similar to Chart 1-21 on page 64 was created.

image

Summary

It's a knack for being able to analyze data with Python, or something that I feel I have to do in the future.

--Although it has nothing to do with Python, a fundamental review and study of statistics and probabilities --I saw one of the key points of Python as a sequence type --Familiarize yourself with libraries for plots and graphs such as matplotlib

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 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 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