download (2)
download (2)
download (2)
keyboard_arrow_down Content
Shallow vs Deep Copy
view()
copy()
Array Splitting
split()
hsplit()
vsplit()
Array Stacking
hstack()
vstack()
concatenate()
Let's create some arrays to understand what's happens while using numpy.
import numpy as np
a = np.arange(4)
a
array([0, 1, 2, 3])
b = a.reshape(2, 2)
b
array([[0, 1],
[2, 3]])
a[0] = 100
a
array([100, 1, 2, 3])
array([[100, 1],
[ 2, 3]])
Now, let's see an example where Numpy will create a Deep Copy of data.
a = np.arange(4)
a
array([0, 1, 2, 3])
# Create `c`
c = a + 2
c
array([2, 3, 4, 5])
a[0] = 100
a
array([100, 1, 2, 3])
array([2, 3, 4, 5])
False
Because it is an operation.
A more permanent change in data.
So, Numpy had to create a separate copy for c - i.e., deep copy of array a for array c .
keyboard_arrow_down Conclusion:
Numpy is able to use same data for simpler operations like reshape → Shallow Copy.
It creates a copy of data where operations make more permanent changes to data → Deep Copy.
Is there a way to check whether two arrays are sharing memory or not?
a= np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a[::2]
b
array([0, 2, 4, 6, 8])
np.shares_memory(a,b)
True
a[0] = 1000
array([1000, 2, 4, 6, 8])
a = np.arange(6)
a
array([0, 1, 2, 3, 4, 5])
b = a[a % 1 == 0]
b
array([0, 1, 2, 3, 4, 5])
b[0] = 10
a[0]
np.shares_memory(a,b)
False
Note:
a = np.arange(10)
a_shallow_copy = a.view()
# Creates a shallow copy of a
np.shares_memory(a_shallow_copy, a)
True
a_deep_copy = a.copy()
# Creates a deep copy of a
np.shares_memory(a_deep_copy, a)
False
keyboard_arrow_down .view()
Documentation: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.view.html
arr = np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
view_arr = arr.view()
view_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Let's modify the content of view_arr and check whether it modified the original array as well.
view_arr[4] = 420
view_arr
arr
np.shares_memory(arr, view_arr)
True
keyboard_arrow_down .copy()
arr = np.arange(10)
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
copy_arr = arr.copy()
copy_arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Let's modify the content of copy_arr and check whether it modified the original array as well.
copy_arr[3] = 45
copy_arr
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.shares_memory(arr, copy)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-35-84a6cba2b044> in <cell line: 1>()
----> 1 np.shares_memory(arr, copy)
keyboard_arrow_down Splitting
In addition to reshaping and selecting subarrays, it is often necessary to split arrays into smaller arrays or merge arrays into bigger arrays.
keyboard_arrow_down np.split()
If indices_or_sections is an integer, n, the array will be divided into n equal arrays along axis.
If indices_or_sections is a 1-D array of sorted integers, the entries indicate where along axis the array is split.
If an index exceeds the dimension of the array along axis, an empty sub-array is returned correspondingly.
x = np.arange(9)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.split(x, 3)
IMPORTANT REQUISITE
b = np.arange(10)
np.split(b, 3)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-38-5033f171e13f> in <cell line: 2>()
1 b = np.arange(10)
----> 2 np.split(b, 3)
1 frames
/usr/local/lib/python3.10/dist-packages/numpy/lib/shape_base.py in split(ary, indices_or_sections, axis)
870 N = ary.shape[axis]
871 if N % sections:
--> 872 raise ValueError(
873 'array split does not result in an equal division') from None
874 return array_split(ary, indices_or_sections, axis)
b[0:-1]
np.split(b[0:-1], 3)
c = np.arange(16)
np.split(c, [3, 5, 6])
[array([0, 1, 2]),
array([3, 4]),
array([5]),
array([ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])]
keyboard_arrow_down np.hsplit()
Splits an array into multiple sub-arrays horizontally (column-wise).
x = np.arange(16.0).reshape(4, 4)
x
The split we want happens across the 2nd axis (Horizontal axis)
That is why we use hsplit()
So, try to think in terms of "whether the operation is happening along vertical axis or horizontal axis".
np.hsplit(x, 2)
keyboard_arrow_down np.vsplit()
x = np.arange(16.0).reshape(4, 4)
x
The split we want happens across the 1st axis (Vertical axis)
That is why we use vsplit()
Again, always try to think in terms of "whether the operation is happening along vertical axis or horizontal axis".
np.vsplit(x, 2)
np.vsplit(x, np.array([3]))
keyboard_arrow_down Stacking
a = np.arange(1, 5)
b = np.arange(2, 6)
c = np.arange(3, 7)
keyboard_arrow_down np.vstack()
np.vstack([b, c, a])
array([[2, 3, 4, 5],
[3, 4, 5, 6],
[1, 2, 3, 4]])
a = np.arange(1, 5)
b = np.arange(2, 4)
c = np.arange(3, 10)
np.vstack([b, c, a])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-49-5148cb6ebc5f> in <cell line: 1>()
----> 1 np.vstack([b, c, a])
2 frames
/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the arra
a = np.arange(5).reshape(5, 1)
a
array([[0],
[1],
[2],
[3],
[4]])
b = np.arange(15).reshape(5, 3)
b
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
np.hstack([a, b])
array([[ 0, 0, 1, 2],
[ 1, 3, 4, 5],
[ 2, 6, 7, 8],
[ 3, 9, 10, 11],
[ 4, 12, 13, 14]])
keyboard_arrow_down np.concatenate()
Provides similar functionality, but it takes a keyword argument axis that specifies the axis along which the arrays are to be concatenated.
The input array to concatenate() needs to be of dimensions atleast equal to the dimensions of output array.
a = np.array([1,2,3])
a
array([1, 2, 3])
b = np.array([[1,2,3], [4,5,6]])
b
array([[1, 2, 3],
[4, 5, 6]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-55-1a93c4fe21df> in <cell line: 1>()
----> 1 np.concatenate([a, b], axis = 0)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the
concatenate() can only work if both a and b have the same number of dimensions.
a = np.array([[1,2,3]])
b = np.array([[1,2,3], [4,5,6]])
a = np.arange(6).reshape(3, 2)
b = np.arange(9).reshape(3, 3)
array([[0, 1, 0, 1, 2],
[2, 3, 3, 4, 5],
[4, 5, 6, 7, 8]])
a = np.array([[1,2], [3,4]])
b = np.array([[5,6,7,8]])
array([1, 2, 3, 4, 5, 6, 7, 8])
array([[1, 2],
[3, 4]])
b = np.array([[5, 6]])
b
array([[5, 6]])
array([[1, 2],
[3, 4],
[5, 6]])
Dimensions of a is 2 ×2
What is the dimensions of b ?
1-D array ?? - NO
Look carefully!!
b is a 2-D array of dimensions 1 ×2
axis = 0 ---> It's a vertical axis
b = np.array([[5, 6]])
b
array([[5, 6]])
array([[1, 2, 5],
[3, 4, 6]])
Dimensions of a is again 2 ×2
Dimensions of b is again 1 ×2
So, Dimensions of b.T will be 2 × 1
Code Text