2. Numpy basics

Numpy is a python module for scientific computing (linear algebra, Fourier transform, random number capabilities), and efficient handling of N-dimensional arrays. Have a closer look at https://numpy.org/.

2.1 Import numpy

First, we have to import the numpy module to our environment and, just to make it shorter, we want to use an abbreviation np instead of numpy.


In [2]:
import numpy as np

2.2 Numpy arrays

In addition to the capabilities of performing calculations with numpy, numpy's great strength is the use and the modification of arrays.

2.2.1 Create an array

In the chapter before you've learned how to create and use lists which can be used as a starting point to create a numpy array with np.array.


In [3]:
temp_list  = [10, 12, 14, 29, 18, 21]
temp_array = np.array(temp_list)

print('--> temp_list:  ',temp_list,type(temp_list))
print('--> temp_array: ',temp_array,type(temp_array))


--> temp_list:   [10, 12, 14, 29, 18, 21] <class 'list'>
--> temp_array:  [10 12 14 29 18 21] <class 'numpy.ndarray'>

2.2.2 Array attributes

Numpy provides attributes for the arrays to retrieve the dimension size, shape, size, data type, and data itself.


In [4]:
ndim  = temp_array.ndim
shape = temp_array.shape
size  = temp_array.size
dtype = temp_array.dtype

print('--> number of array dimensions: ', ndim)
print('--> shape  of array dimensions: ', shape)
print('--> size   of array dimensions: ', size)
print('--> dtype  of array dimensions: ', dtype)


--> number of array dimensions:  1
--> shape  of array dimensions:  (6,)
--> size   of array dimensions:  6
--> dtype  of array dimensions:  int64

You can convert the array from the given data type to another. Here, we want to convert it from integer to floating point values.


In [5]:
print('--> type(temp_array): ',type(temp_array))

print('--> temp_array.astype(float)): ',temp_array.astype(float))


--> type(temp_array):  <class 'numpy.ndarray'>
--> temp_array.astype(float)):  [10. 12. 14. 29. 18. 21.]

2.2.3 Indexing and slicing

Indexing and sclicing of arrays is equivalent to indexing and slicing of lists (see chapter 01).


In [6]:
print('--> temp_array[:]:    ', temp_array[:])
print('--> temp_array[1]):   ', temp_array[1])
print('--> temp_array[0:3]:  ', temp_array[0:3])
print('--> temp_array[:3]:   ', temp_array[:3])
print('--> temp_array[3:]:   ', temp_array[3:])
print('--> temp_array[-1]:   ', temp_array[-1])
print('--> temp_array[-3:-1]:', temp_array[-3:-1])


--> temp_array[:]:     [10 12 14 29 18 21]
--> temp_array[1]):    12
--> temp_array[0:3]:   [10 12 14]
--> temp_array[:3]:    [10 12 14]
--> temp_array[3:]:    [29 18 21]
--> temp_array[-1]:    21
--> temp_array[-3:-1]: [29 18]

2.3 Create and reshape arrays

To generate an evenly spaced array, let's say (0,1,2,3,4), you can use the np.arange function. np.range is similar to the range function but it creates an array instead of a list.

To create an array starting at 0 and have 5 elements you can use one single input value as parameter:


In [7]:
a_array = np.arange(5)
print(a_array)


[0 1 2 3 4]

To create an array giving the start, end, and increment value:

start value = 2
end value   = 10
increment   = 2

In [8]:
a_array = np.arange(2, 10, 2)
print(a_array)


[2 4 6 8]

To create an array with an increment of type float:


In [9]:
a_array = np.arange(2, 10, 1.5)
print(a_array)


[2.  3.5 5.  6.5 8.  9.5]

If you want to create an array on n-elements without knowing the exact increment the np.linspace function is what you are looking for. You have to give the start, end value, and the number of elements to be generated. For example

start value        = 1
end value          = 2
number of elements = 11

In [10]:
b_array = np.linspace(1, 2, num=11)
print(b_array)


[1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. ]

2.3.1 Reshape

So far we've only worked with one-dimensional arrays in this section, and we'll see how easy it is to convert them into two- or three-dimensional arrays.


In [11]:
a = np.arange(0, 12, 1,)
print('a:\n ', a)

