= np.array([[1, 2, 3], [4, 5, 6]])
matrix print(matrix)
[[1 2 3]
[4 5 6]]
Various functions allow us to change the shape and contents of arrays.
One of the most important array operations is transposing. This operation switches rows with columns and vice versa.
= np.array([[1, 2, 3], [4, 5, 6]])
matrix print(matrix)
[[1 2 3]
[4 5 6]]
If we now transpose this array, we get:
print(np.transpose(matrix))
[[1 4]
[2 5]
[3 6]]
If we have this matrix and want to turn it into a vector, we can use the np.flatten()
function:
= matrix.flatten()
vector print(vector)
[1 2 3 4 5 6]
To return to a two-dimensional data structure, we use the function np.reshape(target, shape)
:
print(np.reshape(matrix, [3, 2]))
[[1 2]
[3 4]
[5 6]]
If we want to expand, shrink, or modify the content of an existing array, NumPy also provides suitable functions.
If we have an empty array or want to add elements to an existing array, we use the np.append()
function. This function appends a value to the existing array.
= np.array([1, 2, 3, 4, 5, 6])
array
= np.append(array, 7)
new_array print(new_array)
[1 2 3 4 5 6 7]
Sometimes we need to insert a value not at the end but at a specific position in the array. The appropriate tool here is the function np.insert(array, position, insertion)
. In the following example, the number 7 is inserted at the third position.
= np.array([1, 2, 3, 4, 5, 6])
array
= np.insert(array, 3, 7)
new_array print(new_array)
[1 2 3 7 4 5 6]
Just like we can insert new elements, we can also delete elements. For this, we use the function np.delete(array, position)
, which takes the array and the position of the element to delete.
= np.array([1, 2, 3, 4, 5, 6])
array
= np.delete(array, 3)
new_array print(new_array)
[1 2 3 5 6]
Lastly, let’s look at joining two arrays. In the following example, array b
is appended to array a
using the function np.concatenate((array a, array b))
.
= np.array([1, 2, 3, 4, 5, 6])
a = np.array([7, 8, 9, 10])
b
= np.concatenate((a, b))
new_array print(new_array)
[ 1 2 3 4 5 6 7 8 9 10]
NumPy also provides the ability to sort arrays. In the following example, we start with an unsorted array. Using the np.sort()
function, we obtain a sorted array.
import numpy as np
= np.array([4, 2, 1, 6, 3, 5])
unsorted
= np.sort(unsorted)
sorted_array
print(sorted_array)
[1 2 3 4 5 6]
When working with data where, for example, projects are assigned employee IDs, there may be a finite number of employee IDs that appear multiple times if an employee works on several projects.
If we want a list where each number appears only once, we can use the np.unique
function.
import numpy as np
= np.array([4, 1, 1, 6, 3, 4, 7, 3, 3])
list_with_duplicates
= np.unique(list_with_duplicates)
unique_values
print(unique_values)
[1 3 4 6 7]
If we also set the option return_counts=True
, a second variable will store how often each value occurs.
import numpy as np
= np.array([4, 1, 1, 6, 3, 4, 7, 3, 3])
list_with_duplicates
= np.unique(list_with_duplicates, return_counts=True)
unique_values, counts
print(counts)
[2 3 2 1 1]
Given the following two-dimensional array named matrix
:
= np.array([
matrix 4, 7, 2, 8],
[1, 5, 3, 6],
[9, 2, 4, 7]
[ ])
matrix
into a one-dimensional array.matrix
and print them.