Sorting, adding and removing elements from an array - SOLVED#

To quickly sort the numbers of an array in ascending order we use np.sort function:

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:

some_array = np.random.randint(0,10, size=20)
print(some_array)
[5 1 9 1 5 0 1 9 2 4 4 8 7 2 7 4 6 9 9 3]

In the cell below print the sorted version of the above array.

print(np.sort(some_array))
[0 1 1 1 2 2 3 4 4 4 5 5 6 7 7 8 9 9 9 9]

To concatenate two arrays 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.

def pad_array(arr,N):
  pad_zeros = np.zeros(N)
  concat_array = np.concatenate((arr, pad_zeros))
  return concat_array
an_array = np.linspace(2.3,4.3,5)
padded_arr = pad_array(an_array,5)
print(padded_arr)
[2.3 2.8 3.3 3.8 4.3 0.  0.  0.  0.  0. ]

We add an value by appending it to the end of an array. Funcion 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 that has three inputs

  • first one dimensional numpy array, an array with numbers,

  • second one dimensional numpy array, an array with numbers,

  • a float.

This function should add an element of the second numpy array to the first one, then add a float to the first numpy array. You should iterate over all elements of the second numpy array add it to the first and in the same iteration add the float.

Example:

  • first numpy array is [1, 2, 3],

  • second numpy array is [5, 7]

  • a float is 99.

Then the output of the function should be [1, 2, 3, 5, 99, 7, 99].

def arr_comb(arr1, arr2, f):
  for item in arr2:
    arr1 = np.append(arr1,item)
    arr1 = np.append(arr1,f)
  return arr1

a1 = np.array([1, 2, 3])
a2 = np.array([5, 7])

a3 = arr_comb(a1,a2,99)
print(a3)
[ 1  2  3  5 99  7 99]

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.

def del_array(my_array):
  n = len(my_array)
  d1 = n//2
  if n%2 == 0:
    d2 = d2-1
    trimmed_array = np.delete(my_array,[0,d2,d1,-1])
  else:
    trimmed_array = np.delete(my_array,[0,d1,-1])
  return trimmed_array

t_Arr = np.array([1,2,3,4,5,6,7,8,9])
print(del_array(t_Arr))
[2 3 4 6 7 8]