import numpy as np
# 类似矩阵运算
arr = np.array([[1,2,3],[4,5,6]])
arr = arr * arr
print(arr)
#arr = arr - arr
#print(arr)
#arr= 1/arr
#print(arr)
arr = arr ** 0.5
print(arr)
a = np.arange(9)
# 选取3-7的数
print(a[3:7])
# 区间2-7,选取步长为2的数
print(a[2:7:2])
# 翻转输出
print(a[::-1])
# 切片
s = slice (8,0,-2)
print(a[s])
b = np.arange(24).reshape(2,3,4)
print(b.shape)
print(b)
c= np.zeros((2,3))
c.dtype = int
print(c[0,2])
c[0,2]=5
print(c[0,2])
# :符号相当于省略
print(b[0,:,:])
print(b[0,1,:])
print(b[0,1,::2])
print(b[:,:,1])
# 倒序输出最后一维的值
print(b[0,:: -1,-1])
print(b[0,:: 2,-1])
# 翻转两个矩阵
print(b[::-1])
names = np.array(["Bob","Joe","will","Bob","will","Joe","Joe"])
# 创建一个服从正态分布7*4的数组(矩阵)
data = np.random.randn(7,4)
print(names)
print(data)
# 产生逻辑数组
print(names=="Bob")
print(data[names=='Bob'])
#打印当前索引第三个元素
print(data[names=='Bob',3])
#打印不是该索引的行
print(data[names!='Bob'])
data[data<0] = 0
print(data)
# 花式索引
arr = np.empty((8,4))
for i in range(8):
arr[i] = i
print(arr)
print(arr[[-3,-5,-7]])
# 输出arr[1,0],arr[5,3],......相当于横坐标纵坐标
arr = np.arange(32).reshape((8,4))
print(arr[[1,5,7,2],[0,3,1,2]])
print(arr[1,0])
# 数组的转置 reshape相当与重新定义数组的维度
arr =np.arange(15).reshape((3,5))
arr= arr.T
print(arr)
# 将多维数组展开成一维数组
arr = arr.ravel()
#arr = arr.flatten()
print(arr)
# 又将数组的结构变化
b.shape = (6,4)
print(b)
python学习----numpy2
最新推荐文章于 2024-05-16 22:37:33 发布