PythonNumpy库的用法
这是我学些numpy时做的一些笔记,分享给大家,在CSDN上搜索相关词也可获得numpy简介
NumPy 是一个 Python 包。 它代表 "Numeric Python"。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。 numpy操作数组的算数和逻辑运算。 傅立叶变换和用于图形操作的例程。 与线性代数有关的操作。 NumPy 拥有线性代数和随机数生成的内置函数。 numPy - 数据类型
NumPy 支持比 Python 更多种类的数值类型。 下表显示了 NumPy 中定义的不同标量数据类型。
序号
数据类型及描述
1.
bool_ 存储为一个字节的布尔值(真或假)
2.
int_ 默认整数,相当于 C 的long,通常为int32或int64
3.
intc 相当于 C 的int,通常为int32或int64
4.
intp 用于索引的整数,相当于 C 的size_t,通常为int32或int64
5.
int8 字节(-128 ~ 127)
6.
int16 16 位整数(-32768 ~ 32767)
7.
int32 32 位整数(-2147483648 ~ 2147483647)
8.
int64 64 位整数(-9223372036854775808 ~ 9223372036854775807)
9.
uint8 8 位无符号整数(0 ~ 255) 示例1 标注矩阵格式# 首先创建结构化数据类型。 import numpy as np dt = np.dtype([("age",np.int8)]) print dt
[("age", "i1")] 示例2 矩阵格式传递# 文件名称可用于访问 age 列的内容 import numpy as np dt = np.dtype([("age",np.int8)]) a = np.array([(10,),(20,),(30,)], dtype = dt) print a["age"]
[10 20 30] 示例3 矩阵中对应格式的表达 import numpy as np student = np.dtype([("name","S20"), ("age", "i1"), ("marks", "f4")]) a = np.array([("abc", 21, 50),("xyz", 18, 75)], dtype = student) print a
[("abc", 21, 50.0), ("xyz", 18, 75.0)]
每个内建类型都有一个唯一定义它的字符代码: "b" :布尔值 "i" :符号整数 "u" :无符号整数 "f" :浮点 "c" :复数浮点 "m" :时间间隔 "M" :日期时间 "O" :Python 对象 "S", "a" :字节串 "U" :Unicode "V" :原始数据( void ) numpy-数组属性示例1 调整矩阵行列import numpy as np a = np.array([[1,2,3],[4,5,6]]) b = a.reshape(3,2) print b
[[1, 2] [3, 4] [5, 6]] 示例2 调整矩阵行列# 一维数组 import numpy as np a = np.arange(24) a.ndim # 现在调整其大小 b = a.reshape(2,4,3) print b # b 现在拥有三个维度
[[[ 0, 1, 2] [ 3, 4, 5] [ 6, 7, 8] [ 9, 10, 11]] [[12, 13, 14] [15, 16, 17] [18, 19, 20] [21, 22, 23]]] numpy-来自数据的数组示例1 矩阵表达# 将列表转换为 ndarray import numpy as np x = [1,2,3] a = np.asarray(x) print a
[1 2 3] 示例2 元组转化为矩阵# 来自元组的 ndarray import numpy as np x = (1,2,3) a = np.asarray(x) print a
[1 2 3] 示例3 元组矩阵转化为矩阵# 来自元组列表的 ndarray import numpy as np x = [(1,2,3),(4,5)] a = np.asarray(x) print a
[(1, 2, 3) (4, 5)] numpy-数组上的迭代示例1 矩阵展开import numpy as np a = np.arange(0,60,5) a = a.reshape(3,4) print "原始数组是:" print a print " " print "修改后的数组是:" for x in np.nditer(a): print x,
原始数组是: [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]]
修改后的数组是: 0 5 10 15 20 25 30 35 40 45 50 55 示例2 矩阵的转置import numpy as np a = np.arange(0,60,5) a = a.reshape(3,4) print "原始数组是:" print a print " " print "原始数组的转置是:" b = a.T print b print " " print "修改后的数组是:" for x in np.nditer(b): print x,
原始数组是: [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]]
原始数组的转置是: [[ 0 20 40] [ 5 25 45] [10 30 50] [15 35 55]]
修改后的数组是: 0 5 10 15 20 25 30 35 40 45 50 55 不同风格的顺序
C风格是横着顺序
F风格是竖着的顺序 修改数组的值示例1 矩阵相乘import numpy as np a = np.arange(0,60,5) a = a.reshape(3,4) print "原始数组是:" print a print " " for x in np.nditer(a, op_flags=["readwrite"]): x[...]=2*x print "修改后的数组是:" print a
原始数组是: [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]]
修改后的数组是: [[ 0 10 20 30] [ 40 50 60 70] [ 80 90 100 110]] 广播迭代示例1 矩阵对应排列import numpy as np a = np.arange(0,60,5) a = a.reshape(3,4) print "第一个数组:" print a print " " print "第二个数组:" b = np.array([1, 2, 3, 4], dtype = int) print b print " " print "修改后的数组是:" for x,y in np.nditer([a,b]): print "%d:%d" % (x,y),
第一个数组: [[ 0 5 10 15] [20 25 30 35] [40 45 50 55]]
第二个数组: [1 2 3 4]
修改后的数组是: 0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4 numpy—数组操作修改形状示例1 flat迭代器import numpy as np a = np.arange(8).reshape(2,4) print "原始数组:" print a print " " print "调用 flat 函数之后:" # 返回展开数组中的下标的对应元素 print a.flat[5]
原始数组: [[0 1 2 3] [4 5 6 7]]
调用 flat 函数之后: 5 示例2 横竖格式表达C,Fimport numpy as np a = np.arange(8).reshape(2,4) print "原数组:" print a print " " # default is column-major print "展开的数组:" print a.flatten() print " " print "以 F 风格顺序展开的数组:" print a.flatten(order = "F")
原数组: [[0 1 2 3] [4 5 6 7]]
展开的数组:默认是A [0 1 2 3 4 5 6 7]
以 F 风格顺序展开的数组: [0 4 1 5 2 6 3 7] 翻转操作示例1 transpose转置矩阵import numpy as np a = np.arange(12).reshape(3,4) print "原数组:" print a print " " print "转置数组:" print np.transpose(a) print "转置数组:" print a.T
原数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
转置数组: [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]]