Python常用工具库-实验(6个lab)-NumPy

Python常用工具库-实验(6个lab)-NumPy,第1张

NumPy 数组
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),步长为2
print(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和asarray
print('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*2
print('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("*********步骤2*********\n")
r1 = np.random.rand(2, 2)  # rand函数范围在[0,1)
r2 = np.random.randn(2, 2)  # randn函数具有标准正态分布
r3 = np.random.randint(0, 5)  # randint(low,high,size)函数返回在[low,high)范围的整数
r4 = np.random.random((2, 2))  # random函数,和random.rand函数输出相同,输入有区别

print('r1=', '¥n', r1)
print('r2=', '¥n', r2)
print('r3=', '¥n', r3)
print('r4=', '¥n', r4)

r1 = np.random.choice(5, 3)  # 从np.arange(5)数组中抽取3个数
r2 = np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])  # 被抽取数组中的元素具有不同概率
r3 = np.random.choice(5, 3, replace=False)  # 不放回抽样

print('r1=', '¥n', r1)
print('r2=', '¥n', r2)
print('r3=', '¥n', r3)

print("*********步骤3*********\n")
sample = np.arange(5)
np.random.shuffle(sample)  # 改变自身内容
r2 = np.random.permutation([1, 2, 3.4])  # 返回随机序列

print('sample=', '¥n', sample)
print('r2=', '¥n', r2)

print("*********步骤4*********\n")
r1 = np.random.normal(0, 0.1, 5)  # 正态分布,参数1为均值,参数2为标准差,参数3为返回值的维度
r2 = np.random.uniform(0, 5, 2)  # 均匀分布
r3 = np.random.poisson(5, 2)  # 泊松分布
print('r1=', '¥n', r1)
print('r2=', '¥n', r2)
print('r3=', '¥n', r3)

常用函数
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 in range(1000000):
    c += a[i] * b[i]
toc = time.time()
print("c: %f" % c)
print("耗时:" + str(1000*(toc-tic)) + "ms")

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/736337.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存