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を使ったデータ分析、機械学習
2.7
 ファンシーインデクス
83
[ 6. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
4
はどこに行ったのでしょう。この操作では、最初に
x[0] = 4
が、次に
x[0] = 6
が行われてい
ます。そのため、
x[0]
の値は当然
6
になります。
十分納得できる結果です。それでは次の操作はどうでしょう。
In[21]: i = [2, 3, 3, 4, 4, 4]
x[i] += 1
x
Out[21]: array([ 6., 0., 1., 1., 1., 0., 0., 0., 0., 0.])
各インデクスが何回繰り返されたかに従い、
x[3]
2
x[4]
3
になると思われましたが、実際
にはそうなりませんでした。概念上、
x[i] + = 1
x[i] = x[i] + 1
の省略形であることが原因で
す。
x[i] + 1
が評価され、その結果がインデクス配列で示された
x
の要素に割り当てられます。つ
まり、複数回発生するのはインクリメントではなく代入であり、直感的に予期するものとは異なる
結果となります。
では、繰り返された操作のそれぞれで特定の動作を行いたい場合はどうすればよいのでしょう
か?
ufuncs
at()
メソッド(
NumPy 1.8
から利用可能)を使用して、次のようにします。
In[22]: x = np.zeros(10)
np.add.at(x, i, 1)
print(x)
[ 0. 0. 1. 2. 3. 0. 0. 0. 0. 0.]
at()
メソッドは、指定されたインデクス ...
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