Created
January 11, 2016 03:12
-
-
Save karino2/38763d2d39ac2cb5aafb to your computer and use it in GitHub Desktop.
ゆる募: リストから最頻値を得る方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| intのリストから最頻値を取り出したい。 | |
| たとえば | |
| answers = [1, 2, 7, 3, 2, 5, 2, 1] | |
| というリストがあったら、一番登場回数が多い2が答えとして欲しい。 | |
| 使い道はrandomforestでの投票の結果を得たい。 | |
| 以下は自分が書いた方法だが、もっとマシな解決策があるに違いない。 | |
| """ | |
| classes = list(set(answers)) | |
| freqs = map(lambda klas: answers.count(klas), classes) | |
| classes[freqs.index(max(freqs))] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
collections.Counter(answers).most_common(1) という助言を@nsharp_2chさんからいただきました。
これで良さそうですね。