Numpy discrete note

Anh-Thi Dinh

Import library

1import numpy as np

Checking

Isinstance

1# Check if an object is an instance numpy array?
2type(MyObect).__module__ == np.__name__

Comparing

Compare 2 dicts of multiple objects:
1# 2 dicts of multiple objects
2def _compare_two_dict(dct_1, dct_2):
3    return np.array([(lambda key: (dct_1[key] == dct_2[key]).all()
4       if type(dct_1[key]).__module__ == np.__name__
5       else dct_1[key] == dct2[key])(key)
6       for key in dct_1]).all()
1# 2 numpy arrays containing `np.nan`
2def nan_equal(a,b):
3    try:
4        np.testing.assert_equal(a,b)
5    except AssertionError:
6        return False
7    return True
8
9# if using in a pytest
10# instead of `assert np.testing`,
11# just use
12np.testing.assert_equal(a,b)

Count 0, NaNs

1# count nans
2arr = [1,2,3,4, np.nan,np.nan, 0,0,0]
3np.isnan(arr).sum() # 2
1# count non-nans
2arr = [1,2,3,4, np.nan,np.nan, 0,0,0]
3np.count_nonzero(~np.isnan(arr)) # 7
1# count non-zeros
2arr = [1,2,3,4, np.nan,np.nan, 0,0,0]
3np.count_nonzero(arr) # 6

Creating

Random numbers

1# random int between 0, 5
2np.random.randint(5)
3
4# random int between [1, 100]
5np.random.randint(1, 100 + 1)
1# random array of int between 1, 5
2np.random.randint(1,5,size=(2,3))
3
4# random array of int between 0, 3
5np.random.randint(3,size=(2,3))
1# random float number between [0, 1)
2np.random.random()
1# random float number between [a, b)
2(b - a)*np.random.random() + a
1# array of random between [0, 1)
2np.random.random_sample((5,)) # size: 5x1
1# array of random between (a, b)
2(b - a)*np.random.random_sample((5,1)) + a

Equal size

Create evenly spaced numbers over a specified interval (ref)
1x = np.linspace(0, 3.5, num=20) # default num = 50
Range of int numbers
1np.arange(0, 5)
2np.arange(0, 31, 5)
1array([0, 1, 2, 3, 4])
2array([ 0,  5, 10, 15, 20, 25, 30])

Indexes and values in other arrays

Create an array from nested arrays. Values in array_2 are indexes in array_1 and we create a new array take values in array_1 which is corresponding to its indexes showed in array_2.
1array_1 = np.array([ [0,0,0], [1,1,1], [2,2,2] ])
2array_2 = np.array([1,0,2,1,0,2,1]) # indexes in array_1
3array_3 = array_1[array_2]
4
5print(array_1)
6print(array_2)
7print(array_3)
1[[0 0 0]
2 [1 1 1]
3 [2 2 2]]
4
5[1 0 2 1 0]
6
7[[1 1 1]
8 [0 0 0]
9 [2 2 2]
10 [1 1 1]
11 [0 0 0]]

Array of NaN values

1# single array
2np.repeat(np.nan, 5)
3
4# multi dimensional arrays
5a = np.empty((2,3))
6a[:] = np.nan
7# other way
8np.repeat([np.repeat(np.nan, 3)], 2, axis=0)
1array([nan, nan, nan, nan, nan])
2
3array([[nan, nan, nan],
4       [nan, nan, nan]])

Repeated values

1np.full((5,), 50) # 5x1 of 5

Deleting

1# DELETE BY POSITIONS
2arr = np.arange(6)
3np.delete(arr, [3,4])
1array([0, 1, 2, 3, 4, 5])
2array([0, 1, 2, 5])
1# DELETE BY VALUES
2a = np.array([1, 2, 3, 4, 5, 6])
3np.delete(a, np.where(a == 3))
4
5b = np.array([1, 2, 3, 'a'])
6np.delete(a, np.where(b == 'a'))
7
8# OR
9a[a!=3]
10b[b!='a']
1array([1, 2, 4, 5, 6])
2
3array(['1', '2', '3'], dtype='<U21')
 
1# Remove 'NaT' from an array
2Y = np.array([600000, 300000, 'NaT'],
3              dtype='timedelta64[ns]')
4Y[~np.isnat(Y)]
1# output
2array([600000, 300000],
3       dtype='timedelta64[ns]')