2.1. Exercises

Make that Array

np.ones((4, 3))

Shape, Size, and Dimension

array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15]])
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]]])

More Arrays Questions

Use the following array named hurray to answer the next set of questions.

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
# Question 1
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
# Question 2
array([[10, 11],
       [13, 14]])
# Question 3
array([[ 0,  3,  6,  9, 12, 15],
       [ 1,  4,  7, 10, 13, 16],
       [ 2,  5,  8, 11, 14, 17]])

Coding questions

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Making an Array

Let’s make an array and find its size and dimension.

Tasks:

  • Create an array named arr1 that contains only elements with values 1 and a shape of (3,5).
  • Save the dimension and size of arr1 in objects named arr1_dim and arr1_size respectively.
Hint 1
  • Are you using .ones((3,5))?
  • Are you using .ndim and .size?
  • Are you saving your objects as the correct names?
Fully worked solution:


Array Practice

Let’s make a new array and transform it by slicing and transposing.

Tasks:

  • Create an array named arr2 using np.linspace() with 6 equally spaced values from 1 to 16 and a shape of (2,3) (You’ll need .reshape() for this!).
  • Transpose the array and name it arr2t.
  • Finally, slice the new object arr2t so it only includes the values 7 and 16. Save this as an object named sliced_arr2t.
Hint 1
  • Are you using .reshape() to change the dimension of np.linspace()?
  • Are you using .T?
  • Are you slicing with [:,1]?
Fully worked solution: