def plot_predictions(self, X, Y, col_of_X=1): Xsp = [row[col_of_X] for row in self.X] Xtp = [row[col_of_X] for row in X] BP(Xtp, self.Yp, xp=Xsp, yp=self.Y, t='Model Predictions vs. Inputs', x_t='Inputs', y_t='Predictions and Original Output')
def plot_predictions(self, X, Y, col_of_X=1): if type(X) is not np.ndarray: X = np.asarray(X) Xsp = self.X[:, col_of_X].tolist() Xtp = X[:, col_of_X].tolist() BP(Xtp, self.Yp, xp=Xsp, yp=self.Y, t='Model Predictions vs. Inputs', x_t='Inputs', y_t='Predictions and Original Output')
def plot_solution_convergence(self): BP(self.cnt_list, self.cost_list, t='Cost vs. Solution Steps', x_t='Solution Steps', y_t='Cost')
from Plot_Tools import Basic_Plot as BP import random import sys # Section 1: Fake Training Data Preparation Xp = [2, 2, 4, 4] X = [[1, x] for x in Xp] Y = [2.1, 1.9, 3.1, 2.9] BP(xp=Xp, yp=Y, t='Initial Training Points for X and Y') # Section 2: Solution of Model Parameters / Weights b = 0.01 # initial value for y-intercept m = 0.025 # initial value for slope Yp = [0, 0, 0, 0] LR = 0.001 cost_list = [] b_list = [] m_list = [] num_pts = len(X) for it in range(100000): for i in range(num_pts): # Prediction based on current weights Yp[i] = X[i][0] * b + X[i][1] * m # Error based on current weights delta = Yp[i] - Y[i] cost = delta**2.0 # The gradients for y-intercept and slope
Xp = [2, 2, 4, 4] X = [[1, x] for x in Xp] Y = [2.1, 1.9, 3.1, 2.9] ws = [random.random()] * len(X[0]) LR = 0.01 # Section 2: Solve / train ws, cost_list, delta, cnt, cnt_list, w_list = \ solver(X, ws, Y, LR, 1) # Section 3: Report training results print(f'Delta = {delta}, Count = {cnt}') ws = [round(x, 6) for x in ws] print(f'Solved Weights: {ws}') BP(cnt_list, cost_list, t='cost vs. Step', x_t='Step', y_t='cost') # Plot learning path of weights for i in range(len(ws)): wi_list = [pair[i] for pair in w_list] title = f'Cost vs Weight {i}' x_t = f'Weight {i}' y_t = 'Cost' BP(wi_list, cost_list, t=title, x_t=x_t, y_t=y_t) # Section 4: Fake test data creation and predictions Xtp = [x / 20.0 for x in list(range(100))] Xtc = [[1, x] for x in Xtp] YtP = model(Xtc, ws) BP(Xtp,
radius = 0.1 gravity = 9.81 f_d = 0.1 theta = np.pi / 3.0 theta_list = [theta] theta_d = 0.0 time = 0.0 delta_t = 0.01 time_list = [0.0] count = 0.0 while count < 10000: theta_dd = gravity / radius * np.sin(theta) \ - f_d * theta_d ** 2.0 theta_d += theta_dd * delta_t theta += theta_d * delta_t theta_list.append(theta) time += delta_t time_list.append(time) count += 1 BP(x=time_list, y=theta_list, t='Theta vs. Time', x_t='Time Values', y_t='Theta Values')