import numpy as np
print("*********步骤2*********\n")
a = np.array([1,2,3,4,5,6])# 一维数组print("一维数组:", a)
b = np.array([[1,2,3],[4,5,6]])# 多维数组print("多维数组:", b)
c = np.zeros([2,2])# 创建一个2*2的全0数组print("全零数组:", c)
d = np.ones([2,2])# 创建一个2*2的全1数组print("全一数组:", d)print(np.eye(2))
e = np.asarray([1,2,3,4,5,6])# 一维数组print("一维数组:", e)
f = np.arange(10,20,2)# 一维等间隔数组print("等间隔数组")
g = np.linspace(0,100,10)# 一维等间隔数组print(g)print("*********步骤3*********\n")
a = np.array([[1,2],[3,4],[5,6]])print('shape =', a.shape)# 数组的维度print('shape_row =', a.shape[0])# 数组第一维度上的大小,即行数print('size =', a.size)# 数组元素的个数print('size_column =', np.size(a,0))# 行数print('len =',len(a))# 数组第一维度的大小,即行数print(a.ndim)# 数组维度·
b = a.reshape(2,3)print(b.ndim)
a = np.arange(10)# 创建数组print(a[2:8:2])# 切片下标为2开始到8结束(不取8),步长为2print(a[2:8])print(a[2])print(a[2:])
a = np.array([[1.2],[3.4]])print(a >2)print(a[a >2])
a = np.array([[1,2],[3,4],[5,6]])print("a:¥n", a)
b = a.reshape(2,3)print("reshape:¥n", b)print("*********步骤4*********\n")
a = np.array([1,2,3,4])
b = np.array([1,2,3,4])
c = a * b
print(c)
a = np.arange(27).reshape((3,3,3))print(" a:¥n", a)
b = np.array([[1,2,3]])
c = a + b
print("广播之后:¥n", c)print("*********步骤5*********\n")
a = np.arange(6).reshape(2,3)print("a¥n", a)print("a[1]¥n", a[1])print("a.flat[1]¥n", a.flat[1])
a = np.arange(12).reshape(2,2,3)
b = np.transpose(a,(2,0,1))print('a=', a)print('b=', b)
a = np.arange(6).reshape(2,3)
b = np.arange(7,13).reshape(2,3)
c = np.concatenate((a, b))
d = np.concatenate((a, b), axis=1)# axis=1:按行拼接print('a=', a)print('b=', b)print('c=', c)print('d=', d)
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
c = np.stack((a, b),0)
d = np.stack((a, b),1)print('a=', a)print('b=', b)print('c=', c)print('d=', d)
a = np.arange(6).reshape(2,3)
b = np.delete(a,2)
c = np.delete(a,1, axis=1)print('a=', a)print('b=', b)print('c=', c)
矩阵
import numpy as np
import numpy.matlib
print("*********步骤2*********\n")
a = np.matlib.zeros((2,2))# 全一矩阵
b = np.matlib.ones((2,2))# 全零矩阵print('a=', a)print('b=', b)
c = np.matlib.eye(3)print('c=', c)
d = np.array([[1,2],[3,4]])
d = np.mat(a)print(d)print(type(d))
a = np.matlib.rand(3,3)print('a=', a)
a = np.array([1,2,3])
b = np.mat(a)# 三个函数功能类似
d = np.asmatrix(a)# matrix和asmatrix的区别在于asmatrix处理矩阵或数组时不复制
c = np.matrix(a)# 类似array和asarrayprint('array a=', a)print('matrix b=', b)print('matrix c=', c)print('matrix d=', d)print("*********步骤3*********\n")
a = np.mat([[1],[2],[3]])
b = np.mat([1,2,3])
c = a*b # 星乘(*)和点乘(dot)都是从ndarray类中继承而来的
d = np.dot(a, b)# 星乘(*)对于数组和矩阵而言进行的操作不同print('matrix a=', a)print('matrix b=', b)print('matrix c=', c)print('matrix d=', d)
a = np.mat([1,2,3])
b = a
c = np.multiply(a, b)
d = a*2print('matrix a=', a)print('matrix b=', b)print('matrix c=', c)print('matrix d=', d)
a = np.matrix([[2,0,0],[0,1,0],[0,0,2]])
b = a.I
print('matrix a=', a)print('matrix b=', b)
a = np.matrix([[1,2],[3,4]])
b = np.linalg.det(a)print('matrix a=', a)print('b=', b)
a = np.matrix([[1,2],[3,4]])
b = a.sum(axis=0)# 对列求和
c = a.sum(axis=1)# 对行求和
d = a.max()
e = a.min()print('matrix a=', a)print('b=', b)print('c=', c)print('d=', d)print('e=', e)print("*********步骤4*********\n")
a = np.mat([[1,2,3],[4,5,6]])
b = a.getA()# getA将矩阵类转化为数组类print('matrix a=', a)print(type(a))print('array b=', b)print(type(b))
import numpy as np
print("*********步骤1*********\n")
a = np.array([0,30,45,60,90])
b = np.sin(a*np.pi/180)
c = np.sin(a*np.pi/180)print('b=', b)print('c=', c)print("*********步骤2*********\n")
a = np.array([1.0,1.5,2.0,2.55])
b = np.around(a)
c = np.around(a, decimals=1)print('b=', b)print('c=', c)print("*********步骤3*********\n")
a = np.array([1.0,1.5,2.0,2.55])
b = np.floor(a)# 向下取整
c = np.ceil(a)# 向上取整print('b=', b)print('c=', c)print("*********步骤4*********\n")
a = np.array([1,2,3,4])
b = np.array([4,3,2,1])
c = np.add(a, b)# 加
d = np.subtract(a, b)# 减
e = np.multiply(a, b)# 乘
f = np.divide(a, b)# 除
g = np.mod(a, b)# 取余
h = np.power(a, b)# 乘方print('c=', c)print('d=', d)print('e=', e)print('f=', f)print('g=', g)print('h=', h)print("*********步骤5*********\n")
a = np.arange(6).reshape(2,3)
b = np.amin(a,0)# 第0维度上最小值
c = np.amax(a,1)# 第1维度上最大值
d = np.median(a)# 中位数
e = np.mean(a)# 平均数print('a=', a)print('b=', b)print('c=', c)print('d=', d)print('e=', e)print("*********步骤6*********\n")
a = np.array([[3,5,1],[2,8,7]])
b = np.sort(a)
b = np.sort(a, axis=0)print(b)
向量化编程
import numpy as np
import time
# 计算两组数据
a = np.random.rand(1000000)
b = np.random.rand(1000000)# 使用向量化
tic = time.time()
c = np.dot(a, b)
toc = time.time()print("c: %f"% c)print("耗时:"+str(1000*(toc-tic))+"ms")
c =0
tic = time.time()for i inrange(1000000):
c += a[i]* b[i]
toc = time.time()print("c: %f"% c)print("耗时:"+str(1000*(toc-tic))+"ms")