a_2d = a.reshape(4, 3)
print('\na_2d:\n ', a_2d)

a_3d = a.reshape(2, 3, 2)
print('\na_3d:\n ', a_3d)


a:
  [ 0  1  2  3  4  5  6  7  8  9 10 11]

a_2d:
  [[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

a_3d:
  [[[ 0  1]
  [ 2  3]
  [ 4  5]]

 [[ 6  7]
  [ 8  9]
  [10 11]]]

Of course, you can convert an n-dimensional array to an one-dimensional array using the attribute flatten.


In [12]:
a_1d = a_3d.flatten()
print('a_1d: ', a_1d)


a_1d:  [ 0  1  2  3  4  5  6  7  8  9 10 11]

There is a numpy function doing this too, but it is called ravel.


In [13]:
a_1d = np.ravel(a_3d)
print('a_1d: ', a_1d)


a_1d:  [ 0  1  2  3  4  5  6  7  8  9 10 11]

Adding or multiplying arrays of the same size is simple. We define two arrays, each has 5 elements, and compute the sum and the product of them.


In [14]:
m = np.array([2.1, 3.0, 4.7, 5.3, 6.2])
n = np.arange(5)
print('m = ', m)
print('n = ', n)

mn_add = m + n
mn_mul = m * n

print('m + n = ', mn_add)
print('m * n = ', mn_mul)


m =  [2.1 3.  4.7 5.3 6.2]
n =  [0 1 2 3 4]
m + n =  [ 2.1  4.   6.7  8.3 10.2]
m * n =  [ 0.   3.   9.4 15.9 24.8]

2.4 Mathematical attributes and functions

The attributes of a numpy array can be used to retrieve the minimum or maximum value, or to compute the sum, mean, or standard deviation of arrays.


In [15]:
m_min   = m.min()
m_max   = m.max()
m_sum   = m.sum()
m_mean  = m.mean()
m_std   = m.std()
m_round = m.round()

print('m_min:   ',m_min)
print('m_max:   ',m_max)
print('m_sum:   ',m_sum)
print('m_mean:  ',m_mean)
print('m_std:   ',m.std())
print('m_round: ',m.round())


m_min:    2.1
m_max:    6.2
m_sum:    21.3
m_mean:   4.26
m_std:    1.502797391533536
m_round:  [2. 3. 5. 5. 6.]

Numpy also provides many mathematical routines, see https://docs.scipy.org/doc/numpy/reference/routines.math.html.


In [16]:
m_sqrt = np.sqrt(m)
m_exp  = np.exp(m)
mn_add = np.add(m,n)

print('m_sqrt: ', m_sqrt)
print('m_exp:  ', m_exp)
print('mn_add: ', mn_add)


m_sqrt:  [1.44913767 1.73205081 2.16794834 2.30217289 2.48997992]
m_exp:   [  8.16616991  20.08553692 109.94717245 200.33680997 492.74904109]
mn_add:  [ 2.1  4.   6.7  8.3 10.2]

In [17]:
data_radians = np.linspace(0., 6., 5)
print('data_radians: ', data_radians)

data_sin = np.sin(data_radians)
print('data_sin:     ', data_sin)

data_cos = np.cos(data_radians)
print('data_cos:     ', data_cos)

data_degrees = np.degrees(data_sin)
print('data_degrees: ', data_degrees)


data_radians:  [0.  1.5 3.  4.5 6. ]
data_sin:      [ 0.          0.99749499  0.14112001 -0.97753012 -0.2794155 ]
data_cos:      [ 1.          0.0707372  -0.9899925  -0.2107958   0.96017029]
data_degrees:  [  0.          57.15225282   8.08558087 -56.00835009 -16.00932878]

2.5 Creating arrays for masking

Being able to create arrays containing only zeros ore only ones can be very helpful if you want to mask your data. Numpy provides the functions np.zeros and np.ones.


In [18]:
zeros = np.zeros((3,4))
print('zeros: ',zeros)

zeros = np.zeros((3,4),dtype=int)
print('zeros type integer: ',zeros)

ones = np.ones((3,4))
print('ones: ',ones)

ones = np.ones((3,4),dtype=int)
print('ones type integer: ',ones)


zeros:  [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
zeros type integer:  [[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
ones:  [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
ones type integer:  [[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

2.5 Array stacking

Numpy gives you the possibilities to stack arrays on different axes using the functions np.vstack and hstack.


In [19]:
v = np.vstack((zeros,ones))
h = np.hstack((zeros,ones))

print('np.vstack(zeros,ones): \n', v)
print('np.hstack(zeros,ones): \n', h)


np.vstack(zeros,ones): 
 [[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
np.hstack(zeros,ones): 
 [[0 0 0 0 1 1 1 1]
 [0 0 0 0 1 1 1 1]
 [0 0 0 0 1 1 1 1]]

2.6 Shallow and deep copy

Maybe you have recognized that copying an array a doen't mean to get a physical copy. If you change your copy the origin array will be changed to because it isn't a physical copy it is more or less a link to the origin array. It's called a shallow copy.


In [20]:
a_origin = np.arange(12).reshape(3,4)
print('a_origin: \n', a_origin)

b_copy_of_a = a_origin

b_copy_of_a[1,3] = 999

print('b_copy_of_a: ', b_copy_of_a)
print('a_origin:    ', a_origin)


a_origin: 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
b_copy_of_a:  [[  0   1   2   3]
 [  4   5   6 999]
 [  8   9  10  11]]
a_origin:     [[  0   1   2   3]
 [  4   5   6 999]
 [  8   9  10  11]]

To create a physical copy, so called deep copy, you have to use numpy's np.copy function.


In [21]:
a_origin = np.arange(12).reshape(3,4)

c_deep_copy = a_origin.copy()

c_deep_copy[1,3] = 222

print('a_origin: \n', a_origin)
print('c_deep_copy: \n', c_deep_copy)


a_origin: 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
c_deep_copy: 
 [[  0   1   2   3]
 [  4   5   6 222]
 [  8   9  10  11]]

2.7 Most useful functions

Working with arrays 1- or multiple-dimensional arrays makes the programs sometimes slow when you have to find values in given ranges or to change values to set missing values for invalid data. Usually, you would thinl about a for or while loop to go through all elements of an array, but numpy has efficient functions to do it in just one line.

Useful functions

np.where
np.argwhere
np.all
np.any

The np.where function allows you to look at the array using a logical expression. If it is True than let the value untouched but when it is False change it to the given value, maybe a kind of a missing value, but this is NOT the same as a netCDF missing value (see below masked arrays).


In [22]:
x = np.array([-1, 2, 0, 5, -3, -2])

x_ge0 = np.where(x >= 0, x, -9999)

print('x: ', x)
print('x_ge0: ', x_ge0)


x:  [-1  2  0  5 -3 -2]
x_ge0:  [-9999     2     0     5 -9999 -9999]

In the upper example the values of the array were located and directly changed when the condition is False. But sometimes you want to retrieve the indices of the values instead the values themselves, because you need the same indices later again. Then use the np.argwhere function with a logical condition.


In [23]:
x_ind = np.argwhere(x < 0)

print('--> indices x >= 0: \n', x_ind)

y = x

y[x_ind] = -9999

print('y[x_ind] where x >= 0: \n', y)


--> indices x >= 0: 
 [[0]
 [4]
 [5]]
y[x_ind] where x >= 0: 
 [-9999     2     0     5 -9999 -9999]

To see if the values of an array are less than 0 for instance you can't try do it like below - ah, no that would be too easy.

if(x < 0):
   print('some elements are less 0')
else:
   print('no values are less 0')

if(x > 0):
   print('all elements are greater than 0')
else:
   print('not all values are greater than 0')

The result would be the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-6f0311fb54a3> in <module>
----> 1 if(x < 0):
      2    print('some elements are less 0')
      3 else:
      4    print('no values are less 0')
      5 

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The last line of the error message gives you the right hint to use the array functions any or all.


In [24]:
if(x.any() < 0):
   print('some elements are less 0')
else:
   print('no values are less 0')

if(x.all() > 0):
   print('all elements are greater than 0')
else:
   print('not all values are greater than 0')


no values are less 0
not all values are greater than 0

Or you can use the numpy function np.any or np.all.


In [25]:
if(np.any(x < 0)):
   print('some elements are less 0')
else:
   print('no values are less 0')

if(np.all(x > 0)):
   print('all elements are greater than 0')
else:
   print('not all values are greater than 0')


some elements are less 0
not all values are greater than 0

2.8 Read data from CSV file

Numpy also provides functions to read data from files. Our next example shows how to read data from a CSV file. The input CSV file pw.dat looks like

 ID      LAT      LON      PW
BLAC    36.75   -97.25    48.00
BREC    36.41   -97.69    46.30
BURB    36.63   -96.81    49.80
...

We want to read all values, and because the mixed data types in the file, we read all as strings. We don't need the header line so we skip it.


In [26]:
lines = np.loadtxt('/Users/k204045/data/Station_data/pw.dat', dtype='str', skiprows=1)
ID  = lines[:,0]
lat = lines[:,1]
lon = lines[:,2]
pw  = lines[:,3]

print('ID:  \n', ID)
print('lat: \n', lat)
print('lon: \n', lon)
print('pw:  \n', pw)


ID:  
 ['BLAC' 'BREC' 'BURB' 'DQUA' 'FBYN' 'GUTH' 'HBRK' 'HKLO' 'JTNT' 'LMNO'
 'LTHM' 'MEDF' 'NDS1' 'OILT' 'PRCO' 'REDR' 'RWDN' 'SA14' 'SG01' 'SG09'
 'SG10' 'SG11' 'SG12' 'SG13' 'SG14' 'SG15' 'SG16' 'SG18' 'SG19' 'SG20'
 'SG22' 'VCIO']
lat: 
 ['36.75' '36.41' '36.63' '34.11' '40.08' '35.85' '38.31' '35.68' '33.02'
 '36.69' '39.58' '36.79' '37.30' '36.03' '34.98' '36.36' '40.09' '36.56'
 '36.60' '36.43' '36.88' '37.33' '38.20' '38.12' '37.84' '38.20' '37.38'
 '34.88' '35.36' '35.56' '35.26' '36.07']
lon: 
 ['-97.25' '-97.69' '-96.81' '-94.29' '-97.31' '-97.48' '-97.29' '-95.86'
 '-100.98' '-97.48' '-94.17' '-97.75' '-95.60' '-96.50' '-97.52' '-97.15'
 '-100.65' '-100.61' '-97.49' '-98.28' '-98.29' '-99.31' '-99.32' '-97.51'
 '-97.02' '-95.59' '-96.18' '-98.20' '-98.98' '-98.02' '-97.48' '-99.22']
pw:  
 ['48.00' '46.30' '49.80' '45.00' '38.20' '46.60' '34.30' '50.20' '39.80'
 '47.10' '40.40' '46.70' '39.90' '48.80' '46.50' '50.40' '26.40' '22.40'
 '49.00' '43.20' '41.40' '33.60' '33.90' '36.30' '40.20' '31.70' '42.80'
 '45.90' '45.30' '47.90' '46.80' '38.60']

If you don't need the IDs then you can directly read the lat, lon, and pw values using the usecols parameter. There is no need to use the dtype parameter anymore because the default data type is float, and that's what we want.


In [27]:
data = np.loadtxt('/Users/k204045/data/Station_data/pw.dat', usecols=(1,2,3), skiprows=1)

print()
print('--> data: \n', data)
print()
print('--> lon:  \n', data[:,0])
print('--> lon:  \n', data[:,1])
print('--> pw:   \n', data[:,2])


--> data: 
 [[  36.75  -97.25   48.  ]
 [  36.41  -97.69   46.3 ]
 [  36.63  -96.81   49.8 ]
 [  34.11  -94.29   45.  ]
 [  40.08  -97.31   38.2 ]
 [  35.85  -97.48   46.6 ]
 [  38.31  -97.29   34.3 ]
 [  35.68  -95.86   50.2 ]
 [  33.02 -100.98   39.8 ]
 [  36.69  -97.48   47.1 ]
 [  39.58  -94.17   40.4 ]
 [  36.79  -97.75   46.7 ]
 [  37.3   -95.6    39.9 ]
 [  36.03  -96.5    48.8 ]
 [  34.98  -97.52   46.5 ]
 [  36.36  -97.15   50.4 ]
 [  40.09 -100.65   26.4 ]
 [  36.56 -100.61   22.4 ]
 [  36.6   -97.49   49.  ]
 [  36.43  -98.28   43.2 ]
 [  36.88  -98.29   41.4 ]
 [  37.33  -99.31   33.6 ]
 [  38.2   -99.32   33.9 ]
 [  38.12  -97.51   36.3 ]
 [  37.84  -97.02   40.2 ]
 [  38.2   -95.59   31.7 ]
 [  37.38  -96.18   42.8 ]
 [  34.88  -98.2    45.9 ]
 [  35.36  -98.98   45.3 ]
 [  35.56  -98.02   47.9 ]
 [  35.26  -97.48   46.8 ]
 [  36.07  -99.22   38.6 ]]

--> lon:  
 [36.75 36.41 36.63 34.11 40.08 35.85 38.31 35.68 33.02 36.69 39.58 36.79
 37.3  36.03 34.98 36.36 40.09 36.56 36.6  36.43 36.88 37.33 38.2  38.12
 37.84 38.2  37.38 34.88 35.36 35.56 35.26 36.07]
--> lon:  
 [ -97.25  -97.69  -96.81  -94.29  -97.31  -97.48  -97.29  -95.86 -100.98
  -97.48  -94.17  -97.75  -95.6   -96.5   -97.52  -97.15 -100.65 -100.61
  -97.49  -98.28  -98.29  -99.31  -99.32  -97.51  -97.02  -95.59  -96.18
  -98.2   -98.98  -98.02  -97.48  -99.22]
--> pw:   
 [48.  46.3 49.8 45.  38.2 46.6 34.3 50.2 39.8 47.1 40.4 46.7 39.9 48.8
 46.5 50.4 26.4 22.4 49.  43.2 41.4 33.6 33.9 36.3 40.2 31.7 42.8 45.9
 45.3 47.9 46.8 38.6]

However, we want to have the IDs, too. It's never too late... But again, we have to tell numpy to use the data type string.


In [28]:
IDs = np.loadtxt('/Users/k204045/data/Station_data/pw.dat', dtype='str', usecols=(0), skiprows=1)

print('IDs: \n', IDs)


IDs: 
 ['BLAC' 'BREC' 'BURB' 'DQUA' 'FBYN' 'GUTH' 'HBRK' 'HKLO' 'JTNT' 'LMNO'
 'LTHM' 'MEDF' 'NDS1' 'OILT' 'PRCO' 'REDR' 'RWDN' 'SA14' 'SG01' 'SG09'
 'SG10' 'SG11' 'SG12' 'SG13' 'SG14' 'SG15' 'SG16' 'SG18' 'SG19' 'SG20'
 'SG22' 'VCIO']

2.9 Masking

In our daily work we are confronted with data which we want to mask to see only the values in sections we need. Masking is sometimes tricky and you have to take care.

In the following example we try to demonstrate how to mask a 2-dimensional array by a given mask array containing zeros and ones, where 0 means 'don't mask', and 1 means 'mask'.


In [29]:
field = np.arange(1,9,1).reshape((4,2))

mask = np.array([[0,0],[1,0],[1,1],[1,0]])

mask_field = np.ma.MaskedArray(field,mask)

print('field:      \n', field)
print('mask:       \n', mask)
print('mask_field: \n', mask_field)


field:      
 [[1 2]
 [3 4]
 [5 6]
 [7 8]]
mask:       
 [[0 0]
 [1 0]
 [1 1]
 [1 0]]
mask_field: 
 [[1 2]
 [-- 4]
 [-- --]
 [-- 8]]

For the next example we want to get data from one array depending on data of a second array.

To create two arrays with random data of type integer we use numpy's random generator.


In [30]:
A = np.random.randint(-3, high=5, size=10)
B = np.random.randint(-4, high=4, size=10)

print('--> A: \n', A)
print('--> B: \n', B)


--> A: 
 [ 2 -1  3 -2 -3 -2  1  0  4  4]
--> B: 
 [ 0  0 -1 -2  2 -2 -1 -3  3 -1]

Now, we want only the values of array A which are

  1. greater equal array B values
  2. less than array B values

First, we have to find the indices of those values. Numpy has routines to do that for us, presupposed that both arrays are of the same shape.


In [31]:
ind_ge = list(np.greater_equal(A,B))
ind_lt = list(np.less(A,B))

print('--> ind_ge: \n', ind_ge)
print('--> ind_lt: \n', ind_lt)


--> ind_ge: 
 [True, False, True, True, False, True, True, True, True, True]
--> ind_lt: 
 [False, True, False, False, True, False, False, False, False, False]

This is the same as


In [32]:
ind_ge = list(A>=B)
ind_lt = list(A<B)

print('--> ind_ge: \n', ind_ge)
print('--> ind_lt: \n', ind_lt)


--> ind_ge: 
 [True, False, True, True, False, True, True, True, True, True]
--> ind_lt: 
 [False, True, False, False, True, False, False, False, False, False]

Use these indices to get the data we want.


In [33]:
A_ge = A[ind_ge]
A_lt = A[ind_lt]

print('--> A_ge: \n', A_ge)
print('--> A_lt: \n', A_lt)


--> A_ge: 
 [ 2  3 -2 -2  1  0  4  4]
--> A_lt: 
 [-1 -3]

In this case we get only the values of the array A which conforms the condition, and not the complete masked array. This has to be done with the numpy.ma.MaskedArray.


In [34]:
A_ge2 = np.ma.MaskedArray(A, ind_ge)
A_lt2 = np.ma.MaskedArray(A, ind_lt)

print('--> A_ge2: \n', A_ge2)
print('--> A_lt2: \n', A_lt2)


--> A_ge2: 
 [-- -1 -- -- -3 -- -- -- -- --]
--> A_lt2: 
 [2 -- 3 -2 -- -2 1 0 4 4]

In [35]:
print(type(A_ge))
print(type(A_ge2))


<class 'numpy.ndarray'>
<class 'numpy.ma.core.MaskedArray'>

Here is just a brief example in order to locate all the values of an array that are not equal to zero. Of course, it also shows how to locate the values equal to zero.

Count and locate the non zero values of an array, select them, and mask the array to save the shape of the array.


In [36]:
C = np.random.randint(-2, high=1, size=10)

C_count_nonzero = np.count_nonzero(C)
C_nonzero_ind   = np.nonzero(C)

C2 = C[C_nonzero_ind]

C3 = np.ma.MaskedArray(C, C==0)

print('--> C:               ', C)
print('--> C_count_nonzero: ', C_count_nonzero)
print('--> C_nonzero_ind:   ', C_nonzero_ind)
print('--> C2:              ', C2)
print('--> C3:              ', C3)


--> C:                [ 0 -1  0 -2  0 -2  0 -2  0  0]
--> C_count_nonzero:  4
--> C_nonzero_ind:    (array([1, 3, 5, 7]),)
--> C2:               [-1 -2 -2 -2]
--> C3:               [-- -1 -- -2 -- -2 -- -2 -- --]

Now, we look at the zeros.

Count and locate the zero values of an array, select them, and mask the array to save the shape of the array.


In [37]:
Z_count_zero = np.count_nonzero(C==0)

Z_zero_ind = np.argwhere(C==0)

Z = C[Z_zero_ind]

Z2 = np.ma.MaskedArray(C, C!=0)

print('--> Z_count_zero: ', Z_count_zero)
print('--> Z_zero_ind:   ', Z_zero_ind.flatten())
print('--> Z:            ', Z.flatten())
print('--> Z2:           ', Z2)


--> Z_count_zero:  6
--> Z_zero_ind:    [0 2 4 6 8 9]
--> Z:             [0 0 0 0 0 0]
--> Z2:            [0 -- 0 -- 0 -- 0 -- 0 0]

2.10 Some hints

You've learned to generate random arrays, arrays of zeros or ones but one important array generation ist still missing - the array with missing values.

To generate an array which contains only missing values use the numpy routine np.full.


In [38]:
empty_array = np.full(100,1.0e20)
print(empty_array)


[1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20
 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20 1.e+20]

In some cases it is more efficient to start with an empty (missing value) array.


In [ ]: