Skip to content
    number-bonds-ML@blogmywiki
    main.py
    # Simple machine learning example for kids by Giles Booth @blogmywiki
    #
    # http://www.suppertime.co.uk/blogmywiki/2021/09/simple-machine-learning-abstraction/


    # using decimals not integers so we can see it improve, thanks @veryalien!
    bonds = [5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0,5.0]
    import random
    human_wins = 0
    machine_wins = 0
    turns = 0

    print('Train a machine to learn its number bonds to 10!')
    print('If I give you a number, you tell me what you add to it to make 10')

    while True:
    number = random.randint(0,9)
    print()
    print(bonds) # added so you can see it improve its data set
    print('Number is ' + str(number))
    print('Machine says the bond to 10 is ' + str(round(bonds[number])))
    if number + round(bonds[number]) == 10:
    print('Machine is right!')
    machine_wins += 1
    else:
    print('Machine is wrong, more training data needed.')
    human_answer = input('Give me your answer 1-10: ')
    if human_answer == 'quit':
    break
    elif human_answer == 'reset':
    human_wins = 0
    machine_wins = 0
    turns = 0
    elif number + int(human_answer) == 10:
    print('Correct!')
    human_wins += 1
    bonds[number] = (bonds[number] + int(human_answer)) / 2
    print('Now machine thinks it is ' + str(round(bonds[number])))
    else:
    print('Wrong! Are you trying to mislead a poor machine?')
    turns += 1
    print('Human score ' + str(human_wins) + ' out of ' + str(turns))
    print('Machine score ' + str(machine_wins) + ' out of ' + str(turns))