学习Numpy The N-dimensional array (ndarray)(N维数组)

Array objects
NumPy provides an N-dimensional array type, the ndarray, which describes a collection of “items” of the same type. The items can be indexed using for example N integers.

All ndarrays are homogeneous: every item takes up the same size block of memory, and all blocks are interpreted in exactly the same way. How each item in the array is to be interpreted is specified by a separate data-type object, one of which is associated with every array. In addition to basic types (integers, floats, etc.), the data type objects can also represent data structures.

An item extracted from an array, e.g., by indexing, is represented by a Python object whose type is one of the array scalar types built in NumPy. The array scalars allow easy manipulation of also more complicated arrangements of data.

数组对象
NumPy提供了一个N维数组类型,即ndarray,描述了一组相同类型的“项目”。这些项目可以使用例如N个整数进行索引。

所有的ndarray都是同质的:每个项目占据相同大小的内存块,所有块都以完全相同的方式进行解释。数组中每个项目的解释方式由一个单独的数据类型对象指定,每个数组都与一个数据类型对象相关联。除了基本类型(整数、浮点数等),数据类型对象还可以表示数据结构。

从数组中提取的项目,例如通过索引,由一个Python对象表示,其类型是NumPy中内置的数组标量类型之一。数组标量允许轻松处理更复杂的数据排列。

The N-dimensional array (ndarray)
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.

As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array (using, for example, N integers), and via the methods and attributes of the ndarray.

Different ndarrays can share the same data, so that changes made in one ndarray may be visible in another. That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the buffer or array interfaces.

Example

A 2-dimensional array of size 2 x 3, composed of 4-byte integer elements:

N维数组(ndarray)
ndarray是一个(通常是固定大小的)相同类型和大小的项目的多维容器。数组的维数和项目数由其形状定义,形状是一个包含N个非负整数的元组,指定了每个维度的大小。数组中项目的类型由单独的数据类型对象(dtype)指定,每个ndarray都与一个数据类型相关联。

与Python中的其他容器对象一样,可以通过索引或切片数组(例如使用N个整数),以及通过ndarray的方法和属性来访问和修改ndarray的内容。

不同的ndarray可以共享相同的数据,因此在一个ndarray中进行的更改可能会在另一个ndarray中可见。也就是说,一个ndarray可以是另一个ndarray的“视图”,并且它所引用的数据由“基础”ndarray处理。ndarrays也可以是对由Python字符串或实现缓冲区或数组接口的对象拥有的内存的视图。

示例

一个尺寸为2 x 3的二维数组,由4字节整数元素组成:

>>>x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
>>>type(x)
<class 'numpy.ndarray'>
>>>x.shape
(2, 3)
>>>x.dtype
dtype('int32')

The array can be indexed using Python container-like syntax:

该数组可以使用类似Python容器的语法进行索引:

>>> # The element of x in the *second* row, *third* column, namely, 6.
>>> x[1, 2]
6

For example slicing can produce views of the array:

例如,切片可以生成数组的视图:

>>> y = x[:,1]
>>> y
array([2, 5], dtype=int32)
>>> y[0] = 9 # this also changes the corresponding element in x
>>> y
array([9, 5], dtype=int32)
>>> x
array([[1, 9, 3],
       [4, 5, 6]], dtype=int32)

Constructing arrays

New arrays can be constructed using the routines detailed in Array creation routines, and also by using the low-level ndarray constructor:

ndarray(shape[, dtype, buffer, offset, ...])

An array object represents a multidimensional, homogeneous array of fixed-size items.

Indexing arrays

Arrays can be indexed using an extended Python slicing syntax, array[selection]. Similar syntax is also used for accessing fields in a structured data type.

构建数组
可以使用Array creation routines中详细介绍的例程来构建新数组,也可以使用底层的ndarray构造函数:

ndarray(shape[, dtype, buffer, offset, ...])

一个数组对象代表一个多维、同质的固定大小项目的数组。

索引数组
可以使用扩展的Python切片语法array[selection]来索引数组。类似的语法也用于访问结构化数据类型中的字段。

Array attributes

Array attributes reflect information that is intrinsic to the array itself. Generally, accessing an array through its attributes allows you to get and sometimes set intrinsic properties of the array without creating a new array. The exposed attributes are the core parts of an array and only some of them can be reset meaningfully without creating a new array. Information on each attribute is given below.

Memory layout

The following attributes contain information about the memory layout of the array:

ndarray.flags

Information about the memory layout of the array.

ndarray.shape

Tuple of array dimensions.

ndarray.strides

Tuple of bytes to step in each dimension when traversing an array.

ndarray.ndim

Number of array dimensions.

ndarray.data

Python buffer object pointing to the start of the array's data.

ndarray.size

Number of elements in the array.

ndarray.itemsize

Length of one array element in bytes.

ndarray.nbytes

Total bytes consumed by the elements of the array.

ndarray.base

Base object if memory is from some other object.

Data type

See also

Data type objects

The data type object associated with the array can be found in the dtype attribute:

ndarray.dtype

Data-type of the array's elements.

Other attributes

ndarray.T

View of the transposed array.

ndarray.real

The real part of the array.

ndarray.imag

The imaginary part of the array.

ndarray.flat

A 1-D iterator over the array.

Array interface

See also

The array interface protocol.

__array_interface__

Python-side of the array interface

__array_struct__

C-side of the array interface

ctypes foreign function interface

ndarray.ctypes

An object to simplify the interaction of the array with the ctypes module.

数组属性#

数组属性反映数组固有的信息 本身。通常,通过其属性访问数组允许 您可以获取并有时设置数组的内部属性,而无需 创建一个新数组。公开的属性是 数组,并且只有其中一些可以在不创建的情况下有意义地重置 一个新数组。下面给出了有关每个属性的信息。

内存布局

以下属性包含有关内存布局的信息 数组:

ndarray.标志

有关阵列的内存布局的信息。

ndarray.shape

数组维度的元组。

ndarray.strides

遍历数组时要单步执行每个维度的字节元组。

ndarray.ndim

数组维度数。

ndarray.data

指向数组数据开头的 Python 缓冲区对象。

ndarray.size

数组中的元素数。

ndarray.itemsize

一个数组元素的长度(以字节为单位)。

ndarray.nbytes

数组元素消耗的总字节数。

ndarray.base

如果内存来自其他对象,则为基本对象。

数据类型

另请参阅

数据类型对象

可以在以下属性中找到与数组关联的数据类型对象:

ndarray.dtype

数组元素的数据类型。

其他属性

ndarray。T

转置数组的视图。

ndarray.real

数组的实部。

ndarray.imag

数组的虚部。

ndarray.flat

数组上的一维迭代器。

阵列接口

另请参阅

阵列接口协议

__array_interface__

数组接口的 Python 端

__array_struct__

阵列接口的 C 侧

外部函数接口

ndarray.ctypes

用于简化数组与 ctypes 模块交互的对象。

下一期内容:Array methods

参考文献:

[1] The N-dimensional array (ndarray) — NumPy v1.26 Manual

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值