Homework: Functions and NumPy¶
Functions¶
In [1]:
def splicer(iter, sep='_'):
try:
parts = map(str, iter)
return sep.join(parts)
except Exception as e:
print('Was not able to convert all inputs to strings!')
raise e
In [2]:
assert splicer(['foo', 'bar', 'baz'], '.') == 'foo.bar.baz'
In [3]:
assert splicer(['foo', 'bar', 'baz']) == 'foo_bar_baz'
In [4]:
assert splicer(['a', [1, 2, 3], 'b']) == 'a_[1, 2, 3]_b'
In [5]:
class Failer:
def __str__(self):
pass
test_obj = Failer()
In [6]:
splicer(('foo', test_obj, 'baz'))
Was not able to convert all inputs to strings!
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 1 ----> 1 splicer(('foo', test_obj, 'baz')) Cell In[1], line 7, in splicer(iter, sep) 5 except Exception as e: 6 print('Was not able to convert all inputs to strings!') ----> 7 raise e Cell In[1], line 4, in splicer(iter, sep) 2 try: 3 parts = map(str, iter) ----> 4 return sep.join(parts) 5 except Exception as e: 6 print('Was not able to convert all inputs to strings!') TypeError: __str__ returned non-string (type NoneType)
NumPy¶
In [7]:
import numpy as np
np.random.seed(12345)
In [8]:
diag_mat = np.diag(np.arange(1, 11))
print(diag_mat)
[[ 1 0 0 0 0 0 0 0 0 0] [ 0 2 0 0 0 0 0 0 0 0] [ 0 0 3 0 0 0 0 0 0 0] [ 0 0 0 4 0 0 0 0 0 0] [ 0 0 0 0 5 0 0 0 0 0] [ 0 0 0 0 0 6 0 0 0 0] [ 0 0 0 0 0 0 7 0 0 0] [ 0 0 0 0 0 0 0 8 0 0] [ 0 0 0 0 0 0 0 0 9 0] [ 0 0 0 0 0 0 0 0 0 10]]
In [9]:
diag_mat
Out[9]:
array([[ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 3, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 4, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 5, 0, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 6, 0, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 7, 0, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 8, 0, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 9, 0], [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]])
In [10]:
rand_mat = np.random.randn(3, 4, 7)
rand_mat.shape
Out[10]:
(3, 4, 7)
In [11]:
sub_mat = rand_mat[:, [0, 2]]
sub_mat.shape
Out[11]:
(3, 2, 7)
The entries are read out in order of dimension: across rows, down columns, then back through "sheets." Put another way, all the entries with the same first index are read out in order, then eventually all the entries with the same second index, and so on.
In [12]:
print(sub_mat)
print(sub_mat.flatten())
[[[-2.04707659e-01 4.78943338e-01 -5.19438715e-01 -5.55730304e-01 1.96578057e+00 1.39340583e+00 9.29078767e-02] [ 1.35291684e+00 8.86429341e-01 -2.00163731e+00 -3.71842537e-01 1.66902531e+00 -4.38569736e-01 -5.39741446e-01]] [[ 9.40277775e-04 1.34380979e+00 -7.13543985e-01 -8.31153539e-01 -2.37023165e+00 -1.86076079e+00 -8.60757398e-01] [-1.54199553e+00 -9.70735912e-01 -1.30703025e+00 2.86349747e-01 3.77984111e-01 -7.53886535e-01 3.31285650e-01]] [[-1.54910644e+00 2.21845987e-02 7.58363145e-01 -6.60524328e-01 8.62580083e-01 -1.00319021e-02 5.00093559e-02] [-1.33260971e+00 1.07462269e+00 7.23641505e-01 6.90001853e-01 1.00154344e+00 -5.03087391e-01 -6.22274225e-01]]] [-2.04707659e-01 4.78943338e-01 -5.19438715e-01 -5.55730304e-01 1.96578057e+00 1.39340583e+00 9.29078767e-02 1.35291684e+00 8.86429341e-01 -2.00163731e+00 -3.71842537e-01 1.66902531e+00 -4.38569736e-01 -5.39741446e-01 9.40277775e-04 1.34380979e+00 -7.13543985e-01 -8.31153539e-01 -2.37023165e+00 -1.86076079e+00 -8.60757398e-01 -1.54199553e+00 -9.70735912e-01 -1.30703025e+00 2.86349747e-01 3.77984111e-01 -7.53886535e-01 3.31285650e-01 -1.54910644e+00 2.21845987e-02 7.58363145e-01 -6.60524328e-01 8.62580083e-01 -1.00319021e-02 5.00093559e-02 -1.33260971e+00 1.07462269e+00 7.23641505e-01 6.90001853e-01 1.00154344e+00 -5.03087391e-01 -6.22274225e-01]
In [13]:
# T, or transpose, *permutes* the order of dimensions
sub_mat.T.shape
Out[13]:
(7, 2, 3)
In [14]:
def closest(arr, val):
return np.amin(np.abs(arr - val))
In [15]:
print(closest(sub_mat, 0.0))
0.0009402777753328851