Stacking¶
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [2]:
foo = np.array(['a', 'b'])
bar = np.array(['c', 'd'])
baz = np.array([['e', 'f']])
bingo = np.array([['g', 'h', 'i']])
bongo = np.array([['j', 'k'], ['l', 'm']])
In [3]:
np.vstack((foo, bar))
Out[3]:
array([['a', 'b'], ['c', 'd']], dtype='<U1')
In [4]:
np.vstack((foo, bar, baz))
Out[4]:
array([['a', 'b'], ['c', 'd'], ['e', 'f']], dtype='<U1')
In [5]:
np.vstack((baz, bingo))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 1 ----> 1 np.vstack((baz, bingo)) File <__array_function__ internals>:200, in vstack(*args, **kwargs) File ~/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages/numpy/core/shape_base.py:296, in vstack(tup, dtype, casting) 294 if not isinstance(arrs, list): 295 arrs = [arrs] --> 296 return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting) File <__array_function__ internals>:200, in concatenate(*args, **kwargs) ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 3
In [6]:
np.hstack((foo, bar))
Out[6]:
array(['a', 'b', 'c', 'd'], dtype='<U1')
In [8]:
np.hstack((baz, bingo))
Out[8]:
array([['e', 'f', 'g', 'h', 'i']], dtype='<U1')
In [9]:
np.hstack((foo, bingo))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[9], line 1 ----> 1 np.hstack((foo, bingo)) File <__array_function__ internals>:200, in hstack(*args, **kwargs) File ~/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages/numpy/core/shape_base.py:368, in hstack(tup, dtype, casting) 366 # As a special case, dimension 0 of 1-dimensional arrays is "horizontal" 367 if arrs and arrs[0].ndim == 1: --> 368 return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting) 369 else: 370 return _nx.concatenate(arrs, 1, dtype=dtype, casting=casting) File <__array_function__ internals>:200, in concatenate(*args, **kwargs) ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
In [10]:
np.hstack((bingo, bongo))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[10], line 1 ----> 1 np.hstack((bingo, bongo)) File <__array_function__ internals>:200, in hstack(*args, **kwargs) File ~/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages/numpy/core/shape_base.py:370, in hstack(tup, dtype, casting) 368 return _nx.concatenate(arrs, 0, dtype=dtype, casting=casting) 369 else: --> 370 return _nx.concatenate(arrs, 1, dtype=dtype, casting=casting) File <__array_function__ internals>:200, in concatenate(*args, **kwargs) ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1 and the array at index 1 has size 2
In [11]:
np.stack((foo, bar), axis=0)
Out[11]:
array([['a', 'b'], ['c', 'd']], dtype='<U1')
In [12]:
np.stack((foo, bar), axis=1)
Out[12]:
array([['a', 'c'], ['b', 'd']], dtype='<U1')
In [13]:
np.stack((foo, bar), axis=2)
--------------------------------------------------------------------------- AxisError Traceback (most recent call last) Cell In[13], line 1 ----> 1 np.stack((foo, bar), axis=2) File <__array_function__ internals>:200, in stack(*args, **kwargs) File ~/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages/numpy/core/shape_base.py:467, in stack(arrays, axis, out, dtype, casting) 464 raise ValueError('all input arrays must have the same shape') 466 result_ndim = arrays[0].ndim + 1 --> 467 axis = normalize_axis_index(axis, result_ndim) 469 sl = (slice(None),) * axis + (_nx.newaxis,) 470 expanded_arrays = [arr[sl] for arr in arrays] AxisError: axis 2 is out of bounds for array of dimension 2
In [14]:
np.stack((foo, bar), axis=-1)
Out[14]:
array([['a', 'c'], ['b', 'd']], dtype='<U1')