From 13291abb4cff56beebe5e6f906fdbb31caf91985 Mon Sep 17 00:00:00 2001 From: Vayun-Goel Date: Tue, 10 Oct 2023 10:30:55 +0530 Subject: [PATCH 1/4] Adding linear regression 3d visualizer --- .../Linear_regression_3d_visualizer.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py diff --git a/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py b/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py new file mode 100644 index 0000000..f7c2490 --- /dev/null +++ b/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py @@ -0,0 +1,70 @@ +import sys + +sys.path.append('/Users/vayungoel/Desktop/Code-Forge/ML_Algorithms') + +from Linear_Regression.linear_regression import LinearRegression2D + +import numpy as np +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +n = int(input("Enter 1 for generating equation of a plane without noise\nEnter 2 for generating equation of a plane with gaussian noise\nEnter 3 for generating equation of a non-linear curve\n")) + +num_points = 500 + +x1 = np.random.rand(num_points) * 10 +x2 = np.random.rand(num_points) * 10 + +if(n<=2): + + a = 1.0 + b = 2.0 + c = 4.0 + y = a * x1 + b * x2 + c + +if(n==2): + noise = np.random.normal(0, 2, num_points) + y = (y + noise) + +if(n==3): + a = 1.0 + b = 2.0 + c = 0.5 + d = 0.02 + e = 0.01 + f = 2.0 + y = a * x1 + b * x2 + (x1*x2*c) + (x1*x2*x2*d) + (x1**4)*e + f + +points = [[0 for i in range(3)] for j in range(500)] + +for i in range(500): + points[i][0]=x1[i] + points[i][1]=x2[i] + points[i][2]=y[i] + +if(n<=2): + a = LinearRegression2D(points,1) +else: + a = LinearRegression2D(points,2) + +t = a.visualizer() + +points = np.array(points) + +x_surf = np.linspace(0, 10, 100) +y_surf = np.linspace(0, 10, 100) +X, Y = np.meshgrid(x_surf, y_surf) + +print(t) + +for i in range(len(t)): + fig = plt.figure() + ax = fig.add_subplot(111, projection='3d') + Z = (float(t[i][0])* X + float(t[i][1]) * Y + float(t[i][2])) + ax.scatter(x1, x2, y, c='b', marker='o') + ax.plot_surface(X, Y, Z, alpha=0.5, rstride=100, cstride=100, cmap='viridis') + ax.set_xlabel('X') + ax.set_ylabel('Y') + ax.set_zlabel('Z') + + plt.show() \ No newline at end of file From b0eb263c952323ed8b1175c796f6ba281f475e75 Mon Sep 17 00:00:00 2001 From: Vayun-Goel Date: Tue, 10 Oct 2023 10:38:27 +0530 Subject: [PATCH 2/4] Adding linear regression in 2d --- .../Linear_Regression/linear_regression.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 ML_Algorithms/Linear_Regression/linear_regression.py diff --git a/ML_Algorithms/Linear_Regression/linear_regression.py b/ML_Algorithms/Linear_Regression/linear_regression.py new file mode 100644 index 0000000..5509c1a --- /dev/null +++ b/ML_Algorithms/Linear_Regression/linear_regression.py @@ -0,0 +1,62 @@ +import math +import numpy as np + +class LinearRegression2D(): + + def __init__(self,points,per_graph_epochs): + self.points=points + self.per_graph_epochs = per_graph_epochs + + def dot_product(self,x,w,b): + z = 0.0 + z = z + float(x[0])*float(w[0]) + z = z + float(x[1])*float(w[1]) + z = z + float(b) + return float(z) + + def loss_function(self,w,b): + mse = 0.0 + for i in range(len(self.points)): + z = self.dot_product(self.points[i],w,b) + mse = mse + math.pow((self.points[i][0]-z),2) + mse = (mse / float(len(self.points))) + return mse + + def grad_descent(self,epochs,w,b): + for i in range(epochs): + del_w = [0.0 , 0.0] + del_b = 0.0 + m = len(self.points) + for j in range(m): + h = self.dot_product(self.points[j],w,b) + del_w[0] = del_w[0] + (float(self.points[j][0])*(float(h) - float(self.points[j][2]))) + del_w[1] = del_w[1] + (float(self.points[j][1])*(float(h) - float(self.points[j][2]))) + del_b = del_b + (float(h) - float(self.points[j][2])) + del_w[0] = del_w[0] / float(m) + del_w[1] = del_w[1] / float(m) + del_b = del_b / float(m) + print(del_w) + print(del_b) + w[0] = w[0] - 0.005*float(del_w[0]) + w[1] = w[1] - 0.005*float(del_w[1]) + b = b - 0.05*float(del_b) + return w, b + + def visualizer(self): + w = [np.random.randn() for i in range(len(self.points[0])-1)] + b = np.random.randn() + # print(w,b) + arr = [[0 for i in range(len(w)+1)] for j in range(7)] + for i in range(len(w)): + arr[0][i] = w[i] + arr[0][2] = b + for i in range(5): + w,b = self.grad_descent(int(self.per_graph_epochs),w,float(b)) + arr[i+1][0] = w[0] + arr[i+1][1] = w[1] + arr[i+1][2] = b + w,b = self.grad_descent(2000,w,b) + arr[6][0] = w[0] + arr[6][1] = w[1] + arr[6][2] = b + return arr From 2beb8624da704921bc61c2ff609408f2cbd50812 Mon Sep 17 00:00:00 2001 From: Vayun-Goel <120119682+Vayun-Goel@users.noreply.github.com> Date: Tue, 10 Oct 2023 23:38:21 +0530 Subject: [PATCH 3/4] Rename linear_regression.py to linear_regression_2d.py --- .../{linear_regression.py => linear_regression_2d.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ML_Algorithms/Linear_Regression/{linear_regression.py => linear_regression_2d.py} (100%) diff --git a/ML_Algorithms/Linear_Regression/linear_regression.py b/ML_Algorithms/Linear_Regression/linear_regression_2d.py similarity index 100% rename from ML_Algorithms/Linear_Regression/linear_regression.py rename to ML_Algorithms/Linear_Regression/linear_regression_2d.py From 22dbdec2a7fa98963798bbbd53238977a13fd16b Mon Sep 17 00:00:00 2001 From: Vayun-Goel <120119682+Vayun-Goel@users.noreply.github.com> Date: Tue, 10 Oct 2023 23:51:02 +0530 Subject: [PATCH 4/4] Delete Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py removed from this commit --- .../Linear_regression_3d_visualizer.py | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py diff --git a/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py b/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py deleted file mode 100644 index f7c2490..0000000 --- a/Applications/ML_Algorithms/Linear_Regression/Linear_regression_3d_visualizer.py +++ /dev/null @@ -1,70 +0,0 @@ -import sys - -sys.path.append('/Users/vayungoel/Desktop/Code-Forge/ML_Algorithms') - -from Linear_Regression.linear_regression import LinearRegression2D - -import numpy as np -import matplotlib.pyplot as plt -from mpl_toolkits.mplot3d import Axes3D - -n = int(input("Enter 1 for generating equation of a plane without noise\nEnter 2 for generating equation of a plane with gaussian noise\nEnter 3 for generating equation of a non-linear curve\n")) - -num_points = 500 - -x1 = np.random.rand(num_points) * 10 -x2 = np.random.rand(num_points) * 10 - -if(n<=2): - - a = 1.0 - b = 2.0 - c = 4.0 - y = a * x1 + b * x2 + c - -if(n==2): - noise = np.random.normal(0, 2, num_points) - y = (y + noise) - -if(n==3): - a = 1.0 - b = 2.0 - c = 0.5 - d = 0.02 - e = 0.01 - f = 2.0 - y = a * x1 + b * x2 + (x1*x2*c) + (x1*x2*x2*d) + (x1**4)*e + f - -points = [[0 for i in range(3)] for j in range(500)] - -for i in range(500): - points[i][0]=x1[i] - points[i][1]=x2[i] - points[i][2]=y[i] - -if(n<=2): - a = LinearRegression2D(points,1) -else: - a = LinearRegression2D(points,2) - -t = a.visualizer() - -points = np.array(points) - -x_surf = np.linspace(0, 10, 100) -y_surf = np.linspace(0, 10, 100) -X, Y = np.meshgrid(x_surf, y_surf) - -print(t) - -for i in range(len(t)): - fig = plt.figure() - ax = fig.add_subplot(111, projection='3d') - Z = (float(t[i][0])* X + float(t[i][1]) * Y + float(t[i][2])) - ax.scatter(x1, x2, y, c='b', marker='o') - ax.plot_surface(X, Y, Z, alpha=0.5, rstride=100, cstride=100, cmap='viridis') - ax.set_xlabel('X') - ax.set_ylabel('Y') - ax.set_zlabel('Z') - - plt.show() \ No newline at end of file