MCPcopy
hub / github.com/MingchaoZhu/DeepLearning / fit

Method fit

code/chapter7.py:746–781  ·  view source on GitHub ↗
(self, X, Y)

Source from the content-addressed store, hash-verified

744 self.loss = CrossEntropyLoss()
745
746 def fit(self, X, Y):
747 # 分类问题将 Y 转化为 one-hot 编码
748 if not self.is_regression:
749 Y = to_categorical(Y.flatten())
750 else:
751 Y = Y.reshape(-1, 1) if len(Y.shape) == 1 else Y
752 self.out_dims = Y.shape[1]
753 self.trees = np.empty((self.n_estimators, self.out_dims), dtype=object)
754 Y_pred = np.zeros(np.shape(Y))
755 self.weights = np.ones((self.n_estimators, self.out_dims))
756 self.weights[1:, :] *= self.learning_rate
757 # 迭代过程
758 for i in self.progressbar(range(self.n_estimators)):
759 for c in range(self.out_dims):
760 tree = XGBoostRegressionTree(
761 min_samples_split=self.min_samples_split,
762 min_impurity=self.min_impurity,
763 max_depth=self.max_depth,
764 loss=self.loss,
765 gamma=self.gamma,
766 lambd=self.lambd)
767 # 计算损失的梯度,并用梯度进行训练
768 if not self.is_regression:
769 Y_hat = softmax(Y_pred)
770 y, y_pred = Y[:, c], Y_hat[:, c]
771 else:
772 y, y_pred = Y[:, c], Y_pred[:, c]
773
774 y, y_pred = y.reshape(-1, 1), y_pred.reshape(-1, 1)
775 y_and_ypred = np.concatenate((y, y_pred), axis=1)
776 tree.fit(X, y_and_ypred)
777 # 用新的基学习器进行预测
778 h_pred = tree.predict(X)
779 # 加法模型中添加基学习器的预测,得到最新迭代下的加法模型预测
780 Y_pred[:, c] += np.multiply(self.weights[i, c], h_pred)
781 self.trees[i, c] = tree
782
783 def predict(self, X):
784 Y_pred = np.zeros((X.shape[0], self.out_dims))

Callers

nothing calls this directly

Calls 5

fitMethod · 0.95
to_categoricalFunction · 0.85
softmaxFunction · 0.70
predictMethod · 0.45

Tested by

no test coverage detected