def fit(self, X, y): assert self.__check_valid(X, y), 'input is invalid.' if self._is_trained is False: self._nFeat = X.shape[1] if self._max_depth is None: self._max_depth = self._nFeat if self._is_prune is True: spliter = ShuffleSpliter(X.shape[0], ratio=0.1) train_ix, val_ix = spliter.split() Xtrain, ytrain = X[train_ix], y[train_ix] Xval, yval = X[val_ix], y[val_ix] self._parameter['tree'] = self._build_tree(Xtrain, ytrain, depth=0) self._tree_prune(self._parameter['tree'], Xval, yval) else: self._parameter['tree'] = self._build_tree(X, y, depth=0) self._is_trained = True
def fit(self, X, y): assert self.__check_valid(X, y), 'input is invalid.' if self._is_trained is False: self._nFeat = X.shape[1] self._class_label = list(np.unique(y)) self._class_label.sort() self._nClass = len(self._class_label) if self._is_prune is True: spliter = ShuffleSpliter(X.shape[0], ratio=0.1) train_ix, val_ix = spliter.split() Xtrain, ytrain = X[train_ix], y[train_ix] Xval, yval = X[val_ix], y[val_ix] used_feat = set() self._parameter['tree'] = self._build_tree(Xtrain, ytrain, used_feat) self._tree_prune(self._parameter['tree'], Xval, yval) else: used_feat = set() self._parameter['tree'] = self._build_tree(X, y, used_feat) self._is_trained = True