<p>Just try returning a function that takes the state as an input and returns the probabilities for each action in the form of a numpy array of length of the action space (set of possible actions). Here, is one attempt:</p>
<pre><code>def EpsilonGreedyPolicy(Q, epsilon, no_of_actions):

    def policy(state): 
        probabilities = np.ones(no_of_actions, dtype = float) * 
                    epsilon / num_actions 
        best_action = np.argmax(Q[state]) 
        probabilities[best_action] += (1.0 - epsilon) 
        return probabilities 
   
    return policy
</code></pre>

CC BY-SA 4.0
2020-07-29T16:01:12.840Z
2020-07-29T16:20:27.873Z
2020-07-29T16:20:27.873Z
Rithik Banerjee
<p>I want to implement this function on a voice searching application:</p>
<p><span class="math-container">$$
Q(S, A) \leftarrow Q(S, A)+\alpha\left(R+\gamma Q\left(S^{\prime}, A^{\prime}\right)-Q(S, A)\right)
$$</span></p>
<p>And also restricted to use epsilon-greedy policy based on a given Q-function and epsilon. I simply need a <span class="math-container">$\epsilon$</span>-greedy policy for updating my q-table.</p>

CC BY-SA 4.0
2020-07-29T16:00:15.287Z
At-least show what have you tried?
Rithik Banerjee
<q-learning>
How can I update my Q-table in Python?
mogoja
# How can I update my Q-table in Python?

(Asked by: username_0 on 2020-07-29)

I want to implement this function on a voice searching application:

$$
Q(S, A) \leftarrow Q(S, A)+\alpha\left(R+\gamma Q\left(S^{\prime}, A^{\prime}\right)-Q(S, A)\right)
$$

And also restricted to use epsilon-greedy policy based on a given Q-function and epsilon. I simply need a $\epsilon$-greedy policy for updating my q-table.

**Tags:** q-learning

> **Comments:**
> **username_1**: At-least show what have you tried?  

---

## 1 answers

### Answer by username_1 on 2020-07-29. Score: 2 | Accepted answer

Just try returning a function that takes the state as an input and returns the probabilities for each action in the form of a numpy array of length of the action space (set of possible actions). Here, is one attempt:
def EpsilonGreedyPolicy(Q, epsilon, no_of_actions):

    def policy(state): 
        probabilities = np.ones(no_of_actions, dtype = float) * 
                    epsilon / num_actions 
        best_action = np.argmax(Q[state]) 
        probabilities[best_action] += (1.0 - epsilon) 
        return probabilities 
   
    return policy


---

