import numpy as np
arr = np.arange(10) print('*' * 10 + ' reshape 函数 ' + '*' * 10) print(f'原数组: {arr}, shape: {arr.shape}') print(f'将向量 arr 变换成 2 行 5 列后的数组: {arr.reshape([2, 5])}, shape: {arr.reshape([2, 5]).shape}') print(f'指定-1 让 numpy 自动计算该维度的大小: {arr.reshape([5, -1])}, shape: {arr.reshape([5, -1]).shape}') print(f'将列指定为 2,行数自动计算: {arr.reshape([-1, 2])}, shape: {arr.reshape([-1, 2]).shape}')
print(f'原数组仍然不变: {arr}, shape: {arr.shape}')
print('*' * 10 + ' resize 函数 ' + '*' * 10) arr = np.arange(10) print(f'原数组: {arr}, shape: {arr.shape}') arr.resize([2, 5]) print(f'将向量 arr 变换成 2 行 5 列后的数组(原数组改变): {arr}, shape: {arr.shape}')
print('*' * 10 + ' T 函数 ' + '*' * 10) arr = np.arange(12).reshape([3, 4]) print(f'原数组: \n{arr}, shape: {arr.shape}') print(f'转置后的数组: \n{arr.T}, shape: {arr.T.shape}')
print('*' * 10 + ' flatten 函数 ' + '*' * 10) arr = np.arange(12).reshape([3, 4]) print(f'原数组: \n{arr}, shape: {arr.shape}') print(f'展平后的数组: \n{arr.flatten()}, shape: {arr.flatten().shape}') print(f'原数组仍然不变: \n{arr}, shape: {arr.shape}')
print('*' * 10 + ' ravel 函数 ' + '*' * 10) arr = np.arange(6).reshape([2, -1]) print(f'原数组: \n{arr}, shape: {arr.shape}')
print(f'按列优先展平后的数组: \n{arr.ravel("F")}, shape: {arr.ravel("F").shape}') print(f'按行优先展平后的数组: \n{arr.ravel()}, shape: {arr.ravel().shape}') print(f'注意 flatten 返回的是一个新的数组,而 ravel 返回的是一个视图,改动会影响原数组,下面是一个示例:') a = np.arange(6).reshape([2, 3]) print(f'演示的数组: \n{a}')
b = a.flatten() b[0] = 999 print("a after flatten:", a)
c = a.ravel() c[0] = 888 print("a after ravel:", a)
print('*' * 10 + ' squeeze 函数 ' + '*' * 10) arr = np.arange(3).reshape([3, 1]) print(f'原数组: \n{arr}, shape: {arr.shape}') print(f'去掉维度为1的维度后的数组: \n{arr.squeeze()}, shape: {arr.squeeze().shape}') arr1 = np.arange(6).reshape([3, 1, 2, 1]) print(f'原数组: \n{arr1}, shape: {arr1.shape}') print(f'去掉维度为1的维度后的数组: \n{arr1.squeeze()}, shape: {arr1.squeeze().shape}') arr2 = np.arange(6).reshape([3, 2]) print(f'对多维数组使用 squeeze 不会报错但是无效: \n{arr2.squeeze()}, shape: {arr2.squeeze().shape}')
print('*' * 10 + ' transpose 函数 ' + '*' * 10) arr = np.arange(24).reshape(2, 3, 4) print(f'原数组: \n{arr}, shape: {arr.shape}') print(f'转置后的数组: \n{arr.transpose(1, 2, 0)}, shape: {arr.transpose(1, 2, 0).shape}')
print('*' * 10 + ' append 函数 ' + '*' * 10)
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = np.append(a, b) print(f'用 append 合并一维数组的结果: {c}, shape: {c.shape}')
a = np.arange(4).reshape(2, 2) b = np.arange(4).reshape(2, 2)
c = np.append(a, b, axis=0) print(f'用 append 按行合并二维数组的结果:\n{c}, shape: {c.shape}')
d = np.append(a, b, axis=1) print(f'用 append 按列合并二维数组的结果:\n{d}, shape: {d.shape}')
print('*' * 10 + ' concatenate 函数 ' + '*' * 10) a = np.arange(1, 5).reshape(2, 2) b = np.array([[5, 6]]) c = np.concatenate((a, b), axis=0) print(f'用 concatenate 按行合并二维数组和一维数组的结果:\n{c}, shape: {c.shape}') d = np.concatenate((a, b.T), axis=1) print(f'用 concatenate 按列合并二维数组和一维数组的结果:\n{d}, shape: {d.shape}')
print('*' * 10 + 'stack 函数 ' + '*' * 10) a = np.arange(1, 5).reshape(2, 2) b = np.array([[5, 6], [7, 8]]) c = np.stack((a, b), axis=0) print(f'用 stack 在第0轴堆叠二维数组的结果:\n{c}, shape: {c.shape}') d = np.stack([a, b], axis=1) print(f'用 stack 在第1轴堆叠二维数组的结果:\n{d}, shape: {d.shape}')
print('*' * 10 + 'zip 函数' + '*' * 10) a = np.arange(1, 5).reshape(2, 2) b = np.arange(5, 9).reshape(2, 2) c = list(zip(a, b)) print(f'用 zip 合并二维数组的结果:\n{c}, length: {len(c)}')
a = [1, 2, 3] b = [4, 5, 6] c = zip(a, b) for i, j in c: print(i, end=', ') print(j)
a = [(1, 4), (2, 5), (3, 6)] b, c = zip(*a) print(f'用 zip 解压后的结果: {b}, {c}')
|