import numpy as np
###numpy的创建以及基础运算
#a = np.array([10,20,30,40])
# a = np.array([[1,2],[3,4]])
# b = np.arange(4).reshape(2,2)
# print(a.dot(b))
# print(a, b)
#c = a * b#逐个相乘
#c = np.dot(a, b)#矩阵相乘
#c = b ** 2
#c = np.sin(a)
#print(c)
# a = np.random.random((2,2, 2))
# print(a)
# # print(np.sum(a))
# # print(np.min(a))
# # print(np.max(a))
# print(np.max(a,axis=1))
###numpy的基础运算
# A = np.arange(2,14).reshape(3,4)
# print(A)
# print(np.argmin(A))#输出最小值索引
# print(np.argmax(A))
# print(np.mean(A))
# print(A.mean())
# print(np.average(A))
# print(np.median(A))#输出中位数
# print(np.cumsum(A))#逐步累加,前n项和
# print(np.diff(A))#两两相减
# print(np.nonzero(A))#输出非零数的坐标(行列分开输出)
# print(np.sort(A))#逐行排序,不是整体排序
# print(np.transpose(A))#矩阵的转置,非常重要
# print((A.T).dot(A))#矩阵的转置,非常重要
# print(np.clip(A, 5, 9))#小于5的等于5, 大于9的等于9,中间的不变
# print(np.mean(A,axis = 0))#0代表列,1代表行
###numpy的索引
# A = np.arange(3, 15).reshape(3, 4)
# print(A)
# print(A[2])#索引出整行
# print(A[1][1])#类似c的数组索引
# print(A[1, 1])#同上
# print(A[2, :])#切片,取出整行
# print(A[2, 0:2])
# for column in A.T:#无法直接迭代列,转置后可以迭代到
# print(column)
# print(A.flatten())#改变数组结构
# for item in A.flat:#返回一个迭代器
# print(item)
###numpy的array合并
# A = np.array([1,1,1])[:, np.newaxis]#在后面增加一个维度,所以结构是3,1, 若在前面则是1, 3
# B = np.array([2,2,2])[:, np.newaxis]
# # C = np.vstack((A, B))
# # D = np.hstack((A, B))#进行左右合并
# # print(np.vstack((A, B)))#对数组进行上下的合并,注意要求参数是元组
# # print(A.shape, C.shape)
# # print(D.shape)
# #print(A.T)#序列无法转置
# # print(A[:,np.newaxis].shape)#增加了一个维度,现在可以转置了
# # print(B[:,np.newaxis].shape)
# print(A)
# print(B)
# C = np.concatenate((A, B, B, A), axis = 0)#被合并的维度必须大于1, 可以进行多个array的合并,0表示合并的维度
# print(C)
# C = np.concatenate((A,B), axis = 1)
# print(C)
###numpy array的分割
# A = np.arange(12).reshape(3, 4)
# print(A)
# print()
# # print(np.split(A,3, axis=0))#0是按列分成3行
# #print(np.array_split(A, 3, axis = 1))#不等分割
# print(np.vsplit(A, 3))#按照纵向分割
# print(np.hsplit(A, 2))#按照横向分割
###numpy的赋值
# a = np.array([0, 1, 2, 3])#默认都是整型
# # b = a
# # c = a
# # d = b
# # a[0] = 11#改变a, b与c都会变;d也完全等于a。都指向同一个对象
# # print(a)
# # print(b is a)
# # print(d is a)
# b = a.copy()#deep copy不会关联,不会互相影响
# a[3] = 44
# print(b)
numpy学习记录
最新推荐文章于 2024-11-01 13:45:28 发布