Implement the Knn (2)
Implement the Knn (2)
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from collections import Counter
return np.array(predictions)
return y_pred
# Plot results
plt.scatter(X, y, label="Training Data", color="blue", alpha=0.6)
plt.plot(X_query, y_pred, label=f"LWR Prediction (tau={tau})", color="red",
linewidth=2)
plt.xlabel("X")
plt.ylabel("y")
plt.title("Locally Weighted Regression")
plt.legend()
plt.show()
import numpy as np
import random
# Initialize Q-table
Q_table = np.zeros((GRID_SIZE, GRID_SIZE, len(ACTIONS)))
# Q-learning parameters
alpha = 0.1 # Learning rate
gamma = 0.9 # Discount factor
epsilon = 0.2 # Exploration rate
episodes = 200 # Training episodes