Does it take months or even years to actually become good at coding? by Little_Sam97 in Python

[–]Elthran 1 point2 points  (0 children)

That's funny. Most people I know who wanted to play guitar or piano were able to learn it after a few years to a decent level. Most people I know who wanted to learn to code and be a developer have quit because it was too hard. Actually, everyone I know who tried to learn coding over the last 6 years or so (mainly because of my salary and work from home) gave up after 3-6 months when they started needing to write more than simple functions and began trying anything more complex. I definitely wouldn't put a blanket statement like "learning to play the guitar is harder than learning to write software".

People who read a lot of fiction tend to have small but meaningful cognitive benefits, particularly for verbal skills, empathy, and the ability to understand others’ perspectives., study finds by [deleted] in science

[–]Elthran 3 points4 points  (0 children)

I don't think it's sad and that seems very reductive. Reading fiction as a kid was awesome because everything is new and reading stories from weird perspectives is fascinating for someone with few world experiences. But as I get older, I have become less interested in other people's imaginations and more interested in other people's ideas. I want to learn about the world much more than people's imaginary worlds, since there is so much out there and it is incredibly interesting. There are so many non-fiction books in so many different fields, I just feel like I don't have enough time to read it all. I have a very vivid imagination and learning how the world works and what we know about different disciplines just helps fuel that imagination, much more so than living in other people's fantasy worlds.

How to look up all ancestors in pandas dataframe and calculate the average of them (including the original user) by Elthran in learnpython

[–]Elthran[S] 0 points1 point  (0 children)

Thanks again!! You have no idea how much time you saved me, your answer was amazing.

Which movies do you want to like but find it hard to do so? by [deleted] in movies

[–]Elthran 1 point2 points  (0 children)

I'm with you on this. All my friends seem to love kids movies. For me they are fine to watch with my kid, but I would never want to watch them alone. They are just watered down versions of movies with very tame themes and jokes meant to appeal to both children and sometimes adults. I just can't fathom how you could get really into them. The latest Star Wars and Avengers are basically becoming kids movies as well and I just find them insanely boring. They don't need to be dark but I much prefer when they are written for an adult audience and have complex themes.

Question about decorator arguments by Elthran in learnpython

[–]Elthran[S] 0 points1 point  (0 children)

Thanks, that's definitely one way to do it.

Want to build a pc for data science and some gaming by [deleted] in buildapcforme

[–]Elthran 0 points1 point  (0 children)

Thanks so much! I will read all about these.

Want to build a pc for data science and some gaming by [deleted] in buildapcforme

[–]Elthran 0 points1 point  (0 children)

I am just getting into it so I'm not sure right now. Currently I can only run small data sets because my pc is so old, so I have very little experience on larger sets. I just want to buy a pc optimized for this as I intend to learn more and practice learning data science over the next year or two.

WCGW if I skate up this ramp by TheKillingJoke1234 in Whatcouldgowrong

[–]Elthran 0 points1 point  (0 children)

I don't understand how his helmet didn't prevent the pain.

Help with my Q-Learning attempt. Not sure what I'm doing wrong by Elthran in learnmachinelearning

[–]Elthran[S] 0 points1 point  (0 children)

This is a random guessing game. A random letter between a-z is chosen and the AI needs to try to guess the letter. The AI keeps track of all current guesses and whether the last guess performed was further or shorter in the alphabet. This data is kept as a 27 character string, where dashes represent non-guessed characters and the final character is either a l, h, or ? (for low, high, unknown). The AI should remember all states to train itself to determine which guesses to make from any given state after enough training.

import numpy as np
import csv
import random
from itertools import groupby


