def calculate_alpha_s(self, weak_learner, s): alpha = 0.0 new_s = s data_num = len(weak_learner.train_Y) X = [] for i in range(data_num): predicted_str = np.array(list(map(str, weak_learner.train_X[i]))) predicted_str = ' '.join(predicted_str[1:]) + ' ' + str( weak_learner.train_Y[i]) prediction = weak_learner.prediction(predicted_str, 'test_data') X.append([float(prediction['prediction'])]) X = np.array(X) linear = linear_regression.LinearRegression() linear.status = 'load_train_data' linear.train_X = X linear.train_Y = weak_learner.train_Y - s linear.set_param() linear.init_W() linear.train() alpha = linear.W[0] new_s = s + alpha * np.ravel(X) return alpha, new_s
def init_W(self, mode='normal', decomposition='ova'): self.W = {} if (self.status != 'load_train_data') and (self.status != 'train'): print("Please load train data first.") return self.W self.status = 'init' self.data_num = len(self.train_Y) self.data_demension = len(self.train_X[0]) self.decomposition = decomposition if self.decomposition == 'ovo': self.class_list = list(itertools.combinations(np.unique(self.train_Y), 2)) elif self.decomposition == 'ova': self.class_list = np.unique(self.train_Y) for class_item in self.class_list: self.W[class_item] = np.zeros(self.data_demension) if mode == 'linear_regression_accelerator': accelerator = linear_regression.Accelerator() for class_item in self.class_list: if self.decomposition == 'ovo': modify_X, modify_Y = utility.LoadData.modify_XY(self.train_X, self.train_Y, class_item) self.temp_train_X = self.train_X self.temp_train_Y = self.train_Y self.train_X = modify_X self.train_Y = modify_Y self.temp_data_num = self.data_num self.data_num = len(self.train_Y) self.temp_W = self.W self.W = self.temp_W[class_item] self.temp_W[class_item] = accelerator.init_W(self) self.train_X = self.temp_train_X self.train_Y = self.temp_train_Y self.temp_train_X = [] self.temp_train_Y = [] self.data_num = self.temp_data_num self.temp_data_num = 0 self.W = self.temp_W self.temp_W = {} elif self.decomposition == 'ova': modify_Y = utility.LoadData.modify_Y(self.train_Y, class_item) self.temp_train_Y = self.train_Y self.train_Y = modify_Y self.temp_W = self.W self.W = self.temp_W[class_item] self.temp_W[class_item] = accelerator.init_W(self) self.train_Y = self.temp_train_Y self.temp_train_Y = [] self.W = self.temp_W self.temp_W = {} return self.W
def train(self): predict_X = [] for x in self.models[0].train_X: x_string = ' '.join(map(str, x.tolist()[1:])) transform_x = self.feature_transform(x_string) transform_x.insert(0, 1) predict_X.append(transform_x) self.linear = linear_regression.LinearRegression() self.linear.load_train_data() self.linear.load_test_data() self.linear.train_X = np.array(predict_X) self.linear.train_Y = self.models[0].train_Y self.linear.set_param() self.linear.init_W() self.linear.train()
def init_W(self, mode='normal'): ''' Init the W Simple way is init W all zeros ''' if (self.status != 'load_train_data') and (self.status != 'train'): print("Please load train data first.") return self.W self.status = 'init' self.data_num = len(self.train_Y) self.data_demension = len(self.train_X[0]) self.W = np.zeros(self.data_demension) if mode == 'linear_regression_accelerator': accelerator = linear_regression.Accelerator() self.W = accelerator.init_W(self) return self.W