def test(self, testing_data_matrix, testing_label_matrix): testing_label_matrix = numpy_helper.transfer_values(testing_label_matrix, self.transfer_hash) row_count = testing_data_matrix.shape[0] errors = tuple( self.__classify(testing_data_matrix[i, :]) != numpy.sign(testing_label_matrix[i, 0]) for i in range(row_count) ) self.last_test_info = self.__package_info(errors.count(True), row_count) return self.last_test_info
def train( cls, data_matrix, label_matrix, edge_threshold, tolerance, max_iteration_count, arg_exp, transfer_hash={1: 1, -1: -1}, ): label_matrix = numpy_helper.transfer_values(label_matrix, transfer_hash) smo_basic = SmoBasic(data_matrix, label_matrix, edge_threshold, tolerance, arg_exp) alphas, b = smo_basic.cal_alphas_and_b(max_iteration_count) nonzero_indexs = numpy.nonzero(alphas.A > 0)[0] selected_data_matrix = data_matrix[nonzero_indexs] selected_alphas_label_matrix = numpy.multiply(label_matrix[nonzero_indexs], alphas[nonzero_indexs]) return cls(selected_data_matrix, selected_alphas_label_matrix, b, arg_exp, transfer_hash)