1. 程式人生 > >python中numpy學習

python中numpy學習

通過 import mat imp 向量 ... replace lan 維數

NumPy的主要對象是同種元素的多維數組。這是一個所有的元素都是一種類型、通過一個正整數元組索引的元素表格(通常是元素是數字)。在NumPy中維度(dimensions)叫做軸(axes),軸的個數叫做秩(rank)。

例如,在3D空間一個點的坐標 [1, 2, 3] 是一個秩為1的數組,因為它只有一個軸。那個軸長度為3.又例如,在以下例子中,數組的秩為2(它有兩個維度).第一個維度長度為2,第二個維度長度為3.

[[ 1., 0., 0.],
 [ 0., 1., 2.]]

NumPy的數組類被稱作 ndarray 。通常被稱作數組。註意numpy.array和標準Python庫類array.array並不相同,後者只處理一維數組和提供少量功能。更多重要ndarray對象屬性有:

  • ndarray.ndim

    數組軸的個數,在python的世界中,軸的個數被稱作秩

  • ndarray.shape

    數組的維度。這是一個指示數組在每個維度上大小的整數元組。例如一個n排m列的矩陣,它的shape屬性將是(2,3),這個元組的長度顯然是秩,即維度或者ndim屬性

  • ndarray.size

    數組元素的總個數,等於shape屬性中元組元素的乘積。

  • ndarray.dtype

    一個用來描述數組中元素類型的對象,可以通過創造或指定dtype使用標準Python類型。另外NumPy提供它自己的數據類型。

  • ndarray.itemsize

    數組中每個元素的字節大小。例如,一個元素類型為float64的數組itemsiz屬性值為8(=64/8),又如,一個元素類型為complex32的數組item屬性為4(=32/8).

  • ndarray.data

    包含實際數組元素的緩沖區,通常我們不需要使用這個屬性,因為我們總是通過索引來使用數組中的元素。

>>> from numpy  import *
>>> a = 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
int32 >>> a.itemsize 4 >>> a.size 15 >>> type(a) numpy.ndarray >>> b = array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) numpy.ndarray

一、numpy.apply_along_axis

官方文檔給的:

numpy.apply_along_axis(func1d, axis, arr, *args, **kwargs)

Apply a function to 1-D slices along the given axis.

Execute func1d(a, *args) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.

Parameters:

func1d : function

This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.

axis : integer

Axis along which arr is sliced.

arr : ndarray

Input array.

args : any

Additional arguments to func1d.

kwargs : any

Additional named arguments to func1d.

New in version 1.9.0.

Returns:

apply_along_axis : ndarray

The output array. The shape of outarr is identical to the shape of arr, except along the axisdimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar outarr will have one fewer dimensions than arr.

舉例:

>>> def my_func(a):#定義了一個my_func()函數,接受一個array的參數
...     """Average first and last element of a 1-D array"""
...     return (a[0] + a[-1]) * 0.5 #返回array的第一個元素和最後一個元素的平均值
>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]]) 
>>> np.apply_along_axis(my_func, 0, b)
array([ 4.,  5.,  6.])
>>> np.apply_along_axis(my_func, 1, b)
array([ 2.,  5.,  8.])

定義了一個my_func()函數,接受一個array的參數,然後返回array的第一個元素和最後一個元素的平均值,生成一個array:

1 2 3
4 5 6
7 8 9
np.apply_along_axis(my_func, 0, b)意思是說把b按列,傳給my_func,即求出的是矩陣列元素中第一個和最後一個的平均值,結果為;
4. 5. 6.
np.apply_along_axis(my_func, 1, b)意思是說把b按行,傳給my_func,即求出的是矩陣行元素中第一個和最後一個的平均值,結果為;
2. 5. 8.

二、numpy.linalg.norm

  • (1)np.linalg.inv():矩陣求逆
  • (2)np.linalg.det():矩陣求行列式(標量)

np.linalg.norm

顧名思義,linalg=linear+algebranorm則表示範數,首先需要註意的是範數是對向量(或者矩陣)的度量,是一個標量(scalar)

首先help(np.linalg.norm)查看其文檔:

norm(x, ord=None, axis=None, keepdims=False)

這裏我們只對常用設置進行說明,x表示要度量的向量,ord表示範數的種類,

技術分享


>>> x = np.array([3, 4]) >>> np.linalg.norm(x) 5. >>> np.linalg.norm(x, ord=2) 5. >>> np.linalg.norm(x, ord=1) 7. >>> np.linalg.norm(x, ord=np.inf) 4

範數理論的一個小推論告訴我們:?1?2?

三、numpy.expand_dims

主要是把array的維度擴大

numpy.expand_dims(a, axis)

舉例:

>>> x = np.array([1,2])
>>> x.shape
(2,)

shape是求矩陣形狀的。

>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)

維度擴大,axis=0

>>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,newaxis]
>>> y
array([[1],
       [2]])
>>> y.shape
(2, 1)

維度擴大,axis=1

參考:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html

http://blog.csdn.net/lanchunhui/article/details/51004387

python中numpy學習