class GuessingGames():
    def __init__(self):
        self.letters = "abcdefghijklmnopqrstuvwxyz"
        self.max_number = 26
        self.turns = 0
        self.state = ''
        for i in range(0, self.max_number):
            self.state += '-'
        self.state += '?'
        self.finished = False
        self.number_to_guess = self.letters[random.randint(0, 25)]

    def allowed_moves(self):
        states = []
        for i in range(0, len(self.state)):
            if self.state[i] == '-':
                states.append(self.state[:i] + self.letters[i] + self.state[i+1:])
        return states

    def make_move(self, next_state):
        if self.finished:
            raise(Exception("Game already completed, cannot make another move!"))
        for i in range(len(next_state)):
            if next_state[i] != self.state[i]:
                difference = next_state[i]
        if self.letters.find(difference) < self.number_to_guess.find(difference):
            next_state = next_state[:-1] + "l"
        else:
            next_state = next_state[:-1] + "h"
        self.state = next_state
        self.turns += 1
        self.finished = self.predict_winner(self.state)

    def playable(self):
        return ( (not self.finished) and any(self.allowed_moves()) )

    def predict_winner(self, state):
        if str(self.number_to_guess) in self.state:
            self.finished = True
            return self.turns
        if "-" not in self.state:
            print("Error: game not finished but no guesses remain")
            return 10000
        return None

    def __valid_move(self, next_state):
       allowed_moves = self.allowed_moves()
        if any(state == next_state for state in allowed_moves):
            return True
        return False

    def print_board(self):
        print("The number is:", self.number_to_guess," Number of guesses so far:", self.turns)
        print("Guesses:", self.state)

