<jupyter_start><jupyter_text>Guessing Gamesmachine learning, one step at a timeStep 4. Seeing Things  4. Let's invent a game that involves throwing pairs of dice. Here are the rules:the maximum score is 20roll a pair of dice, add them up, that's your score    decide if you want to roll both dice again... if you do, add the roll to your scoreget as close as you can to a total of 20...but if you go over 20, your score is zero. First, let's write a function to roll a speficied number of dice:<jupyter_code>import random

def roll_dice(number_of_dice):
    
    sum = 0
    
    for i in range(number_of_dice):
        sum += random.randrange(6)+1     # randrange is 0 through 5, so add 1 to get a six-sided die roll
        
    return sum

roll_dice(1)
roll_dice(2)
roll_dice(13)<jupyter_output><empty_output><jupyter_text>Now let's step back and try to **visualize** what happens when we roll dice.Here is a function that counts the cumulative number of times that total occurs when rolling dice:<jupyter_code>def count_rolls(number_of_dice, number_of_rolls):
    
    counts = []
    
    for i in range(number_of_dice * 6 + 1):        # make a list to store the totals for each roll...
        counts.append(0)                           # start with a list full of zeros
    
    for i in range(number_of_rolls):               # loop: once for each roll...
        counts[roll_dice(number_of_dice)] += 1     # ...for each roll, increment the related list entry
        
    return counts                                  # return the list, which contains a count of each r<jupyter_output><empty_output><jupyter_text>That works like this: roll one die ten times, and maybe you get:<jupyter_code>totals = count_rolls(1,10)
print(totals)<jupyter_output><empty_output><jupyter_text>In case that's not clear... here are the details:<jupyter_code>for i in range(len(totals)):
    print("I rolled ",i,'a total of ',totals[i],'times')<jupyter_output><empty_output><jupyter_text>Let's roll two dice 100,000 times:<jupyter_code>count_rolls(2, 100000)<jupyter_output><empty_output><jupyter_text>Or, try rolling 4 dice 100,000 times:<jupyter_code>count_rolls(4, 100000)<jupyter_output><empty_output><jupyter_text>When examining these results, two things pop out right away:1. the first entries are zero, because you can't roll less than the number of dice2. at first glance, the numbers are hard to understandIn machine learning, a **first glance** is worth a lot... visualization is important when looking for relationships in data.So let's look at a graph instead of a bunch of numbers:<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt        # this gizmo draws graphs (first line is special magic to make it work)
plt.plot(count_rolls(2,1000000),'rs')   # just provide some values...

# see https://matplotlib.org/tutorials/introductory/pyplot.html<jupyter_output><empty_output><jupyter_text>That's better. Looks like the peak is at 7, which makes sense, if you look at every possible combination:<jupyter_code>print("Here are the possible rolls using two 6-sided dice:")
for first_die in range(1,7):                 # first die is 1,2,3,4,5,6
    row = []
    for second_die in range(1,7):            # second die is 1,2,3,4,5,6
        both_dice = first_die + second_die
        row.append(both_dice)                # both dice are sum of first & second
    print(row)<jupyter_output><empty_output><jupyter_text>For four dice:<jupyter_code>plt.plot(count_rolls(4,100000),'bx')<jupyter_output><empty_output><jupyter_text>Interesting. The first example looks more like straight lines, but this one looks curved. Let's connect the dots:<jupyter_code>plt.plot(count_rolls(4,100000),'bx-')<jupyter_output><empty_output><jupyter_text>Sure **seems** curved. Kind of makes you want to try 10 dice and roll a million times (this might take a minute)...<jupyter_code>plt.plot(count_rolls(10,1000000),'go-')<jupyter_output><empty_output><jupyter_text>Yup. Curved. Wouldn't see that in a table of 60 numbers that add up to a million....now that we are seeing things, let's get back to the dice game. ***Exercises***Let's spend some time learning how to plot functions. We'll go back to rolling dice in the next lesson.see https://matplotlib.org/tutorials/introductory/pyplot.html**Write programs to plot these functions:**$f(x)=x^2-3x-4$$f(x)=x^3-8x$$f(x)=\frac{1}{1+e^{-x}}$<jupyter_code># hint... you can make a function, then evaluate it, like this:
def f(x):
    return x**2     # f(x) = x^2

y = [f(x) for x in range(-10,10)]  # this is a 'list comprehension'
                                   # see https://docs.python.org/3/tutorial/datastructures.html
print(y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1001914486279515, 'fraction_numerical': 0.025739204424590514, 'mean_word_length': 3.9235602094240836, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '611479', 'license': 'MIT', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1498, 'n_words': 516, 'path': 'Machine Learning, 04. Seeing Things.ipynb', 'repo_name': 'junior-ai-intro/guess-learn-jrnlyras19', 'size': '4701'}