这次机器学习的作业可以用第三方库了,果断抛弃 MATLAB 改用 Python
但是操作数组的 Numpy 之前一直没用过,今天先看看官方教程入个门
The Basics
Numpy 中主要的对象是同类元素组成的多维数组,可以通过一个正整数的元组进行索引。
在 Numpy 中维度(dimension)
称为轴(axes)
,轴的数量称为秩rank
[[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:
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'>
Array Creation
create from list/tupe:
a = np.array([1, 2, 3])
b = np.array([[1, 2, 3], [4, 5, 6]], dtype=float)
create with shape:
- zeros:
np.zeros((3, 4))
- ones:
np.ones((2, 4, 3))
- empty:
np.empty((2, 3))
uninitialized
- zeros:
create sequences of number (similar to
range()
):arange:
np.arange(10, 30, 5)
syntax is the same asrange()
but returnsarray
接受浮点数,但是由于精度影响,输出的元素个数不确定。这种情况应使用
linspace
- linspace:
np.linspace(0, 2, 9)
return an array contains 9 numbers from 0 to 2
Basic Operations
- 算数操作对于
array
是按元素运算的,并返回一个新的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]])
- 一元运算 (sum, min, max)
>>> 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])