How to create basic arrays - SOLVED
Contents
How to create basic arrays - SOLVED#
To create an array of a specified number of zeros we use:
import numpy as np # Run this cell or the numpy commands won't run
n = 5
array_of_zeros = np.zeros(n)
print(array_of_zeros)
[0. 0. 0. 0. 0.]
Exercise 1.1#
In the cell below create a numpy array that is filled with 50 zeros. Then print it.
array_zeros_2 = np.zeros(50)
print(array_zeros_2)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
To create an array of a specified number of ones we use:
m = 6
array_of_ones = np.ones(m)
print(array_of_ones)
[1. 1. 1. 1. 1. 1.]
Exercise 1.2#
In the cell below create a numpy array that is filled with 65 ones. Then print it.
array_ones_2 = np.ones(65)
print(array_ones_2)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
To create an array of a specified number of some other value we use:
r = 7
value = 2.
array_of_same_val = np.full(r, value)
print(array_of_same_val)
[2. 2. 2. 2. 2. 2. 2.]
Note that the first argument of np.full is its shape, and the second argument is the value that we wish to fill.
Exercise 1.3#
In the cell below create a numpy array that is filled with 40 occurences of a string “I’m only a clone”.
n = 40
s = "I am only a clone"
clone_array = np.full(n,s)
print(clone_array)
['I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone' 'I am only a clone' 'I am only a clone'
'I am only a clone']
Ranging values#
To create an array with elements to match the range function, so that we specify the start, stop and the step we use:
np.arange(start, stop, step)
start = 5
stop = 10
step = 2
print(np.arange(start, stop, step))
[5 7 9]
Exercise 1.4#
In the cell below create an array of consecutive 50 integers, starting from 100 (ie, [100,101,102, 103, …]. Use the np.arange function, not range. Then print the resulting array.
start = 100
stop = 150
step = 1
print(np.arange(start,stop,step))
[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 137 138 139 140 141 142 143 144 145 146 147 148 149]
Exercise 1.5#
In the cell below create an array of all even integers in the interval \([0, 100]\). Then print the resulting array.
start = 0
stop = 101
step = 2
stepped_array = np.arange(start,stop,step)
print(stepped_array)
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100]
Linear space#
To create an array with values that are spaced linearly in a specific interval, we use:
np.linspace(start_int, end_int, num_points)
start_int = 0
end_int = 10
num_points = 5
print(np.linspace(start_int, end_int, num_points))
[ 0. 2.5 5. 7.5 10. ]
Exercise 1.6#
In the cell below create an array of 100 equally spaced points from an interval \([0, 1]\). First find the start of the interval, that the end point of the interval, and then specify the number of equally spaced points.
start = 0
stop = 1
num_points =100
new_array = np.linspace(start,stop,num_points)
print(new_array)
[0. 0.01010101 0.02020202 0.03030303 0.04040404 0.05050505
0.06060606 0.07070707 0.08080808 0.09090909 0.1010101 0.11111111
0.12121212 0.13131313 0.14141414 0.15151515 0.16161616 0.17171717
0.18181818 0.19191919 0.2020202 0.21212121 0.22222222 0.23232323
0.24242424 0.25252525 0.26262626 0.27272727 0.28282828 0.29292929
0.3030303 0.31313131 0.32323232 0.33333333 0.34343434 0.35353535
0.36363636 0.37373737 0.38383838 0.39393939 0.4040404 0.41414141
0.42424242 0.43434343 0.44444444 0.45454545 0.46464646 0.47474747
0.48484848 0.49494949 0.50505051 0.51515152 0.52525253 0.53535354
0.54545455 0.55555556 0.56565657 0.57575758 0.58585859 0.5959596
0.60606061 0.61616162 0.62626263 0.63636364 0.64646465 0.65656566
0.66666667 0.67676768 0.68686869 0.6969697 0.70707071 0.71717172
0.72727273 0.73737374 0.74747475 0.75757576 0.76767677 0.77777778
0.78787879 0.7979798 0.80808081 0.81818182 0.82828283 0.83838384
0.84848485 0.85858586 0.86868687 0.87878788 0.88888889 0.8989899
0.90909091 0.91919192 0.92929293 0.93939394 0.94949495 0.95959596
0.96969697 0.97979798 0.98989899 1. ]
Differences and similarities between arange and linspace.#
Note the difference and similarities between np.arange and np.linspace.
With np.arange similarly to range function the last value is stop - step, while in the np.linspace the last value is end_int.
Unlike range np.arange allows the step to be a float; in range only ints are accepted for the step.
np.linspace finds the right step automatically, based on the number of points. In np.arange we specify the step that then determines the number of points.
Caution#
In some cases it is recommended to use np.linspace over np.arange. With steps being floats np.arange sometimes returns unexpected outputs due to a floating point error. See the example below:
np.arange(1.0, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
Due to a floating point error it includes 1.3, and does not follow the rule of the last element of an array being stop - step.
This issue can occur only if the step is a float.
NOTE both np.linspace and np.arange are very useful tools where “iterating” (or looping) over a range of values. Think again about the differences between both and consider which one is more appropriate for what you are doing!