《用Python做科学计算》学习笔记(3)--numpy中nd数组的创建

来源:互联网 发布:查找两列不同数据 编辑:程序博客网 时间:2024/06/10 19:51
  1. ndarray的内存结构

和其他的库一样,每个库都可能有自己独特的数据结构,例如OpenCV,numpy库的多维数组叫做ndarray( N dimensionality array ),它的内存结构如下图:
这里写图片描述
ndarray的内存结构
在这个结构体中有两个对象,一个是用来描述元素类型的头部区域,一个是用来储存数据的数据区域。(事实上大多数数据类型的数据都是这么储存的)。

2 ndarray对象的创建

2.1 ndarray多维数组的创建常规方法
创建一个3*3的数组并在屏幕打印它以及它的类型和维数:

import numpy as npx = np.array([[0,1,2],[3,4,5],[6,7,8]],dtype = np.int32)print('这个数组是:',x)print('这个数组的数据类型是:',x.dtype)print('这个数组的大小:',x.shape)

屏幕输出结果:
这里写图片描述

我们也可以采用更加直接的办法:

import numpy as npx = np.arange(0,9).reshape(3,3)print('这个数组是:',x)print('这个数组的数据类型是:',x.dtype)print('这个数组的大小:',x.shape)

屏幕上打印输出的结果和前一种的结果是一样的。

2.2 ndarray多维数组的创建其他方法
除了常规方法,numpy还提供了一些其他的创建方法:
2.2.1 创建全0或者全1的数组

这里写图片描述
例如:
这里写图片描述

import numpy as npx = np.ones([3,3])print('这个数组是:',x)print('这个数组的数据类型是:',x.dtype)print('这个数组的大小:',x.shape)
>>> x = np.arange(6) >>> x = x.reshape(2,3)>>> x     array([[0, 1, 2], [3, 4, 5]]) >>> np.ones_like(x) array([[1, 1, 1], [1, 1, 1]])>>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.ones_like(y) array([ 1., 1., 1.])

当然也可以填充其他的数:

import numpy as npx = np.full([3,3],np.inf)print('这个数组是:',x)print('这个数组的数据类型是:',x.dtype)print('这个数组的大小:',x.shape)

打印输出:这里写图片描述

2.2.2 从已存在的数据中创建数组
这里写图片描述

>>> np.array([1, 2, 3]) array([1, 2, 3])>>> np.array([1, 2, 3.0]) array([ 1., 2., 3.])>>> np.array([[1, 2], [3, 4]]) array([[1, 2], [3, 4]])>>> np.array([1, 2, 3], ndmin=2) array([[1, 2, 3]])>>> np.array([1, 2, 3], dtype=complex) array([ 1.+0.j, 2.+0.j, 3.+0.j])>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')]) >>> x['a'] array([1, 3])

2.2.3 创建记录阵列(record array,可能翻译不准):创建一个阵列,将其他数组集中在一起管理,并可以命名:
这里写图片描述
例如:

import numpy as npx1 = np.arange(0,3)x2 = np.array(['ff','hh','yy'])x3 = ([1,2,3])r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')print(r[2])print(r.a)

2.2.4 创建字符数组
numpy提供了专门的函数创建字符数组:np.chararray()
首先看看它的参数:
Parameters
| ———-
| shape : tuple
| Shape of the array.
| itemsize : int, optional
| Length of each array element, in number of characters. Default is 1.
| unicode : bool, optional
| Are the array elements of type unicode (True) or string (False).
| Default is False.
| buffer : int, optional
| Memory address of the start of the array data. Default is None,
| in which case a new array is created.
| offset : int, optional
| Fixed stride displacement from the beginning of an axis?
| Default is 0. Needs to be >=0.
| strides : array_like of ints, optional
| Strides for the array (see ndarray.strides for full description).
| Default is None.
| order : {‘C’, ‘F’}, optional
| The order in which the array data is stored in memory: ‘C’ ->
| “row major” order (the default), ‘F’ -> “column major”
| (Fortran) order.

 Examples |  -------- |  >>> charar = np.chararray((3, 3)) |  >>> charar[:] = 'a' |  >>> charar |  chararray([['a', 'a', 'a'], |         ['a', 'a', 'a'], |         ['a', 'a', 'a']], |        dtype='|S1') |   |  >>> charar = np.chararray(charar.shape, itemsize=5) |  >>> charar[:] = 'abc' |  >>> charar |  chararray([['abc', 'abc', 'abc'], |         ['abc', 'abc', 'abc'], |         ['abc', 'abc', 'abc']], |        dtype='|S5')

2.2.5 创建数值数组
这里写图片描述

import numpy as npx1 = np.arange(0,3,1)x2 = np.linspace(0,3,4)x3 = np.logspace(1,8,3)x4 = np.mgrid[0:3,0:3]x5 = np.ogrid[0:3,0:3]print(x1,x2,x3,x4,x5)

2.2.6 创建矩阵
这里写图片描述

2.2.7 矩阵类(The matrix class)
这里写图片描述

1 0
原创粉丝点击