def train(self):
if (self.status != '<STR_LIT>'):<EOL><INDENT>print("<STR_LIT>")<EOL>return self.W<EOL><DEDENT>self.status = '<STR_LIT:train>'<EOL>new_W = self.W<EOL>self.temp_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)<EOL>for _ in range(self.updates):<EOL><INDENT>if (self.loop_mode is '<STR_LIT>'):<EOL><INDENT>data_check_order = range(self.data_num)<EOL><DEDENT>elif (self.loop_mode is '<STR_LIT>'):<EOL><INDENT>data_check_order = range(self.data_num)<EOL>data_check_order = random.sample(data_check_order, self.data_num)<EOL><DEDENT>else:<EOL><INDENT>data_check_order = range(self.data_num)<EOL>data_check_order = random.sample(data_check_order, self.data_num)<EOL><DEDENT>for i in data_check_order:<EOL><INDENT>if self.error_function(self.score_function(self.train_X[i], new_W), self.train_Y[i]):<EOL><INDENT>self.tune_times += <NUM_LIT:1><EOL>new_W = new_W + self.step_alpha * (self.train_Y[i] * self.train_X[i])<EOL>new_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)<EOL>if new_avg_error < self.temp_avg_error:<EOL><INDENT>self.put_in_pocket_times += <NUM_LIT:1><EOL>self.temp_avg_error = new_avg_error<EOL>self.W = new_W<EOL><DEDENT>break<EOL><DEDENT><DEDENT><DEDENT>return self.W<EOL>
Train Pocket Perceptron Learning Algorithm
From f(x) = WX
Find best h(x) = WX similar to f(x)
Output W