LoginSignup
0
0

More than 1 year has passed since last update.

【Python】numpyで空の配列を生成する方法

Last updated at Posted at 2022-10-03

背景

  • 最終的な要素数がわからない場合に、空の配列を作成しておいて、要素を追加して配列を作成したい時があります。
  • listでは、l = []という形式で空の配列を作り、l.append(1)という形で簡単に要素を追加できます。
  • ただ、おそらくlistで行うよりもnumpyで同様の処理を行なった方が、処理速度が速いので、今回numpyでの方法を調べてみました。

目標

  • numpyで空の配列を作れるようになる。

numpyで空の配列を生成する方法

  • numpyで空の配列を作成するには、np.empty()を使用すればできます。
numpyでの空の配列の作り方
#1次元の空の配列を作る
1d_empty = np.empty([0])

#2次元の空の配列を作る
2d_empty = np.empty([0, 0])

#3次元の空の配列を作る
3d_empty = np.empty([0, 0, 0])

補足

  • 速度検証もそのうち行いたい。

参考資料

個人ブログ

0
0
0

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