LoginSignup
0
0

More than 5 years have passed since last update.

numpyでのリストでのappendの方法まとめ(自分用メモ)

Last updated at Posted at 2019-03-19

同じミスをしないための自分用メモ

恥ずかしいくらいのミスをしたけどもう二度と間違えないために記事にして供養します。


aa = np.array([]) 
bb = np.array([]) 

print(aa)
print(bb)

aa[0] =np.append(aa,1)
#IndexError: index 0 is out of bounds for axis 0 with size 0


bb[0] =np.append(bb,1)
#IndexError: index 0 is out of bounds for axis 0 with size 0

しょうもないミスすぎて検索にも引っかかりませんでしたが、普通に配列番号入れちゃだめですよね。

aa = np.array([]) 
bb = np.array([]) 

print(aa)
print(bb)

aa =np.append(aa,1)
#(1.)

bb =np.append(bb,1)
#(1.)

numpyの配列は返り値が配列なので当然といえば当然ですよね。
listならappendするだけで良いのでわからなくなるんですよね。

余談ですが、配列の末尾に追加するときはlistのほうが速度が早いです。機械学習で大きいデータを扱っていたので苦労してnumpyの配列への要素追加ができたにもかかわらず最後にはlistで実装しました。

オチがついたところでここまで。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0