Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第五章分类决策树代码问题 #60

Open
zzh1161 opened this issue May 21, 2022 · 4 comments
Open

第五章分类决策树代码问题 #60

zzh1161 opened this issue May 21, 2022 · 4 comments

Comments

@zzh1161
Copy link

zzh1161 commented May 21, 2022

第五章,在分类决策树的train()函数里

sub_train_df = train_data.loc[train_data[max_feature_name] ==
                                          f].drop([max_feature_name], axis=1)

这里把当前最大特征那一行给drop掉了,导致下面通过index索引特征时会出现错误

# class Node
def predict(self, features):
        if self.root is True:
            return self.label
        return self.tree[features[self.feature]].predict(features)

建议可以直接把DTree类中的predict()函数的参数改成Dataframe格式,这样不再需要通过当先特征的下标来索引,而是直接通过特征来索引

# class DTree
def predict(self, X):
        m,n = np.shape(X)
        pred_res = []
        for i in range(m):
            temp = X.iloc[i,:]
            pred_res.append(self.tree.predict(temp))
        return pred_res

# class Node
def predict(self, test):
        if self.root is True:
            return self.label
        return self.tree[test[self.feature_name]].predict(test)
@AaronYin0514
Copy link

也发现了这个问题

@nicholaslsq
Copy link

nicholaslsq commented Dec 20, 2023 via email

@cyy0214
Copy link

cyy0214 commented Dec 20, 2023 via email

@daibitao19
Copy link

daibitao19 commented Dec 20, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants