nasties = randoms[:12].copy()
nasties[0] *= 10000
nasties[1] /= 10000
nasties[3] = float('inf')
nasties[4] = float('-inf')
nasties[5] = float('nan')
nasties = nasties.reshape((2,6))๐งพ View as a summary
Pretty printing
lovely
def lovely(
x:Union, # The data you want to explore
plain:bool=False, # Plain old way
verbose:bool=False, # Both summaty and plain
depth:int=0, # Show deeper summary, up to `depth`
lvl:int=0, # Indentation level
color:Optional=None, # Override `get_config().color`
show_histogram:Optional=None, # Show histogram
)->str: # The summary
Pretty-print the stats of a numpy array or scalar
Examples
print(lovely(nasties))array[2, 6] n=12 xโ[-0.151, 1.764e+04] ฮผ=1.960e+03 ฯ=5.544e+03 +Inf! -Inf! NaN!
print(lovely(randoms[0]))
print(lovely(randoms[:2]))
print(lovely(randoms[:6].reshape(2, 3))) # More than 2 elements -> show statistics
print(lovely(randoms[:11])) # More than 10 -> don't show values1.764
array[2] ฮผ=1.082 ฯ=0.682 [1.764, 0.400]
array[2, 3] n=6 xโ[-0.977, 2.241] ฮผ=1.046 ฯ=1.090 [[1.764, 0.400, 0.979], [2.241, 1.868, -0.977]]
array[11] xโ[-0.977, 2.241] ฮผ=0.684 ฯ=0.938
Do we have any floating point nasties? Are the values all zeros?
# Statistics and range are calculated on good values only, if there are at lest 3 of them.
print(lovely(nasties))array[2, 6] n=12 xโ[-0.151, 1.764e+04] ฮผ=1.960e+03 ฯ=5.544e+03 +Inf! -Inf! NaN!
print(lovely(nasties, color=False))array[2, 6] n=12 xโ[-0.151, 1.764e+04] ฮผ=1.960e+03 ฯ=5.544e+03 +Inf! -Inf! NaN!
print(lovely(np.array([float("nan")]*11)))array[11] NaN!
print(lovely(np.zeros(12, dtype=np.float16)))
print(lovely(np.array([], dtype=int)))array[12] f16 all_zeros
array[0] i64 empty
str(lovely(np.array([], dtype=int)))'array[0] i64 \x1b[38;2;127;127;127mempty\x1b[0m'
np.set_printoptions(precision=3)
print(lovely(nasties, verbose=True))array[2, 6] n=12 xโ[-0.151, 1.764e+04] ฮผ=1.960e+03 ฯ=5.544e+03 +Inf! -Inf! NaN!
array([[ 1.764e+04, 4.002e-05, 9.787e-01, inf, -inf,
nan],
[ 9.501e-01, -1.514e-01, -1.032e-01, 4.106e-01, 1.440e-01,
1.454e+00]])
print(lovely(nasties, plain=True))array([[ 1.764e+04, 4.002e-05, 9.787e-01, inf, -inf,
nan],
[ 9.501e-01, -1.514e-01, -1.032e-01, 4.106e-01, 1.440e-01,
1.454e+00]])
image = np.load("mysteryman.npy")
image[1,100,100] = float('nan')
print(lovely(image, depth=1))array[3, 196, 196] f32 n=115248 (450 KiB) xโ[-2.118 |โโ
โโโโโโโโ| 2.640] ฮผ=-0.388 ฯ=1.073 NaN!
array[196, 196] f32 n=38416 xโ[-2.118 |โโโ
โโโโโโโ| 2.249] ฮผ=-0.324 ฯ=1.036
array[196, 196] f32 n=38416 xโ[-1.966 |โโโ
โโโโโโโ| 2.429] ฮผ=-0.274 ฯ=0.973 NaN!
array[196, 196] f32 n=38416 xโ[-1.804 |โโโโโโโโโโ| 2.640] ฮผ=-0.567 ฯ=1.178
big_data = np.random.default_rng(1337).standard_normal((1024, 1024, 128), dtype=np.float32)print(lovely(big_data)) # Takes ~1s to plot. Acceptable.array[1024, 1024, 128] f32 n=134217728 (512 MiB) xโ[-5.626 | โโโโโโโโ | 5.798] ฮผ=-2.046e-05 ฯ=1.000
print(lovely(big_data, depth=1)) # Still ~same time.array[1024, 1024, 128] f32 n=134217728 (512 MiB) xโ[-5.626 | โโโโโโโโ | 5.798] ฮผ=-2.046e-05 ฯ=1.000
array[1024, 128] f32 n=131072 xโ[-4.462 |โโโโโโโ
โโโ| 4.006] ฮผ=0.005 ฯ=0.998
array[1024, 128] f32 n=131072 xโ[-3.986 |โโโโ
โโโโโโ| 4.226] ฮผ=-0.000 ฯ=0.999
array[1024, 128] f32 n=131072 xโ[-4.110 |โโโโ
โโโโโโ| 4.495] ฮผ=0.006 ฯ=1.004
array[1024, 128] f32 n=131072 xโ[-4.674 | โโโโโโ
โโโ| 4.333] ฮผ=-0.001 ฯ=0.999
array[1024, 128] f32 n=131072 xโ[-4.513 |โโโโโโโ
โโโ| 4.308] ฮผ=-0.001 ฯ=0.998
array[1024, 128] f32 n=131072 xโ[-4.588 |โโโโโโโ
โโโ| 4.318] ฮผ=0.005 ฯ=1.002
array[1024, 128] f32 n=131072 xโ[-4.308 |โโโโ
โโโโโโ| 4.482] ฮผ=0.000 ฯ=0.999
array[1024, 128] f32 n=131072 xโ[-4.388 |โโโโโโโ
โโโ| 4.200] ฮผ=0.004 ฯ=1.001
array[1024, 128] f32 n=131072 xโ[-4.958 |โโโโโโโโโโ| 4.814] ฮผ=0.002 ฯ=1.001
...
# We don't really supposed complex numbers yet
c = np.random.randn(2) + 1j*np.random.randn(2)
print(lovely(c))array([ 0.838-1.07j , -1.49 -0.209j])
# Other weirs stuff
w = np.array(["a", "b", "c"])
print(lovely(w))
z = np.array([{}, {"a": 1}, {"b": 2, "c": 3}])
print(lovely(z))array(['a', 'b', 'c'], dtype='<U1')
array([{}, {'a': 1}, {'b': 2, 'c': 3}], dtype=object)
i = np.array([1, 2, 3])
test_eq(str(lovely(i)), "array[3] i64 xโ[1, 3] ฮผ=2.000 ฯ=0.816 [1, 2, 3]")i = np.array([True, False, True])
test_eq(str(lovely(i)), 'array[3] bool xโ[False, True] ฮผ=0.667 ฯ=0.471 [True, False, True]')
i = np.array([False]*20 + [True]*40)
test_eq(str(lovely(i)), 'array[60] bool xโ[False |โโ| True] ฮผ=0.667 ฯ=0.471')