diff --git a/Energy_prediction.ipynb b/Energy_prediction.ipynb index 0eabc5ab938a88433c9ba14c496ddeec34d4e747..ca3f3b4091a03335f5baf5efcc57b21617146b34 100644 --- a/Energy_prediction.ipynb +++ b/Energy_prediction.ipynb @@ -4613,7 +4613,8 @@ "id": "23b0ff00", "metadata": {}, "source": [ - "# Q learning" + "# Q learning\n", + "Refrenced from lab 10 ,11 " ] }, { @@ -4626,7 +4627,7 @@ "# Filter data for a specific country \n", "country_data = df1[df1['Entity'] == 'United States'].sort_values(by='Year')\n", "\n", - "# Define the state as the year and the target as the renewable energy share\n", + "\n", "states = country_data['Year'].values\n", "targets = country_data['Renewable energy share in the total final energy consumption (%)'].values\n", "\n", @@ -4657,16 +4658,16 @@ "source": [ "# Q-learning algorithm\n", "for episode in range(num_episodes):\n", - " state = 0 # Start from the first year\n", + " state = 0 \n", " for t in range(len(states) - 1):\n", " if np.random.uniform(0, 1) < epsilon:\n", - " action = np.random.randint(len(states)) # Explore: select a random action\n", + " action = np.random.randint(len(states)) \n", " else:\n", - " action = np.argmax(q_table[state]) # Exploit: select the action with max Q-value\n", + " action = np.argmax(q_table[state]) \n", "\n", " # Take action and observe the reward\n", " next_state = state + 1 if action == state + 1 else state\n", - " reward = -abs(targets[next_state] - targets[state]) # Reward is the negative absolute difference\n", + " reward = -abs(targets[next_state] - targets[state]) \n", "\n", " # Update Q-table\n", " q_table[state, action] = q_table[state, action] + alpha * (\n", @@ -4698,7 +4699,7 @@ "for action in policy:\n", " predicted_targets.append(targets[action])\n", "\n", - "# Calculate accuracy (here we use mean absolute error for simplicity)\n", + "# Calculate accuracy \n", "mae = np.mean(np.abs(np.array(predicted_targets) - targets))\n", "print(f\"Mean Absolute Error: {mae}\")" ]