2016年6月23日 星期四

Python_Note14

Python Numpy

NumPy v1.10 Manual

http://docs.scipy.org/doc/numpy-1.10.1/index.html

Search page

http://docs.scipy.org/doc/numpy/search.html

Numpy for Matlab users

http://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html#numpy-for-matlab-users
相信有許多人是想利用Python Numpy 來取代Matlab的一些功能,譬如科學計算、畫圖等,上面是官方釋出給從Matlab轉過來使用Numpy的使用者手冊。所以有使用過Matlab的人可以先看這篇,相信可以更快上手Numpy。

numpy.ndarray.shape

範例:
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged


numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False)

http://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html#numpy-unique
Find the unique elements of an array.找出陣列中所有不同的元素。
範例:
In[128]: ar = [1,1,2,3,4,2,3,4,6,6,7,2,1,3,1,1,2,3,5,5,3]
In[129]: np.unique(a)
Out[129]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

numpy.meshgrid(*xi, **kwargs)

http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html#numpy-meshgrid
Return coordinate matrices from coordinate vectors.

範例:
>>> X, Y = np.meshgrid([1,2,3], [4,5,6,7])
>>> X
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
>>> Y
array([[4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7]])

numpy.ravel(a, order='C')

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html#numpy-ravel
Return a contiguous flattened array.
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print(np.ravel(x))
[1 2 3 4 5 6]
等同於 reshape(-1,order = order)
It is equivalent to reshape(-1, order=order).
>>> print(x.reshape(-1))
[1 2 3 4 5 6]

numpy.cov(my=Nonerowvar=Truebias=Falseddof=Nonefweights=Noneaweights=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html
Estimate a covariance matrix, given data and weights.
Covariance indicates the level to which two variables vary together. If we examine N-dimensional samples, X = [x_1, x_2, ... x_N]^T, then the covariance matrix element C_{ij} is the covariance of x_i and x_j. The element C_{ii} is the variance of x_i.


Linear algebra (numpy.linalg)

https://docs.scipy.org/doc/numpy/reference/routines.linalg.html
  • Matrix and vector products
  • Decompositions
  • Matrix eigenvalues
linalg.eig(a)Compute the eigenvalues and right eigenvectors of a square array.
  • Norms and other numbers
  • Solving equations and inverting matrices
  • Exceptions




沒有留言:

張貼留言