python中的np_python中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)>>>a

array([[ 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])>>>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.

举例:

>>> defmy_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+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)>>>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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值