>>> a = np.array([1, 2, 3])
>>> b = np.array([(2.5, 1, 4.5), (5, 6, 7, 8)])
>>> a.shape # Array dimensions
>>> len(a) # Length of array
>>> len(b) # Length of array
>>> b.ndim # Number of array dimensions
>>> a.size # Number of array elements
>>> b.size # Number of array elements
>>> b.dtype # Data type of array elements
dtype('O')
>>> a.dtype # Data type of array elements
dtype('int32')
>>> b.dtype.name # Name of data type
'object'
>>> a.dtype.name # Name of data type
'int32'
>>> a.astype(float) # Convert an array to a different type
array([1., 2., 3.])
3.5 尋求幫助 (Asking For Help)
>>> np.info(max)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
>>> np.info(np.ndarray.dtype)
Data-type of the array's elements.
Parameters
----------
Returns
-------
d : numpy dtype object
See Also
--------
numpy.dtype
Examples
--------
array([[0, 1],
[2, 3]])
>>> x.dtype
dtype('int32')
>>> type(x.dtype)
<type 'numpy.dtype'>
3.6 陣列數學運算 (Array Mathematics)
>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 4, 6])
>>> np.add(a, b) # 陣列加法,也可以a + b
array([3, 6, 9])
>>> np.subtract(a, b) # 陣列減法,也可以a – b
Array([-1, -2, -3])
>>> np.multiply(a, b) # 陣列乘法,也可以a * b
Array([ 2, 8, 18])
>>> np.divide(a, b) # 陣列除法,也可以a / b
array([0.5, 0.5, 0.5])
>>> a = np.array([(1, 2, 3), (4, 5, 6)])
>>> b = np.array([(1, 3, 3), (4, 7, 6)])
>>> c = np.array([(1, 2, 3), (4, 5, 6)])
>>> a == b # Element-wise comparison
array([[ True, False, True],
[ True, False, True]])
>>> a < 3 # Element-wise comparison
array([[ True, True, False],
[False, False, False]])
>>> np.array_equal(a, b) # Array-wise comparison
False
>>> a = np.array([2, 5, 7])
>>> b = np.array([(0, 1), (2, 3)])
>>> a.sum() # Array-wise sum
>>> a.max() # Array-wise maximum value
>>> a.min() # Array-wise minimum value
>>> b.max(axis=0) # Maximum value of an array row
array([2, 3])
>>> b.max(axis=1) # Maximum value of an array row
array([1, 3])
>>> np.median(a) # Median
>>> np.mean(a) # Mean
4.666666666666667
>>> np.std(a) # Standard deviation
2.0548046676563256