id3.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:decision-tree-id3 作者: svaante 项目源码 文件源码
def predict(self, X):
        """Predict class for every sample in X.

        Parameters
        ----------
        X : array-like of shape = [n_samples, n_features_idx]
            The input samples.

        Returns
        -------
        y : array of shape = [n_samples]
        """
        check_is_fitted(self, 'tree_')
        X = check_array(X)
        n_features = X.shape[1]
        if n_features != self.n_features_:
            raise ValueError("Number of features of the model must "
                             "match the input. Model n_features is {} and "
                             "input n_features is {}."
                             .format(self.n_features_, n_features))

        X_ = np.empty(X.shape)
        for i in range(self.n_features_):
            if self.is_numerical_[i]:
                X_[:, i] = X[:, i]
            else:
                try:
                    X_[:, i] = self.X_encoders_[i].transform(X[:, i])
                except ValueError as e:
                    raise ValueError('New attribute value not found in '
                                     'train data.')
        y = self.builder_._predict(self.tree_, X_)
        return self.y_encoder_.inverse_transform(y)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号