一、介绍
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
NumPy 的前身 Numeric 最早是由 Jim Hugunin 与其它协作者共同开发,2005 年,Travis Oliphant 在 Numeric 中结合了另一个同性质的程序库 Numarray 的特色,并加入了其它扩展而开发了 NumPy。NumPy 为开放源代码并且由许多协作者共同维护开发。
二、常用API
2.1.numpy.genfromtxt
从文本上读取相应的矩阵数据,delimiter是分隔符
import numpydata = numpy.genfromtxt("data.txt",dtype=str,delimiter=",")print(data)print(type(data))print(help(numpy.genfromtxt))
2.2.numpy.array
将列表转换为矩阵
vector = numpy.array([5,10,15,20])matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])print(vector)print(matrix)
2.3.vector.shape matrix.shape
获取一维矩阵的元素个数,获取二维矩阵的行列
vector = numpy.array([1,2,3,4])# 一维则打印元素个数print(vector.shape)matrix = numpy.array([[5,10,15],[20,25,30]])# 二维打印数组的行列print(matrix.shape)
2.4.dtype
返回元素类型
# 得相同类型vector2 = numpy.array([1,2.0,u"xx"])vector2.dtype
2.5.取行列直接用中括号取
matrix = numpy.array( [ [5,10,15], [20,25,30] ])print(matrix[1][0])print(matrix[1][2])
2.6.切片的使用
2.6.1.一维切片
vector = numpy.array([0,1,2,3])# 取[0,3)print(vector[0:3])
2.6.2.二维切片
切片列元素,并转换为行
matrix = numpy.array( [ [5,10,15], [20,25,30] ])# 取第一列print(matrix[:,1])# 取第[0,2)列print(matrix[:,0:2])print(matrix[0:1:,0:2])
2.7.进行每个元素判断
import numpyvector = numpy.array([5,10,15,20])# 对每个元素都进行判断vector == 10
2.8.进行判断二维
matrix = numpy.array( [ [5,10,15], [20,25,30], [35,40,45] ])matrix == 20
2.9.这个判断值可以作为索引
vector = numpy.array([5,10,15,20])equal_to_ten = (vector == 10)print(equal_to_ten)print(vector[equal_to_ten])matrix = numpy.array( [ [5,10,25], [20,25,30], [35,40,45] ])value_25 = matrix==25print(value_25)print(matrix[value_25])
2.10.与或进行条件判断
多条件判断矩阵
vector = numpy.array([5,10,15,20])equal_to_ten_and_five = (vector == 10) & (vector==5)print(equal_to_ten_and_five)equal_to_ten_and_five = (vector == 10) | (vector==5)print(equal_to_ten_and_five)vector[equal_to_ten_and_five] = 100print(vector)
2.11.元素转换类型
vector = numpy.array(["1","2","3"])print(vector)print(vector.dtype)vector = vector.astype(float)print(vector)print(vector.dtype)
2.12.求最大最小值min max
vector = numpy.array([5,6,10,10])min = vector.min()max = vector.max()print(min)print(max)
2.13.求列和
matrix = numpy.array( [ [5,10,25], [20,25,30], [35,40,45] ])# 维度为0时候,按行读取matrix.sum(axis=0)
2.14.求行和
matrix = numpy.array( [ [5,10,25], [20,25,30], [35,40,45] ])# 维度为1时候,按列读取matrix.sum(axis=1)
2.15.列表
import numpy as np# 进行15个元素print(np.arange(15))# 转成3*5的a = np.arange(15).reshape(3,5)print(a)
2.16.打印行列
# 打印行列print(a.shape)
2.17.打印维度
# 打印维度a.ndim
2.18.打印dtype的名字
a.dtype.name
2.19.数组大小
# 数组大小a.size
2.20.生成3行4列0矩阵
# 生成3行4列的0矩阵np.zeros((3,4))
2.21.生成3行4列1矩阵
# 生成3行4列的1矩阵np.ones((3,4))
2.22.生成10~30,隔着5
np.arange(10,30,5)
2.23.重置行列
np.arange(12).reshape(4,3)
2.24.随机数
# 2行3列的矩阵np.random.random((2,3))
2.25.造100个值,0~2*pi,均分100
# 造100个值from numpy import pinp.linspace(0,2*pi,100)
2.26.矩阵的加减点乘法和直接乘法,次方
a = np.array([20,30,40,50])b = np.arange(4)print(a)print(b)c = a-bprint(c)c -= 1print(c)b**=2print(b)print(a<35)
A = np.array([[1,1], [0,1]])B = np.array([[2,0], [3,4]])print(A)print(B)print("--------")print(A*B)print("--------")print(A.dot(B))