numpy基础2

Numpy的主对象是同质的多维数组,称为ndarray。它有多个轴,轴的数量称为秩(rank)。重要属性包括ndim(轴的数量),shape(数组的尺寸),size(元素总数),dtype(元素类型)和itemsize(每个元素的字节大小)。此外,文章还提到了Numpy中的索引方式,range与arange的区别,以及Numpy的常量和类型判断函数。
摘要由CSDN通过智能技术生成

Numpy basic

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are calledaxes. The number of axes is rank.

NumPy’s array class is called ndarray. Itis also known by the alias array.

the low-level ndarray constructor

attributes:

The more important attributesof an ndarray object are:

解释说明:

ndarray.ndim

the number of axes (dimensions) of thearray. In the Python world, the number of dimensions is referred to as rank.

ndarray.shape

the dimensions of the array. This is atuple of integers indicating the size of the array in each dimension. For amatrix with n rows and m columns, shape will be (n,m). The length of the shapetuple is therefore the rank, or number of dimensions, ndim.

ndarray.size

the total number of elements of the array.This is equal to the product of the elements of shape.

ndarray.dtype

an object describing the type of theelements in the array. One can create or specify dtype’s using standard Pythontypes. Additionally NumPy provides types of its own. numpy.int32, numpy.int16,and numpy.float64 are some examples.

ndarray.itemsize

the size in bytes of each element of thearray. For example, an array of elements of type float64 has itemsize 8(=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalentto ndarray.dtype.itemsize.

ndarray.data

the buffer containing the actual elementsof the array. Normally, we won’t need to use this attribute because we willaccess the elements in an array using indexing facilities.

method:

Numpy中括号与维度的关系

二维矩阵:

对二维矩阵,这种摆放方式其元素位置就是其位置关系。

三维矩阵:

对三维矩阵,这种摆放方式其每一块就是Z轴上的一面

总之,无论对二维矩阵,还是三维矩阵,对其位置关系要以链式索引的角度来看。

range(), arange()与xrange()的区别

1、range多用作循环,range(0,10)返回一个range对象,如想返回一个list,前面加上list转换;

2、arange是numpy模块中的函数,使用前需要先导入此模块,arange(3):返回array类型对象。【注:range()中的步长不能为小数,但是np.arange()中的步长可以为小数】

3、xrange()也是用作循环,只是xrang(0,10)不返回list,返回xrange对象。每次调用返回其中的一个值。返回很大的数的时候或者频繁的需要break时候,xrange性能更好。

【注意:python3.x中把xrange()取消了】

Numpy中的常量

常见的常量:

  1. np.nan

  1. np.e

  1. np.pi

  1. np.euler_gamma

  1. np.PZERO: 表示正零,正零被认为是有限数

  1. np.NZERO:表示负零,负零被认为是有限数

  1. np.newaxis:None 的便捷别名,对索引数组很有用。

  1. np.inf:

np.inf = np.Inf = np.infty = np.Infinity = np.PINF

np.PINF!=np.NINF

np.inf==np.NINF

常量判断函数:

  • isnan : 显示哪些元素不是数字;

  • isinf : 显示哪些元素为正或负无穷大;

  • isposinf : 显示哪些元素是正无穷大;

  • isneginf : 显示哪些元素为负无穷大;

  • isfinite : 显示哪些元素是有限的(不是非数字,正无穷大和负无穷大中的一个);

类型关系图:

注意:

np.isscalar(5)

True

np.isscalar(np.array([5]))

False

np.isscalar(np.array(5))

False

注意:

np.array(5).shape

()

np.array([5]).shape

(1,)

Numpy常识

In NumPy dimensions are called axes.The number of axes is rank

NumPy’s array class is called ndarray.It is also known by the alias array. Note that numpy.arrayis not the same as the Standard Python Library class array.array, which onlyhandles one-dimensional arrays and offers less functionality

the product operator * operates elementwisein NumPy arrays. The matrix product can be performed using the dot function ormethod

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值