510 A Computational Introduction to Digital Image Processing, Second Edition
Python
In : B = A-1
In : print A.dot(B)
[[ 24 30 36]
[ 51 66 81]
[ 78 102 126]]
Note that we invoked the sqrt function from numpy, which operates on arrays. The sqrt
function f rom
math
is designed only to operate on single numbers:
Python
In : math.sqrt(A)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-69-8214aa97e221> in <module>()
----> 1 math.sqrt(A)
TypeError: only length-1 arrays can be converted to Python scalars
Here is an example of two functions sqrt with the same name, but with completely different
operations. For this reason, the importing of all functions from a library:
Python
In : from math import
*
may be dangerous, as it may place the wrong version of a function into the user namespace.
It is better to call functions from their libraries, as above.
Python provides an object oriented model, where an object of a particular type (such
as an array) has many methods associated with it. To see them, enter
A. (with period)
followed by the Tab
key:
Python
In : A. Tab
A.T A.conjugate A.flatten A.prod A.sort
A.all A.copy A.getfield A.ptp A.squeeze
A.any A.ctypes A.imag A.put A.std
A.argmax A.cumprod A.item A.ravel A.strides
A.argmin A.cumsum A.itemset A.real A.sum
A.argpartition A.data A.itemsize A.repeat A.swapaxes
A.argsort A.diagonal A.max A.reshape A.take
A.astype A.dot A.mean A.resize A.tofile
A.base A.dtype A.min A.round A.tolist
A.byteswap A.dump A.nbytes A.searchsorted A.tostring
A.choose A.dumps A.ndim A.setfield A.trace
A.clip A.fill A.newbyteorder A.setflags A.transpose
A.compress A.flags A.nonzero A.shape A.var
A.conj A.flat A.partition A.size A.view
In order to see what each one does, enter it followed by a question mark: