2.5. Manipulating Arrays#
There may be times when you want to be able to manipulate arrays by performing actions such as appending values to the end of an array, inserting values into the middle and array or deleting elements from an array altogether. The NumPy library provides a number of functions shown in Table 2.3 to do this.
Function |
Description |
---|---|
|
Add elements to the end of an array |
|
Insert elements into an array |
|
Delete elements from an array |
|
Sort the elements of an array into ascending order |
|
Change the number of rows and columns of an array |
2.5.1. Appending to an array#
To append (put something on the end of) to an array we use the np.append()
function. The syntax for this is
np.append(array, object)
where object
is the thing we are appending to array
. To demonstrate this lets append the array [4, 5]
to the end of the array a
. Add the following code to your program.
# Manipulating arrays
print(f"a = {a}")
a = np.append(a, np.array([4, 5]))
print(f"Append [4, 5] to array a: a = {a} \n")
Run the program and you should see the following added to the console.
Append [4, 5] to array a: a = [1 2 3 4 5]
When dealing with 2D arrays we need to specify which axis to which we are appending.
np.append(array, object, axis)
If axis = 0
then object
is appended to the bottom of array
and if axis = 1
then object
is appended to the right of array
. To demonstrate this, enter the following code into your program.
new_matrix = np.append(A, B, 0)
print(f"Append B to the bottom of A: \n\n {new_matrix} \n")
new_matrix = np.append(A, B, 1)
print(f"Append B to the right of A: \n\n {new_matrix} \n")
Run your program and you should see the following added to the console.
Append B to the bottom of A:
[[1 2]
[3 4]
[5 6]
[7 8]]
Append B to the right of A:
[[1 2 5 6]
[3 4 7 8]]
2.5.2. Inserting into an array#
To insert an object into an array we can use the np.insert()
function. The syntax for this is
np.insert(array, index, object)
where object
will be inserted into array
starting at the index
. Lets insert the array [6, 7, 8]
starting at the 3rd element of a
. Add the following code to your program.
a = np.insert(a, 2, np.array([6, 7, 8]))
print(f"\nInsert [6, 7, 8] into array a: a = {a} \n")
Run the program and you should see the following added to the console.
Insert [6, 7, 8] into array a: a = [1 2 6 7 8 3 4 5]
When dealing with 2D arrays we need to specify the axis.
np.insert(array, index, object, axis)
If axis = 0
then object
will be inserted at the index
row and axis = 1
then object
will be inserted at the index
column. To demonstrate this, enter the following code into your program.
new_matrix = np.insert(A, 1, B, 0)
print(f"Insert B between the rows of A: \n\n {new_matrix} \n")
new_matrix = np.insert(A, 1, B.T, 1)
print(f"Insert B between the columns of A: \n\n {new_matrix} \n")
Run the program and you should see the following added to the console.
Insert B between the rows of A:
[[1 2]
[5 6]
[7 8]
[3 4]]
Insert B between the columns of A:
[[1 5 6 2]
[3 7 8 4]]
Here E
is formed by inserting B
into A
at the 2nd row and F
is formed by inserting the transpose of B
into A
at the 2nd column (note that we needed to transpose the B
array when inserting between the columns).
2.5.3. Deleting from an array#
To delete an object or objects from an array we can use the np.delete()
function. The syntax for this is
np.delete(array, index)
where the objects located at index
will be removed from array
. Lets remove the numbers 7 and 8 from a
. We want to delete the elements with indices 3 and 4 so add the following code to your program.
a = np.delete(a, [3, 4])
print(f"Delete the 4th and 5th elements of a: a = {a} \n")
Run the program and you should see the following added to the console.
Delete the 4th and 5th elements of a: a = [1 2 6 3 4 5]
When dealing with 2D arrays we need to specify the axis we are deleting along.
np.delete(array, index, axis)
If axis = 0
then column index
is deleted and if axis = 1
then row index
is deleted. To demonstrate this, enter the following code into your program.
C = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(f"C = \n\n {C} \n")
new_matrix = np.delete(C, 2, 0)
print(f"Delete the 2nd row of C: \n\n {new_matrix} \n")
new_matrix = np.delete(C, -1, 1)
print(f"Delete the last column of C: \n\n {new_matrix} \n")
Run the program and you should see the following added to the console.
C =
[[1 2 3]
[4 5 6]
[7 8 9]]
Delete the 2nd row of C:
[[1 2 3]
[4 5 6]]
Delete the 2nd row of C:
[[1 2]
[4 5]
[7 8]]
2.5.4. Sorting an array#
To sort an array into ascending order we can use the np.sort()
function. The syntax for this is
np.sort(array)
Lets sort the array into ascending order. To demonstrate this, enter the following code into your program.
a = np.sort(a)
print(f"Sort array a into ascending order: a = {a} \n")
Run the program and you should see the following added to the console.
Sort array a into ascending order: a = [1 2 3 4 5 6]
2.5.5. Reshaping an array#
To reshape an array (change the shape to a different number of rows and columns) we can use the np.reshape()
function. The syntax for this is
np.reshape(array, (rows, columns), order)
Lets reshape a
so that it is a 3 \(\times\) 2 array, enter the following code into your program.
a_3x2 = np.reshape(a, (3, 2))
print(f"Reshape array a into a 3 x 2 array: \n\n {a_3x2} \n")
Run the program and you should see the following added to the console.
Reshape array a into a 3 x 2 array:
[[1 2]
[3 4]
[5 6]]
Here a 2D array is formed by reshaping the 1D array a
into a \(3 \times 2\) array where the elements are reshaped using row-major order so the first two elements a
form the first row of of the 2D array, the next two elements form the second row of the 2D array and so on.
The other way is to use column-major order where the columns of the reshaped are formed by taking successive elements to from the original array. The NumPy function np.reshape()
allows us to choose which order to use by specifying 'C'
for row-major order which is the default method or 'F'
for column-major order[1]. To demonstrate this, enter the following code into your program.
a_3x2 = np.reshape(a, (3, 2), 'F')
print(f"Reshape array a into a 3 x 2 array (column order): \n\n {a_3x2} \n")
Run the program and you should see the following added to the console.
Reshape array a into a 3 x 2 array (row-major order):
[[1 4]
[2 5]
[3 6]]
2.5.6. Exercise#
Use the arrays from Exercise 2.1 and appropriate NumPy functions to print the following:
\(D\) appended to the right of \(B\)
The first two rows of \(D\) appended to the bottom of \(B\)
\(D\) inserted between the 2nd and 3rd columns of \(B\)
\(B\) with the middle row removed
\(\vec{a}\) sorted in descending order (hint: you will need to use array slicing)
\(C\) reshaped into an \(8 \times 1\) array