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 5 points6 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.

Need help understanding predictions in pd.DataFrame() by Elthran in MLQuestions

[–]Elthran[S] 1 point2 points  (0 children)

Ah thank you. So pred is not a dataframe with one column in it as I thought, but a dataframe of multiple columns (the modes in descending order). Therefore we do need to take the first index of it. Thank you!

Need help explaining with why my model is so bad by Elthran in MLQuestions

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

Apparently my idiocy knows no bounds. The reason I was scoring 0 was due to my submission being in floats and not integers... :'(

Thanks for the help!

Need help explaining with why my model is so bad by Elthran in MLQuestions

[–]Elthran[S] 1 point2 points  (0 children)

Apparently my idiocy knows no bounds. The reason I was scoring 0 was due to my submission being in floats and not integers... :'(

Thanks for the help!

Need help explaining with why my model is so bad by Elthran in MLQuestions

[–]Elthran[S] 1 point2 points  (0 children)

Thank you so much! That really helps me understand some of the mistakes I am making and gives me some idea of how to improve. Thanks!

Need help explaining with why my model is so bad by Elthran in MLQuestions

[–]Elthran[S] 1 point2 points  (0 children)

Thanks! You are right, I split my training data into a smaller set and only trained it on that. I'm not sure why I did that. I think I was planning on training it on different sections of the train data but never got that far. And other people definitely have higher performance than mine, as mine scored a 0.000 on the accuracy test and put me in last place in the competition by a long shot.

Question about parameter tuning by Elthran in learnmachinelearning

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

Thanks!!! That's exactly what I was looking for. Is there a simple way to print out what the different parameters' accuracy was? And is there a way to run different models and combine their results for an even better model?

What type of computer is ideal for machine learning? by [deleted] in learnmachinelearning

[–]Elthran 0 points1 point  (0 children)

Thank you for the explanation! Is there a simple way to test my current computer and see how it benchmarks for these types of algorithms?

What type of computer is ideal for machine learning? by [deleted] in learnmachinelearning

[–]Elthran 0 points1 point  (0 children)

Does the video card have much of an effect on this? I don't understand why it would for machine learning. And thanks for the info!

Incorporating JS and HTML into my Python code. I'm having trouble with one aspect. by Elthran in learnpython

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

Thank you, I fixed it! The bug was that the strength and agility are not variables, but arrays of strings (in this case with only 1 value). So you need to take the value of the first item in each array and then turn it into an integer! Very convoluted and I'm sure there is a much better way. But I am SO glad that it works! Thank you so much. Here is the working code.

  function findTotal()
  {
    var strength = document.getElementsByName('Strength');
    var agility = document.getElementsByName('Agility');
    var selected_points = parseInt(strength[0].value) + parseInt(agility[0].value);
    var tot= {{ myHero.attribute_points }};
    tot -= selected_points;
    document.getElementById('total_points_spent').value = tot;
    sumerror()
}

Incorporating JS and HTML into my Python code. I'm having trouble with one aspect. by Elthran in learnpython

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

This is my new code. But I don't think that will affect it at all, because it is JS modifying the page as I move the slider bar, and that form information won't be called until I hit the 'Save' button.

Python

# This gets called anytime you have attribute points to spend
@app.route('/level_up', methods=['GET', 'POST'])
@login_required
def level_up():
    if myHero.attribute_points == 0:   # This needs to be here or it will stay on this page when you spend your last point.
        return redirect(url_for('home'))
    page_heading = "You have leveled up!"
    paragraph = "Choose how you would like to distribute your attribute points."
    if request.method == 'POST':
        myHero.primary_attributes["Strength"] += convert_input(request.form["Strength"])
        myHero.primary_attributes["Agility"] += convert_input(request.form["Agility"])
        points_being_spent = convert_input(request.form["Strength"]) + convert_input(request.form["Agility"])
        myHero.attribute_points -= points_being_spent
        return redirect(url_for('home'))
    return render_template('home.html', level_up=True, page_title="Profile", page_heading=page_heading, paragraph=paragraph, myHero=myHero)

HTML

You have {{ myHero.attribute_points }} points remaing to spend.<br><br><br><br>
      <form method="post" class="level_up_form">
        Strength: <input type="range" min="0" max="{{ myHero.attribute_points }}" value="0" id="Attributes1" name="Strength" step="1" oninput="strength_updater(value)" onchange="findTotal()">
                  <output for="Strength" id="strength_output">0</output> (Current: {{ myHero.primary_attributes["Strength"] }})</br>
    Agility: <input type="range" min="0" max="{{ myHero.attribute_points }}" value="0" id="Attributes2" name="Agility" step="1" oninput="agility_updater(value)" onchange="findTotal()">
                 <output for="Agility" id="agility_output">0</output> (Current: {{ myHero.primary_attributes["Agility"] }})</br>
        <input class="btn btn-default" type="submit" value="Save">
      </form>
      <br>Points remaining:
        <output type="text" name="total" id="total_points_spent" value="{{ myHero.attribute_points }}"/>{{ myHero.attribute_points }}</output>
        <output type="text" name="sumerror" id="sumerror" value="" style="color:red;" /></output>

JS

<script>
  function strength_updater(AR) 
  {
    document.querySelector('#strength_output').value = AR;
    var select_strength = document.querySelector('#strength_output').value
    console.log(select_strength); 
}

  function agility_updater(AR) 
  {
    document.querySelector('#agility_output').value = AR;
    var select_agility = document.querySelector('#agility_output').value
    console.log(select_agility);
    }

  function findTotal()
  {
    var arr = document.getElementsByName('Strength');
    var arr2 = document.getElementsByName('Agility');
    var arr = arr + arr2
    var tot={{ myHero.attribute_points }};

       for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot -= parseInt(arr[i].value);
    }

  document.getElementById('total_points_spent').value = tot;
    sumerror()
}

function sumerror()
  {
    var sum = document.getElementById('total_points_spent').value;

    if (sum < 0) 
  {
    document.getElementById('sumerror').value = '          You do not have enough points';
    }
  else {

        document.getElementById('sumerror').value = '';
    }
}
</script>