此次机器学习的做业能够用第三方库了,果断抛弃 MATLAB 改用 Python
可是操做数组的 Numpy 以前一直没用过,今天先看看官方教程入个门html
Numpy 中主要的对象是同类元素组成的多维数组,能够经过一个正整数的元组进行索引。
在 Numpy 中维度(dimension)
称为轴(axes)
,轴的数量称为秩rank
python
[[1., 0., 0.], [0., 1., 2.]]
:rank=2
the first dimension has a length of 2, the second dimension has a length of 3数组
Numpy 的数组类叫作 ndarray
or array
attributes:dom
ndarray.ndim
ndarray.shape
ndarray.size
ndarray.dtype
:ndarray.dtype.name
返回字符串表示的类型名称ndarray.data
Example:机器学习
>>> import numpy as np >>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> a.shape (3, 5) >>> a.ndim 2 >>> a.dtype.name 'int64' >>> a.itemsize 8 >>> a.size 15 >>> type(a) <type 'numpy.ndarray'> >>> b = np.array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) <type 'numpy.ndarray'>
create from list/tupe:学习
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6]], dtype=float)
create with shape:ui
np.zeros((3, 4))
np.ones((2, 4, 3))
np.empty((2, 3))
uninitializedcreate sequences of number (similar to range()
):spa
arange: np.arange(10, 30, 5)
syntax is the same as range()
but returns array
code
接受浮点数,可是因为精度影响,输出的元素个数不肯定。这种状况应使用
linspace
np.linspace(0, 2, 9)
return an array contains 9 numbers from 0 to 2array
是按元素运算的,并返回一个新的 array
>>> a = np.array( [20,30,40,50] ) >>> b = np.arange( 4 ) >>> b array([0, 1, 2, 3]) >>> c = a-b >>> c array([20, 29, 38, 47]) >>> b**2 array([0, 1, 4, 9]) >>> 10*np.sin(a) array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) >>> a<35 array([ True, True, False, False], dtype=bool) >>> A = np.array( [[1,1], ... [0,1]] ) >>> B = np.array( [[2,0], ... [3,4]] ) >>> A*B # elementwise product array([[2, 0], [0, 4]]) # 两种矩阵乘法 >>> A.dot(B) array([[5, 4], [3, 4]]) >>> np.dot(A, B) array([[5, 4], [3, 4]])
>>> a = np.random.random((2,3)) >>> a array([[ 0.18626021, 0.34556073, 0.39676747], [ 0.53881673, 0.41919451, 0.6852195 ]]) >>> a.sum() 2.5718191614547998 >>> a.min() 0.1862602113776709 >>> a.max() 0.6852195003967595 >>> b = np.arange(12).reshape(3,4) >>> b array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> >>> b.sum(axis=0) # sum of each column array([12, 15, 18, 21]) >>> >>> b.min(axis=1) # min of each row array([0, 4, 8])