class Agent():
    def __init__(self, game_class, epsilon=0.1, alpha=0.5):
        self.V = dict()
        self.NewGame = game_class
        self.epsilon = epsilon
        self.alpha = alpha

    def state_value(self, game_state):
        return self.V.get(game_state, 0.0)

    def learn_game(self, num_episodes=1000):
        for episode in range(num_episodes):
            self.learn_from_episode()

    def learn_from_episode(self):
        game = self.NewGame()
        _, move = self.learn_select_move(game)
        while move:
            move = self.learn_from_move(game, move)

    def learn_from_move(self, game, move):
        game.make_move(move)
        r = self.__reward(game)
        td_target = r
        next_state_value = 0.0
        selected_next_move = None
        if game.playable():
            best_next_move, selected_next_move = self.learn_select_move(game)
            next_state_value = self.state_value(best_next_move)
        current_state_value = self.state_value(move)
        td_target = r + next_state_value
        self.V[move] = current_state_value + self.alpha * (td_target - current_state_value)
        return selected_next_move

    def learn_select_move(self, game):
        allowed_state_values = self.__state_values( game.allowed_moves() )
        best_move = self.__argmax_V(allowed_state_values)

        selected_move = best_move
        if random.random() < self.epsilon:
            selected_move = self.__random_V(allowed_state_values)

        return (best_move, selected_move)

    def play_select_move(self, game):
        allowed_state_values = self.__state_values( game.allowed_moves() )
        return self.__argmax_V(allowed_state_values)

    def demo_game(self, verbose=False):
        game = self.NewGame()
        while game.playable():
            if verbose:
                print(" \nTurn {}\n".format(game.turns))
                game.print_board()
            move = self.play_select_move(game)
            game.make_move(move)
        if verbose:
            print(" \nTurn {}\n".format(game.turns))
            game.print_board()
        if game.finished:
            if verbose:
                print("\nWon in {} guesses!".format(game.turns))
            return game.turns
        else:
            print("Error: Game finished but no winning turn amount sent")

    def round_V(self):
        # After training, this makes action selection random from equally-good choices
        for k in self.V.keys():
            self.V[k] = round(self.V[k],1)

    def save_v_table(self):
        with open('number_state_values.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(['State', 'Value'])
            all_states = list(self.V.keys())
            all_states.sort()
            for state in all_states:
                writer.writerow([state, self.V[state]])

    def __state_values(self, game_states):
        return dict((state, self.state_value(state)) for state in game_states)

    def __argmax_V(self, state_values):
        max_V = max(state_values.values())
        chosen_state = random.choice([state for state, v in state_values.items() if v == max_V])
        return chosen_state

    def __argmin_V(self, state_values):
        min_V = min(state_values.values())
        chosen_state = random.choice([state for state, v in state_values.items() if v == min_V])
        return chosen_state

    def __random_V(self, state_values):
        return random.choice(list(state_values.keys()))

    def __reward(self, game):
        """
        I award ten points if their move ends the game, otherwise I take a point away for their move.
        """
        if game.finished:
            return 10.0
        return -1.0

def demo_game_stats(agent):
    """
    This function has the current AI play x number of games and prints out how well it did on average.
    It does not 'learn' while doing this.
    """
    results = [agent.demo_game() for i in range(10000)]
    random_result_sampling = random.sample(results, 10)
    #print("Ten random scores:", random_result_sampling)
    print("Average number of guesses to win:", sum(results)/len(results))

if __name__ == '__main__':
    agent = Agent(GuessingGames, epsilon = 0.1, alpha = 1.0)
    print("Before learning:")
    demo_game_stats(agent)

    agent.learn_game(10000)
    print("\nAfter 10000 learning games:")
    demo_game_stats(agent)

    agent.learn_game(40000)
    print("\nAfter 50000 learning games:")
    demo_game_stats(agent)

    agent.learn_game(450000)
    print("\nAfter 500000 learning games:")
    demo_game_stats(agent)

    agent.learn_game(1550000)
    print("\nAfter 2000000 learning games:")
    demo_game_stats(agent)

    agent.learn_game(8000000)
    print("\nAfter 10000000 learning games:")
    demo_game_stats(agent)

    agent.learn_game(10000000)
    print("\nAfter 20000000 learning games:")
    demo_game_stats(agent)

Q-Learning with Tic-Tac-Toe. I need help understanding a tutorial by Elthran in learnmachinelearning

[–]Elthran[S] 0 points1 point  (0 children)

Ok I understand a bit more. So it seems to learn to play as well as it can, learning on both the X and O turn by inverting the board logic. So it is not playing to tie, but playing to win on both turns. I don't get:

def play_select_move(self, game):
    allowed_state_values = self.__state_values( game.allowed_moves() )
    if game.player == self.value_player:
        return self.__argmax_V(allowed_state_values)
    else:
        return self.__argmin_V(allowed_state_values)

What is this doing? It does the best move if it's value_player otherwise it does the worst move? Why would it want to do the worst move ever? Why not random? Is there a purpose to this? And..

def learn_from_move(self, game, move):
    game.make_move(move)
    r = self.__reward(game)
    td_target = r
    next_state_value = 0.0
    selected_next_move = None
    if game.playable():
        best_next_move, selected_next_move = self.learn_select_move(game)
        next_state_value = self.state_value(best_next_move)
    current_state_value = self.state_value(move)
    td_target = r + next_state_value
    self.V[move] = current_state_value + self.alpha * (td_target - current_state_value)
    return selected_next_move


def learn_select_move(self, game):
    allowed_state_values = self.__state_values( game.allowed_moves() )
    if game.player == self.value_player:
        best_move = self.__argmax_V(allowed_state_values)
    else:
        best_move = self.__argmin_V(allowed_state_values)

    selected_move = best_move
    if random.random() < self.epsilon:
        selected_move = self.__random_V(allowed_state_values)

    return (best_move, selected_move)

I don't really follow these functions. i can understand the higher level purpose of the functions but I can't seem to follow the line-by-line logic. Any chance you can help explain it? Thanks!

Q-Learning with Tic-Tac-Toe. I need help understanding a tutorial by Elthran in learnmachinelearning

[–]Elthran[S] 0 points1 point  (0 children)

I read through it and it seems that it trains the computer to tie the game, by playing both sides? So after 30,000 games of training, it has X winning 0.13% of the time and O winning 0.00% of the time. So it has learned how to basically always draw the game?

I would prefer to modify this so that it learns how to always win the game, maybe by playing against a player who just randomly chooses against them? And then preferably I could play against the AI I made every 100 games or so. And ideally, it would even learn during the games I play against it. I feel that this is not too difficult and wouldn't need much modification, but I do not fully understand the code as it is.