How To Create An Array From Existing Data ?
Table Of Contents:
- Slicing and Indexing
- np.vstack( )
- np.hstack( )
- np.hsplit( )
- .view( )
- copy( )
(1) Slicing and Indexing
- You can easily create a new array from a section of an existing array.
- You need to mention the start and end index position of the array.
Example:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr1 = a[3:8]
arr1
Output:
array([4, 5, 6, 7, 8])
Note:
- Here I have created a new array from an existing array by mentioning the start and end index of the array.
(2) np.vstack( )
- np.vstack( ) is used to stack two existing arrays vertically together.
Example:
import numpy as np
a1 = np.array([[1, 1],
[2, 2]])
a2 = np.array([[3, 3],
[4, 4]])
np.vstack((a1, a2))
Output:
array([[1, 1],
[2, 2],
[3, 3],
[4, 4]])
(3) np.hstack( )
- np.hstack( ) is used to stack two existing arrays horizontally together.
Example:
import numpy as np
a1 = np.array([[1, 1],
[2, 2]])
a2 = np.array([[3, 3],
[4, 4]])
np.hstack((a1, a2))
Output:
array([[1, 1, 3, 3],
[2, 2, 4, 4]])
(4) np.hsplit( )
- You can split an array into several smaller arrays using
hsplit
. - You can specify either the number of equally shaped arrays to return or the columns after which the division should occur.
Example:
x = np.arange(1, 25).reshape(2, 12)
x
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])
- If you wanted to split this array into three equally shaped arrays, you would run:
np.hsplit(x, 3)
Output:
[array([[ 1, 2, 3, 4],
[13, 14, 15, 16]]),
array([[ 5, 6, 7, 8],
[17, 18, 19, 20]]),
array([[ 9, 10, 11, 12],
[21, 22, 23, 24]])]
- If you wanted to split your array after the third and fourth column, you’d run:
np.hsplit(x, (3, 4))
Output:
[array([[ 1, 2, 3],
[13, 14, 15]]),
array([[ 4],
[16]]),
array([[ 5, 6, 7, 8, 9, 10, 11, 12],
[17, 18, 19, 20, 21, 22, 23, 24]])]
(5) view
- ‘view’ is the shallow copy of the array. When you do a slicing and indexing a ‘view’ will be automatically created.
- This saves memory and is faster (no copy of the data has to be made).
- However, it’s essential to be aware of this – modifying data in a view also changes the original array!
Example:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
b1 = a[0, :]
b1
array([1, 2, 3, 4])
b1[0] = 99
b1
array([99, 2, 3, 4])
a
array([[99, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
Note:
- Here, we create an array
b1
by slicinga
and modify the first element ofb1
. This will modify the corresponding element ina
as well!
(5) copy
- Using the
copy
method will make a complete copy of the array and its data (a deep copy).
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
b1 = a[0, :].copy()
b1
array([1, 2, 3, 4])
b1[0] = 99
b1
array([99, 2, 3, 4])
a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
Note:
- Here, you can see that, we have used the copy() method.
- Hence, by changing copied array ‘b1’ won’t affect the original array.