Skip to Content
Pythonデータサイエンスハンドブック ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習
book

Pythonデータサイエンスハンドブック ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

by Jake VanderPlas, 菊池 彰
May 2018
Intermediate to advanced
556 pages
13h 21m
Japanese
O'Reilly Japan, Inc.
Content preview from Pythonデータサイエンスハンドブック ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習
88
2
章 
NumPy
の基礎
間の関係が失われることに注意してください。
2.8.2
 部分ソート:分割(パーティショニング)
配列全体をソートするのではなく、配列内の
K
個の最小値を探したいだけという場合がありま
す。このために
NumPy
の提供する
np.partition
関数を使います。
np.partition
関数に配列と数値
K
を渡すと、左側のパーティションに最も小さい
K
個の要素が、残りの値は右側のパーティショ
ンに、それぞれ任意の順序で並ぶ新しい配列ができます。
In[12]: x = np.array([7, 2, 3, 1, 6, 5, 4])
np.partition(x, 3)
Out[12]: array([2, 1, 3, 4, 6, 5, 7])
最初の
3
つの値は、元の配列内の
3
つの最小値であり、残りの位置には残りの値が含まれている
ことに注意してください。
2
つのパーティション内では、要素は任意の順序で並び
ます。
並べ替えと同様に、多次元配列を任意の軸に沿って分割できます。
In[13]: np.partition(X, 2, axis=1)
Out[13]: array([[3, 4, 6, 7, 6, 9],
[2, 3, 4, 7, 6, 7],
[1, 2, 4, 5, 7, 7],
[0, 1, 4, 5, 9, 5]])
この結果は、各行の最初の
2
つの要素がその行における
2
個の最小値に、残りの値が残りの要素
となる配列です。
ソートのインデクスを計算する
np.argsort
があ
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pythonデータサイエンスハンドブック 第2版 ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

Pythonデータサイエンスハンドブック 第2版 ―Jupyter、NumPy、pandas、Matplotlib、scikit-learnを使ったデータ分析、機械学習

Jake VanderPlas, 菊池 彰
初めてのGraphQL ―Webサービスを作って学ぶ新世代API

初めてのGraphQL ―Webサービスを作って学ぶ新世代API

Eve Porcello, Alex Banks, 尾崎 沙耶, あんどうやすし

Publisher Resources

ISBN: 9784873118413Other