python numpy库学习之数组变形,拼接

import numpy as np

# x = np.array([1, 2, 3])
# x1 = x.reshape((1, 3))
# x2 = x.reshape((3, 1))
# print(x, '\n', x1, '\n', x2,'\n')


t = np.arange(0, 16)
# print(t)
t1 = t.reshape(2, 8)
# print(t1)
# t2 = t1.reshape(4, 4)
# print(t2)
# t3 = t1.reshape(4, 4, order='F')
# print(t3)
t4 = t1.reshape(4, 4, order='C')
# print(t4)

# print('以 C 风格顺序排序:')
# for x in np.nditer(t4, order='C'):
#     print(x, end=", ")
# print('\n')
# print('以 F 风格顺序排序:')
# for x in np.nditer(t4, order='F'):
#     print(x, end=", ")
# print('\n' + "修改元素:")
# t4[1, 1] = 100
# print(t4)
# t4[0] = 100
# print(t4)
# t4[2] = 100, 200, 300, 400
# print(t4)

# print("转置数组:")
# t5 = np.transpose(t4)
# print(t5)

# print("连接数组:")
# hstack	水平堆叠序列中的数组(列方向) np.hstack([x, y] 等价于[x y]   要求x y行相同
# vstack	竖直堆叠序列中的数组(行方向) np.vstack([x, y] 等价于[x
#                                                               y]     要求x y列相同 
# x = np.array([1, 2, 3])
# y = np.array([4, 5, 6])
# z = np.random.randint(1, 10, (3, 3))
# print("x = ", x, '\n', "y = ", y, '\n', "z = ", z)
# # print("行方向连接,相当于在末尾添加一行", '\n', np.vstack([x, y]))
# # print("列方向连接", '\n', np.hstack([x, y]))    #同样你也可以插入一列,或者末尾添加一列,或者删除一列
# # print("行方向连接", '\n', np.vstack([x, z]))

# print("行方向连接,相当于插入一行", '\n', np.vstack([z[0, :], x, z[1:, :]]))
# x = x.reshape((3,1))
# print("列方向连接,相当于插入一列", '\n', np.hstack([z[:, 0:1], x, z[:, 1:]]))

# print(z[:, 0])
# print(z[:, 0:1])

#z[:, 0] 列向量他是一维
#z[:, 0:1] 是个二维的

# k = np.random.randint(1, 10, (3, 3))
# print(k)
# print("行方向连接,相当于删除一行", '\n', np.vstack([k[0, :], k[2, :]]))
# print("列方向连接,相当于删除一列", '\n', np.hstack([k[:, 1:2], k[:, 2:]]))


# print("去重")
# uu = np.random.randint(1, 10, (3, 3))
# print(uu)
# ff = np.unique(uu)
# print("去重", '\n', ff)


print("排序")
kk = np.random.randint(1, 10, (3, 3))
print(kk)
hh = np.sort(kk)  # axis=0 按列排序,axis=1 按行排序,默认按行排
hh1 = np.sort(kk, axis=0 )
print("按行排:", '\n', hh)
print("按列排:", '\n', hh1)

index = np.argsort(kk)
print("按行排的下标索引:", '\n', index)

哔哩哔哩视频链接 https://www.bilibili.com/video/BV1Lf4y197ov

python numpy库学习之数组变形,拼接

猜你喜欢

转载自blog.csdn.net/qq_43657442/article/details/107951648