tanmay_ml_a2_p1

py

School

New York University *

*We aren’t endorsed by this school

Course

6143

Subject

Electrical Engineering

Date

Feb 20, 2024

Type

py

Pages

4

Uploaded by tanmayr71

Report
# -*- coding: utf-8 -*- """Tanmay_ML_A2_P1.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1t2D2inhqA4aaH7FKf-Rsyv9iq84bn6Bn """ import numpy as np import matplotlib.pyplot as plt """Loading the dataset from the .mat file to Numpy Array""" import scipy.io data3 = scipy.io.loadmat('/content/data3.mat') type(data3) data3.keys() data = data3['data'] """Checking the dataset values and shape of the dataset""" data.shape type(data) """Extracting the features into X And target variable to y """ X = data[:, :-1] X = np.concatenate((np.ones((X.shape[0],1)),X), axis=1) y = data[:, -1].reshape((-1,1)) def stepFn(a): a[a>=0]=1 a[a<0]=-1 return a import math def linear_perceptron_reg(X, y, num_iters=6000, l_rate=1, random_theta=True, show_stats=False): m, n = X.shape iter_i = 0 seed_value = 42 np.random.seed(seed_value) w = np.random.random_sample((n, 1)) if random_theta else np.zeros((n, 1)) perceptron_error_history, classification_error_history = [], [] while iter_i < num_iters: random_i = np.random.randint(m)
output = X[random_i].reshape((1,3)).dot(w) if stepFn(np.multiply(y[random_i],output))[0]==-1: w += l_rate * X[random_i].reshape((3, 1)).dot(y[random_i].reshape((1,1))) y_pred = X.dot(w) classification_error = float(np.count_nonzero(stepFn(y_pred) != y)) classification_error_history.append(classification_error / m) risk = np.sum(stepFn(np.multiply(-y, y_pred))) perceptron_error_history.append(risk / m) if show_stats and (iter_i % math.ceil(num_iters/10) == 0 or iter_i == (num_iters-1)): print(f"Iteration {iter_i:5}: Perceptron Error {float(perceptron_error_history[-1]):8.2f}, Classification Error {float(classification_error_history[-1]):.6f} ") if classification_error_history[-1]==0: if show_stats: print(f'Convergence Reached! Iteration/(Max No of Iter): {iter_i}/{num_iters}') print(f"Iteration {iter_i:5}: Perceptron Error {float(perceptron_error_history[-1]):8.2f}, Classification Error {float(classification_error_history[-1]):.6f} ") break iter_i+=1 return w, perceptron_error_history, classification_error_history def plot_data_and_decision_boundary(w, x, y): plt.figure(0) samples_num = x.shape[0] for i in range(samples_num): if y[i] == 1: plt.plot(x[i, 1], x[i, 2], 'g.', label='Class +1' if i==0 else "") else: plt.plot(x[i, 1], x[i, 2], 'r.', label='Class -1' if i==0 else "") min_x = min(x[:, 1]) max_x = max(x[:, 1]) y_min_x = (-w[0] - w[1] * min_x) / w[2] y_max_x = (-w[0] - w[1] * max_x) / w[2] plt.plot([min_x, max_x], [y_min_x, y_max_x], '-k', label='Decision Boundary') plt.xlabel('Feature X1') plt.ylabel('Feature X2') plt.legend(loc='upper left') plt.show() def plot_classification_and_risk_error_trends(start_iterations, stop_iterations, perceptron_error_history, classification_error_history): plt.figure(1,figsize=(12,8)) plt.plot(range(start_iterations,stop_iterations), classification_error_history,'b-',label='Classification Error') plt.plot(range(start_iterations,stop_iterations), perceptron_error_history, 'y-',label='Perceptron Error/Risk')
plt.xlabel('Iterations') plt.title('Iterations vs Classification Error/Empirical Risk') plt.legend() plt.show() w, J, B= linear_perceptron_reg(X, y, show_stats=True) B[-1] plot_data_and_decision_boundary(w, X, y) plot_classification_and_risk_error_trends(0, len(B), J, B) last_n = 20 if len(B) >= last_n: start_index = len(B) - last_n plot_classification_and_risk_error_trends(start_index, len(B), J[start_index:], B[start_index:]) else: print(f"There are less than {last_n} iterations.") """Convergence Behaviour as we change Step Size (l_rate/ Learning Rate) """ step_sizes = [0.1, 0.01, 0.001, 1, 10] plt.figure(1,figsize=(12,8)) for l_rate in step_sizes: print(f'\nLearning Rate: {l_rate}') w, perceptron_error_history, classification_error_history = linear_perceptron_reg( X, y, num_iters=6000, l_rate=l_rate, random_theta=True, show_stats=True ) plt.plot(classification_error_history, label=f'Step Size: {l_rate}') plt.xlabel('Iterations') plt.ylabel('Classification Error') plt.title('Convergence Behavior with Different Step Sizes') plt.legend() plt.show() """For the very low learning rate (0.001), the perceptron error is fluctuating and not reaching -1 even after 6000 iterations, showing non-convergence.\ Very Low Learning Rate (e.g., 0.001): Hasn’t converged even after 6000 iterations, indicating it might be too slow for practical purposes in this case. """ fig, axs = plt.subplots(len(step_sizes), 1, sharex=True, figsize=(10, 15)) for i, l_rate in enumerate(step_sizes): w, perceptron_error_history, classification_error_history = linear_perceptron_reg( X, y, num_iters=6000, l_rate=l_rate, random_theta=True ) axs[i].plot(classification_error_history, label=f'Step Size: {l_rate}') axs[i].set_ylabel('Classification Error')
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
axs[i].legend() axs[i].set_yscale('log') plt.xlabel('Iterations') plt.suptitle('Convergence Behavior with Different Step Sizes') plt.show() """On the other hand, say for example a higher learning rate 10, it is able to converge quickly but if the learning rate is too high, the updates may be so large that they carry the parameters past the minimum to the other side of the loss curve.\ This can cause the optimization process to oscillate back and forth across the minimum (Visible in the graph for Step size 10), potentially never settling at the minimum point. This can lead to divergence, where the error actually increases with each step instead of decreasing. Higher learning rates lead to faster convergence but might overshoot the minimum, while lower learning rates might converge more accurately but are slower.\ Based on this run, a learning rate of 1 or 0.1 might be suitable for this problem as it offers a good balance, converging to 0 classification error relatively quickly. """ print('Tanmay Anil Rathi\nAssignment 2')