python中numpy学习,pythonnumpy学习,NumPy的主要对象


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)>>> aarray([[ 0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [10, 11, 12, 13, 14]])>>> a.shape(3, 5)>>> a.ndim2>>> a.dtype.name‘int32‘>>> a.itemsize4>>> a.size15>>> type(a)numpy.ndarray>>> b = array([6, 7, 8])>>> barray([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.

Executefunc1d(a, *args)wherefunc1doperates on 1-D arrays andais a 1-D slice ofarralongaxis.

Parameters:

func1d: function

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

axis: integer

Axis along whicharris sliced.

arr: ndarray

Input array.

args: any

Additional arguments tofunc1d.

kwargs: any

Additional named arguments tofunc1d.

New in version 1.9.0.

Returns:

apply_along_axis: ndarray

The output array. The shape ofoutarris identical to the shape ofarr, except along theaxisdimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value offunc1d. So iffunc1dreturns a scalaroutarrwill have one fewer dimensions thanarr.

举例:

>>> 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 34 5 67 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+algebra,norm则表示范数,首先需要注意的是范数是对向量(或者矩阵)的度量,是一个标量(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)>>> yarray([[1, 2]])>>> y.shape(1, 2)

维度扩大,axis=0

>>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,newaxis]>>> yarray([[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学习

评论关闭