import numpy as np import matplotlib.pyplot as plt # Initialize variables ws = [] gamma = 0.01 R = 1 # Generate random points within the circle of radius R points = [] while len(points) < 100: x, y = np.random.uniform(-R, R, 2) if x**2 + y**2 <= R**2: points.append((x, y)) # Remove points with x < gamma points = [point for point in points if point[0] >= gamma] # Initialize w ws = [] w = np.array([-0.1, 0.0]) ws.append(w.copy()) # Perceptron learning algorithm convergence_flag = False while not convergence_flag: np.random.shuffle(points) convergence_flag = True for v in points: if np.dot(w, v) <= 0: w += v ws.append(w.copy()) convergence_flag = False # Plot the items fig, ax = plt.subplots(figsize=(10,10)) # Vertical dotted line at x=gamma ax.axvline(x=gamma, linestyle='--', color='r', label='x=gamma') # Circle with radius R centered at (0, 0) circle = plt.Circle((0, 0), R, fill=False, color='b', linestyle='-', label='Circle') ax.add_artist(circle) # Jagged line connecting entries in ws points = np.array(points) ax.scatter(points[:,0], points[:,1]) ws = np.array(ws) for i in range(1, ws.shape[0]): ax.arrow(ws[i-1, 0], ws[i-1, 1], ws[i, 0] - ws[i-1, 0], ws[i, 1] - ws[i-1, 1], color='g', linewidth=0.5, head_width=0.03, head_length=0.04, length_includes_head=True) # Set axis limits # ax.set_xlim(-R, R) # ax.set_ylim(-R, R) ax.set_xlabel('X') ax.set_ylabel('Y') ax.legend() plt.gca().set_aspect('equal') plt.savefig("perceptron convergence.svg")