参考博客之一Python之Numpy详细教程
其中简介和对象出自该博客。
该博客中介绍的函数,本人并未采用,因为有些确实不太常用。
常用的函数我放在下面单独开了一章。
numpy简介
numpy 是一个 Python 包。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。
numpy和matplotlib(绘图库)一起使用,可以替代matlab。
numpy的对象 - ndarray
numpy 中最重要的对象为 “ndarray”。其为 n 维数组类型。它描述相同类型的元素的集合。可以使用索引来访问集合中的元素。
ndarray中的每个元素是数据类型对象的对象(称为 dtype)
numpy的常用函数
以下代码均须导入numpy函数库。
import numpy as np
3.1 mat()
mat函数可以将目标数据的类型转换为矩阵(matrix)
示例:
a=[[1,2,3,], [3,2,1]]
print('type(a) is ',type(a))
myMat=np.mat(a)
print('type(myMat) is ',type(myMat))
# 运行结果如下
# type(a) is <class 'list'>
# type(myMat) is <class 'numpy.matrix'>
3.3 tolist
把一个矩阵转化成为list(列表)
示例:
import numpy as np
x1 = np.mat([[1, 2, 3], [4, 5, 6]])
print('x1 = \n',x1)
# x1 =
# [[1 2 3]
# [4 5 6]]
print(type(x1))
# <class 'numpy.matrix'>
x2=x1.tolist()
print('x2 = ',x2)
# x2 = [[1, 2, 3], [4, 5, 6]]
print(type(x2))
# <class 'list'>