numpy教程:numpy基本数据类型及多维数组元素存取

本文详细介绍了NumPy的基础,包括array与ndarray的区别,NumPy中的数据类型,如缺失值的表示,以及多维数组ndarray的内存结构。文章还探讨了数组元素的存取,如使用整数序列和布尔数组进行切片操作,以及结构数组和掩码数组的概念。通过实例解析了多维数组的存取方法,展示了NumPy在数据处理方面的强大功能。
摘要由CSDN通过智能技术生成

http://blog.csdn.net/pipisorry/article/details/39215089

NumPy介绍

Numpy(读作num-pie)是Python中的一个矩阵计算包,功能类似于MATLAB的矩阵计算。

标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针。这样为了保存一个简单的[1,2,3],需要有3个指针和三个整数对象。对于数值运算来说这种结构显然比较浪费内存和CPU计算时间。

此外Python还提供了一个array模块,array对象和列表不同,它直接保存数值,和C语言的一维数组比较类似。但是由于它不支持多维,也没有各种运算函数,因此也不适合做数值运算。

NumPy的诞生弥补了这些不足,NumPy提供了两种基本的对象:ndarray(N-dimensional array object)和ufunc(universal function object)。ndarray(下文统一称之为数组)是存储单一数据类型的多维数组(同c语言数组直接保存数值,见下面的多维数组ndarray内存结构部分),而ufunc则是能够对数组进行处理的函数。from:张若愚的《Python科学计算》

具体参见http://www.numpy.org/

安装numpy参见linux和windows中安装python科学计算环境-pycharm、numpy

在Python中使用help帮助

>>> import numpy

>>> help(numpy.argsort)

Numpy中array和ndarray的区别

What is the difference between ndarray and array in numpy?

np.array is just a convenience function to create an ndarray, it is not a class itself.

You can also create an array using np.ndarray, but it is not the recommended way. From the docstring of np.ndarray:

          Arrays should be constructed using array, zeros orempty ... The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.

where can I find the implementations in the numpy source code?

1 Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here:

https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py

2 array() is implemented in core/src/multiarray/methods.c in array_getarray()

皮皮blog

NumPy中的数据类型

对于科学计算来说,Python中自带的整型、浮点型和复数类型远远不够,因此NumPy中添加了许多数据类型。

Numpy中基础的数据类型是np.dtype类的对象.

需要指定所用数据类型的场合, 比如新生成数组时, 一般都会有个可选参数叫dtype (注意看上面示例). 这个参数可以接受真正的np.dtype对象, 也可以很聪明地直接接收普通的标量类型, 也可以接收各种类型的字符串表示(注意看下面那个大列表里的字符简称). 其默认值一般都是python原生的那种float型(一般就相当于C里面的double).

类似于其他语言的int, float, double之类的标量类型并不是np.dtype类型对象, 但可以用它来构造np.dtype对象. 比如用Python原生的float型来构造:my_type = np.dtype(float)

numpy缺失值的表示(None, np.NaN, np.NaT, pd.NaT)

NaN: not a number, NaN is the default missing value marker forreasons of computational speed and convenience, we need to be able to easilydetect this value with data of different types: floating point, integer,boolean, and general object.

None: treats None like np.nan. In many cases, however, the Python None will arise and we wish to also consider that “missing” or “null”.

NaT:  Datetimes, For datetime64[ns] types, NaT represents missing values. This is a pseudo-native sentinel value that can be represented by numpy in a singular dtype (datetime64[ns]). pandas objects provide intercompatibility between NaT and NaN.

inf: Prior to version v0.10.0 inf and -inf were also considered to be “null” in computations. This is no longer the case by default; use the mode.use_inf_as_null option to recover it.

Note: numpy缺失值的判断要用np.isnan(),而不能使用a[0] == np.NaN.[numpy教程:逻辑函数Logic functions ]

numpy数据类型

Numpy中提供了更细致的标量类型, 见下表,这些基础的标量类型不是np.dtype对象.

每一个numpy内置的数据类型都有一个特征码,它能唯一标识数据类型:

采用特征码来表示数据类型时,特征码来表示数据类型时,第一个字符指明数据的种类,其余字符指明每一种数据类型的字节( 对于字符串来说,该指明字符中字符数)。其中数据 类型的字节数必须与某一个 numpy内置的类型相匹配,否则将会抛出异常。

np.类型名 对应于别的语言的类型 字符简称
Booleans
np.bool_ Python bool '?'
np.bool8 8 bit bool
Int
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值