def _init_features(self, feature_names, X): """ Init the features the model will begin evolving. If feature names have not been specified they are generated as x1, x2, ..., xk. Parameters ---------- feature_names : list of strings Names of the features. X : ndarray training data """ features = [] if feature_names is None: feature_names = ['x' + str(x) for x in range(X.shape[1])] for i, name in enumerate(feature_names): features.append( Feature(X[:, i], name, name, original_variable=self.preserve_originals)) for _ in range(self.range_operators): features.append( RangeOperation(self.variable_type_indices, feature_names, X)) return feature_names, features
def _swap_range_operators(self, X): for f in self.current_features_: if type(f) == RangeOperation and f.original_variable: self.current_features_.remove(f) for _ in range(self.range_operators): self.current_features_.append( RangeOperation(self.variable_type_indices, self.predictor_names_, X))
def build_basis_from_features(infix_features, predictor_names, X, variable_type_indices, operators): basis = np.zeros((X.shape[0], len(infix_features))) for j, f in enumerate(infix_features): if variable_type_indices and f.startswith('RangeOperation'): range_operation = RangeOperation(variable_type_indices, predictor_names, X, string=f) basis[:, j] = np.squeeze(range_operation.value) elif f in predictor_names: variable_index = predictor_names.index(f) basis[:, j] = X[:, variable_index] else: operation_stack = build_operation_stack(f) basis[:, j] = get_feature_value(operation_stack, predictor_names, X, variable_type_indices, operators) return basis
def get_feature_value(stack, feature_names, X, variable_type_indices, operators): variables_stack = [] while len(stack) > 0: current = stack.pop() if variable_type_indices and current.startswith('RangeOperation'): range_operation = RangeOperation(variable_type_indices, feature_names, X, string=current) variables_stack.append(np.squeeze(range_operation.value)) elif current in feature_names: variable_index = feature_names.index(current) variables_stack.append(X[:, variable_index]) elif operators.contains(current): operator = operators.get(current) variables = [] for _ in range(operator.parity): variables.append(variables_stack.pop()) result = operator.operation(*variables) variables_stack.append(result) return variables_stack.pop()