Sorting, adding and removing elements from an array
Contents
Sorting, adding and removing elements from an array#
To quickly sort the numbers of an array in ascending order we use np.sort function (after importing numpy of course):
import numpy as np #Run this cell or none of the np commands will work
array_unsorted = np.array([10, 5, 3, 13, 15, 1, 20])
array_sorted = np.sort(array_unsorted)
print(array_sorted)
[ 1 3 5 10 13 15 20]
Exercise 5.1#
Please evaluate the cell below by running it:
some_array = np.random.randint(0,10, size=20)
In the cell below print the sorted version of the above array.
To concatenate two arrays i.e add one array to the end of another, we use np.concatenate:
array1 = np.array([2, 4, 7])
array2 = np.array([3, 5, 8])
concat_arrat = np.concatenate((array1, array2))
print(concat_arrat)
[2 4 7 3 5 8]
Exercise 5.2#
We wish to pad our 1d-array with zeros. This means that we wish to add zeros to the end of the array (add right padding).
In the cell below write a function has two inputs:
one-dimensional numpy array, an array of numbers,
an int, integer that represents the number of zeros that we wish to use in our padding.
You function should return a modified array that consists of the original input concatenated to the array of zeros.
Please test your function on a few examples.
We add an value by appending it to the end of an array. The function np.append creates a copy of an old array with the new values at its end.
old_array = np.array([1, 4, 5])
new_array = np.append(old_array, 99)
print(f"The old array is {old_array}")
print(f"The new array is {new_array}")
The old array is [1 4 5]
The new array is [ 1 4 5 99]
Exercise 5.3#
Write a function with three inputs:
A one-dimensional NumPy array (the first array)
Another one-dimensional NumPy array (the second array)
A float number
The function should add elements to the first array in the following way:
For each element in the second array:
Add that element to the end of the first array.
Then immediately add the float to the end of the first array.
In other words, for every element in the second array, you first append that element, then append the float.
Example:#
The first numpy array is
[1, 2, 3],The second numpy array is
[5, 7]The float is
99.
Then the output of the function should be [1, 2, 3, 5, 99, 7, 99].
### Deleting elements
To remove the value by specifying the index we use np.delete. This function creates a copy of the old array without the element on the specified index.
old_array = np.array([1, 4, 5, 99])
new_array = np.delete(old_array, 0)
print(f"The old array is {old_array}")
print(f"The new array is {new_array}")
The old array is [ 1 4 5 99]
The new array is [ 4 5 99]
Exercise 5.4#
Create a function that has one input
a one dimensional numpy array.
You function should use np.delete to create a copy of the original array without the first, middle and the last element. If there are two middle elements (if the size of the array is an even number), then remove them both.