def train_with_normal_equation(self, feature_data, labels_data, normalized_feature_data, normalized_labels): print("Normal Equation: ") # Normal equation feature_data_bias = np.insert(normalized_feature_data, 0, 1, axis=1) x_trans_x = np.dot(np.transpose(feature_data_bias), feature_data_bias) norm_equation_theta = np.dot( np.dot(np.linalg.inv(x_trans_x), np.transpose(feature_data_bias)), normalized_labels) print(norm_equation_theta) self.linear_regression_learner.theta = norm_equation_theta normalized_predictions = self.linear_regression_learner.predict( normalized_feature_data) predictions = np.around( DataService.denormalize_predictions(labels_data, normalized_predictions, method='min-max')) cost = self.linear_regression_learner.calculate_cost( normalized_predictions, normalized_labels) print("Final Norm Equation Normalized Cost: ", cost) x, y, z = self.build_model_plot_data(feature_data, predictions) PlotService.plot3d_line( x, y, z, labels=['Age', 'Weight', 'BP'], title="Normal Equation Model: Blood Pressure for Age and Weight.") PlotService.plot3d_scatter_compare( feature_data, labels_data, predictions, labels=['Age', 'Weight', 'BP'], title="Normal Equation Actual vs Projected")
def train_with_gradient_descent(self, feature_data, labels_data, normalized_feature_data, normalized_labels): self.linear_regression_learner.train(normalized_feature_data, normalized_labels) normalized_predictions = self.linear_regression_learner.predict( normalized_feature_data) predictions = np.around( DataService.denormalize_predictions(labels_data, normalized_predictions, method='min-max')) PlotService.plot_line(x=range( 1, len(self.linear_regression_learner.cost_history) + 1), y=self.linear_regression_learner.cost_history, x_label="Iteration", y_label="Training Cost", title="Training Learning Curve") x, y, z = self.build_model_plot_data(feature_data, predictions) PlotService.plot3d_line( x, y, z, labels=['Age', 'Weight', 'BP'], title="Linear Model: Blood Pressure for Age and Weight.") PlotService.plot3d_scatter_compare(feature_data, labels_data, predictions, labels=['Age', 'Weight', 'BP'], title="Actual vs Projected") cost = self.linear_regression_learner.calculate_cost( normalized_predictions, normalized_labels) print("Final Normalized Cost: ", cost)