def predict(self, dataset_instance): self.compile() new_dataset_instance = dataset_instance.copy() for processing in self.pre_processing: # pre_processing if processing[0] == "add_const": new_dataset_instance.RHS.add_const(processing[1]) elif processing[0] == "linear_standard": rhs_matrix = new_dataset_instance.get_RHS() std = processing[1] mean = processing[2] rhs_matrix = (rhs_matrix - mean) / std new_dataset_instance.set_RHS(rhs_matrix) object_to_analyze = to_analyze( new_dataset_instance.get_solutions(), self.model.predict(new_dataset_instance.get_RHS()).flatten()) for processing in self.pre_processing[::-1]: # post_processing if processing[0] == "linear_mean" or processing[0] == "linear_max": object_to_analyze.untransform_predictions_linear( processing[1], processing[2]) elif processing[0] == "linear_standard": std = processing[3] mean = processing[4] object_to_analyze.untransform_predictions_linear( 1 / std, -mean / std) object_to_analyze.add_used_nn(self) return object_to_analyze
def evaluate(self, dataset_instance): """ Evaluates the network with the dataset. Arguments : class dataset Out : class to_analyze""" raise(Exception("modify this method, predict is different")) history = self.model.evaluate(dataset_instance.get_RHS(), dataset_instance.get_solutions()) object_to_analyze = to_analyze(dataset_instance.get_solutions(), self.model.predict(dataset_instance.get_RHS())) object_to_analyze.add_learning_history(history) object_to_analyze.add_used_nn(self) return object_to_analyze
def predict(self): sol = self.dataset.get_solutions() sol_inv = 1 / sol # list_J = [] list_err_rel = [] print("begin") print(sol) for i in range(self.nb_iteration): x_theta = self.data.dot(self.theta) vect = sol - x_theta absvect = abs(vect) err_rel = 1 / len(self.data) * sol_inv.dot(np.transpose(absvect)) list_err_rel.append(err_rel) if (err_rel < self.min_prec): break delta_J = -2 * (np.transpose(self.data)).dot(np.transpose(vect)) # step = max(1/abs(delta_J).max(),100) step = 2 theta0 = self.theta - step * delta_J x_theta = self.data.dot(theta0) # print("sol: ", sol) # print("x_theta: ", x_theta) # print("delta_J: ", delta_J) # print("self.data: ", np.transpose(self.data)) # print("vect: ", vect) vect = sol - x_theta J0 = 1 / len(self.data) * vect.dot(np.transpose(vect)) for j in range(40): step = step / 2 theta = self.theta - step * delta_J x_theta = self.data.dot(theta) vect = sol - x_theta J = 1 / len(self.data) * vect.dot(np.transpose(vect)) if J > J0: print("J0: ", J0) print("step: ", step) break J0 = J theta0 = theta self.theta = theta0 print("coef : ----------------------", self.theta) for i in range(len(list_err_rel)): list_err_rel[i] = math.log10(list_err_rel[i]) plt.plot(range(len(list_err_rel)), list_err_rel, 'r--', label='error') plt.title('With gradient descent linear regression') plt.xlabel('Iteration') plt.ylabel('Log of mean absolute relative error') plt.legend() plt.show() object_to_analyze = to_analyze(sol, self.data.dot(self.theta)) return object_to_analyze
def sol_exact(self): sol = self.dataset.get_solutions() self.theta = np.linalg.inv( (np.transpose(self.data).dot(self.data))).dot( np.transpose(self.data)).dot(sol) error = sol - self.data.dot(self.theta) print("coef : ----------------------", self.theta) print("error_systematique: ", error) print(type(error[1])) object_to_analyze = to_analyze(sol, self.data.dot(self.theta)) return object_to_analyze
10000, 0.1, [25], path="") problem_set_for_evaluation = problem_set_for_training.cut(0.2) x = problem_set_for_training.get_RHS() y = problem_set_for_training.get_solutions() # Fit regression model model2 = DecisionTreeRegressor(max_depth=20) model3 = linear_model.LinearRegression() model2.fit(x, y) model3.fit(x, y) # Predict X_test = problem_set_for_evaluation.get_RHS() y = problem_set_for_evaluation.get_solutions() # print(X_test) y_2 = model2.predict(X_test) y_3 = model3.predict(X_test) #create to anlalyze from package_name.analyse import to_analyze analyze_data = to_analyze(y, y_2) hist = analyze_data.precision_histogram("") plt.show() #compare to linearRegression linearReg_data = to_analyze(y, y_3) hist = linearReg_data.precision_histogram("") plt.show()