NumPy

作者: 昨年今日 | 来源:发表于2018-01-21 11:23 被阅读0次

# NumPy

## The Basics

NumPy 主要面向的对象是齐次多维数组。在NumPy中维度(dimension)被称为轴(axe)。轴的个数即为秩(rank)。

例如,一个坐标为[1, 2, 1]的点的秩为1,因为它只有一个轴,并且这个轴的长度为3。下面这个例子,这个数组的秩为2。第一个轴的长度为2,第二个轴的长度为3

```

[[1,0,0],

[0,1,2]]

```

NumPy的数组类(class)被称为ndarray。它的别名(alias)被叫做数组。需要注意的是,numpy.array与标准Python库类array.array不一样,它只处理一维数组,并且提供较少的功能。ndarray的主要属性如下:

- ndarray.admin

    数组的轴(axe)的数量。在Python中维度即为秩.

- ndarray.shape

    这个属性包含了数组的各向维度,例如一个m*n的矩阵,其shape为元组(m,n)

- ndarray.size

    数组中元素总共的数目。

- ndarray.dtype

    用于描述数组中元素的类型

- ndarray.itemsize

    数组中每个元素的字节数。例如,一个float64的字节数为8 =(64/8),等价于ndarray.dtype,itemsize。

- ndarray.data

    用于存储数组的实际元素。

```Python

>>> import numpy as np

>>> a = np.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.ndim

2

>>> a.dtype.name

'int32'

>>> a.itemsize

4

>>> type(a)

>>> b = np.array([6,7,8])

>>> b

array([6, 7, 8])

>>> type(b)

```

## Array Creation

```Python

>>> a = np.array([2,3,4])

>>> a

array([2, 3, 4])

>>> a = np.array(1,2,3,4)  # wrong

Traceback (most recent call last):

  File "", line 1, in

    a = np.array(1,2,3,4)

ValueError: only 2 non-keyword arguments accepted

>>> b = np.array([(1.5,2,3),(4,5,6)])

>>> b

array([[ 1.5,  2. ,  3. ],

      [ 4. ,  5. ,  6. ]])

>>> c = np.array([[1,2],[3,4]], dtype=complex)

>>> c

array([[ 1.+0.j,  2.+0.j],

      [ 3.+0.j,  4.+0.j]])

>>> np.zeros((3,4))

array([[ 0.,  0.,  0.,  0.],

      [ 0.,  0.,  0.,  0.],

      [ 0.,  0.,  0.,  0.]])

>>> np.ones(shape=(2,3),dtype=np.int16)

array([[1, 1, 1],

      [1, 1, 1]], dtype=int16)

>>> np.empty((2,3))

array([[  3.90311860e-322,  0.00000000e+000,  2.78145267e-307],

      [  4.00537061e-307,  2.23419104e-317,  8.36014030e+250]])

```

numpy.arange([start,]stop,[step,]dtype=None)

- 功能:在区间[statr,stop)之间生成一个步长为step的连续数组。start的默认值为0,默认步长为1

numpy.linspace(start,stop,num=50,endpoint=True,restep=False,dtype=None)

- start:必要参数,返回序列的起始位置。

- stop:返回序列的最后一个位置。

- num:采样点的数目,即序列的长度。

- endpoint:如果为True,则对于区间进行num-1等分,并且序列最后一个点为stop,如果为False,则对于区间尽心num等分,并且区间最后一个点为stop前一点。

- restep:默认值为False。如果为True,返回值为(samples,step)

```Python

>>> np.linspace(start=1,stop=5,num=4)

array([ 1.        ,  2.33333333,  3.66666667,  5.        ])

>>> np.linspace(start=1,stop=5,num=4,endpoint=False)

array([ 1.,  2.,  3.,  4.])

```

numpy.random.rand(d0, d1,..., dn)

- d0,d1,...,dn:shape

- return: ndarray,shape(d0, d1,...,dn)

```Python

>>> np.random.rand(3,2)

array([[ 0.17227376,  0.22609618],

      [ 0.53162876,  0.70428079],

      [ 0.30794007,  0.36767049]])

```

## Ptinting Arrays

```Python

>>> a = np.arange(6)

>>> print(a)

[0 1 2 3 4 5]

>>> b = np.arange(12).reshape(4,3)

>>> b

array([[ 0,  1,  2],

      [ 3,  4,  5],

      [ 6,  7,  8],

      [ 9, 10, 11]])

>>> print(b)

[[ 0  1  2]

[ 3  4  5]

[ 6  7  8]

[ 9 10 11]]

>>> c = np.arange(24).reshape(2,3,4)

>>> print(c)

[[[ 0  1  2  3]

  [ 4  5  6  7]

  [ 8  9 10 11]]

[[12 13 14 15]

  [16 17 18 19]

  [20 21 22 23]]]

```

## Basic Operations

```Python

>>> a = np.array([20,30,40,50])

>>> b = np.arange(4)

>>> b

array([0, 1, 2, 3])

>>> c = a-b

>>> c

array([20, 29, 38, 47])

>>> b**2

array([0, 1, 4, 9], dtype=int32)

>>> 10*np.sin(a)

array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])

>>> a<35

array([ True,  True, False, False], dtype=bool)

>>>

```

相关文章

  • 科学计算库numpy的执行示例

    numpy1 numpy2 numpy3 numpy4

  • numpy中的常量

    Constants 正无穷 numpy.inf numpy.Inf numpy.Infinity numpy.in...

  • NumPy学习资料

    Numpy 中文资料 NumPy 中文文档 NumPy 中文用户指南 NumPy 中文参考手册

  • Numpy基础

    安装Numpy Numpy Numpy属性 ndim:纬度 shape:行数和列数 size:元素个数 Numpy...

  • Numpy和Pandas基本操作速查

    """ numpy 基本操作 """'''安装 Numpy 的方法:pip install numpy''''''...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • 学习:biopython的安装

    安装Numpy 因为使用biopython需要numpy的支持,所以需要先安装numpy。安装numpy过程如下:...

  • Numpy

    Numpy中文文档 # 基本语法 ``` import numpy myText = numpy.genfromt...

  • numpy运算

    numpy的与运算 numpy 中 argsort() numpy 中的布尔索引

网友评论

    本文标题:NumPy

    本文链接:https://www.haomeiwen.com/subject/oybzoxtx.html