Help us understand the problem. What is going on with this article?

[python]numpy配列のappend方法

やりたいこと

numpy配列をappendしたい。

一次元

array1 = np.array([])
array1 = np.append(array1, np.array([1, 2, 3]))
array1 = np.append(array1, np.array([4, 5, 6]))
array1
array([1., 2., 3., 4., 5., 6.])

二次元

array2 = np.empty((0,3))
array2 = np.append(array2, np.array([[1, 2, 3]]), axis = 0)
array2 = np.append(array2, np.array([[4, 5, 6]]), axis = 0)
array2
array([[1., 2., 3.],
       [4., 5., 6.]])

今回大切なのはnp.empty((0,3))と次元を合わせることが大切

array2 = np.empty((0,3))
array2 = np.append(array2, np.array([1, 2, 3]), axis = 0)
array2 = np.append(array2, np.array([4, 5, 6]), axis = 0)
array2
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-0cbe71e01df5> in <module>
      1 array2 = np.empty((0,3))
----> 2 array2 = np.append(array2, np.array([1, 2, 3]), axis = 0)
      3 array2 = np.append(array2, np.array([4, 5, 6]), axis = 0)
      4 array2

~\Anaconda3\envs\tensorflow\lib\site-packages\numpy\lib\function_base.py in append(arr, values, axis)
   4526         values = ravel(values)
   4527         axis = arr.ndim-1
-> 4528     return concatenate((arr, values), axis=axis)

ValueError: all the input arrays must have same number of dimensions

つまり以下のである。

array3 = np.array([1, 2, 3])
array4 = np.array([[1, 2, 3]])
array5 = np.empty((0,3))
print(array3.shape)
print(array4.shape)
print(array5.shape)
(3,)
(1, 3)
(0, 3)

ちなみに

np.array([array3]).shape
(1, 3)

多次元

import numpy as np
a = np.random.randn(1000, 10)
b = np.random.randn(1000, 10)
c = np.random.randn(1000, 10)
new_img = np.empty((0,1000,10))
new_img = np.append(new_img,np.array([a]),axis=0)
new_img = np.append(new_img,np.array([b]),axis=0)
new_img = np.append(new_img,np.array([c]),axis=0)
new_img.shape

(3, 1000, 10)

axisについて

array2 = np.random.randn(1000,100,10,1)
print(array2.shape)
array2 = np.append(array2, array2, axis = 0)
array2 = np.append(array2, array2, axis = 0)
array2 = np.append(array2, array2, axis = 0)
print(array2.shape)
(1000, 100, 10, 1)
(8000, 100, 10, 1)
array2 = np.random.randn(1000,100,10,1)
print(array2.shape)
array2 = np.append(array2, array2, axis = 1)
array2 = np.append(array2, array2, axis = 1)
array2 = np.append(array2, array2, axis = 1)
print(array2.shape)
(1000, 100, 10, 1)
(1000, 800, 10, 1)
array2 = np.random.randn(1000,100,10,1)
print(array2.shape)
array2 = np.append(array2, array2, axis = 2)
array2 = np.append(array2, array2, axis = 2)
array2 = np.append(array2, array2, axis = 2)
print(array2.shape)
(1000, 100, 10, 1)
(1000, 100, 80, 1)
array2 = np.random.randn(1000,100,10,1)
print(array2.shape)
array2 = np.append(array2, array2, axis = 3)
array2 = np.append(array2, array2, axis = 3)
array2 = np.append(array2, array2, axis = 3)
print(array2.shape)
(1000, 100, 10, 1)
(1000, 100, 10, 8)
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away