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

knn的 predict的优化 #38

Open
farsmile opened this issue Apr 25, 2020 · 1 comment
Open

knn的 predict的优化 #38

farsmile opened this issue Apr 25, 2020 · 1 comment

Comments

@farsmile
Copy link

手动实现向量化的norm,应该通用性更好一些, 是否可行,请指正:
def predict_leon(self, X):
dist = np.power(X - self.X_train, self.p).sum(axis=1)
dist = np.power(dist, 1 / self.p)
knn = self.y_train[np.argsort(dist)[:self.n]]

    # 统计
    count_pairs = Counter(knn)
    max_count = sorted(count_pairs.items(), key=lambda x: x[1])[-1][0]
    return max_count
@gaojianchina
Copy link

同样是这个问题, 原代码中
max_count = sorted(count_pairs.items(), key=lambda x: x[1])[-1][0]
可以直接用
max_count=count_pairs.most_common(1)[0][0]
代替

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

2 participants