《用Python做科学计算》学习笔记(3)--numpy中nd数组的创建
来源:互联网 发布:查找两列不同数据 编辑:程序博客网 时间:2024/06/10 19:51
- 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)
- 《用Python做科学计算》学习笔记(3)--numpy中nd数组的创建
- 《用Python做科学计算》学习笔记(4)--numpy矩阵的操作
- python 学习之 用python做科学计算Numpy
- 【Python】二、Numpy——《用Python做科学计算》
- python做科学计算 numpy scipy matplotlib
- Python科学计算--NumPy的数组对象ndarray
- 《用Python做科学计算》笔记
- 深度学习之Python的科学计算包 – Numpy
- 《用Python做科学计算》学习笔记(1)--书本介绍
- 《用Python做科学计算》学习笔记(2)--函数库介绍
- python科学计算笔记(一)NumPy中ndarray对象、ufunc运算、矩阵运算
- python科学计算numpy
- python科学计算:numpy
- Python 科学计算 -- Numpy
- 科学计算库Numpy --数组(ndarray)
- Python科学计算库Numpy的使用
- python 科学计算Numpy的自学(一)
- python科学计算库numpy的使用
- Wins10系统下安装MinGW与Eclipse IDE 环境下的配置详解(C/C++)
- 【pta】实验1-7 What is a computer?
- JS数组栈和队列操作
- Java中的static关键字解析
- matlab保存已画图像
- 《用Python做科学计算》学习笔记(3)--numpy中nd数组的创建
- AppCompat主题下 button 宽高设置wrap_content属性无效的原因
- 盲人辅助模式Accessibility剖析
- linux alias 编写 函数 脚本
- 结构体在内存中的对齐方式
- 【Android】Android开发之自定义View的功能实现详解。教你一步一步学会自定义View
- C#获取当前项目路径
- 给 Android 开发者的 RxJava 详解
- axis2系列之与spring集成