1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import numpy as np
x = np.arange(10) print('x: ', x)
X = np.arange(15).reshape(3, 5) print('X:', X)
print('x维度:', x.ndim) print('X维度:', X.ndim) print('X.shape: ', X.shape) print('X.size: ', X.size)
print('X:', X) print('X[0][0]: (不推荐)', X[0][0]) print('X[2, 2]: (推荐) ', X[2, 2]) print('X[:2, :3]: (前2行前3列)', X[:2, :3]) print('X[:2][:3]: (预料不符)', X[:2][:3]) print('X[:2, ::2]: ', X[:2, ::2]) print('X[: , 0]: (取一列) ', X[:, 0])
subX = X[:2, :3] print("subX:", subX) print("X: ", X)
X[0, 0] = 999 print("subX:", subX) print("X: ", X)
subX = X[:2, :3].copy() X[0, 0] = 888 print("subX:", subX) print("X: ", X)
print('x.shape: ', x.shape) A = x.reshape((2, 5)) print('x.reshape(2, 5): ', A)
tenlines = x.reshape(10, -1) print("转换为10行: ", tenlines)
[out]: x: [0 1 2 3 4 5 6 7 8 9] X: [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] x维度: 1 X维度: 2 X.shape: (3, 5) X.size: 15 X: [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] X[0][0]: (不推荐) 0 X[2, 2]: (推荐) 12 X[:2, :3]: (前2行前3列) [[0 1 2] [5 6 7]] X[:2][:3]: (预料不符) [[0 1 2 3 4] [5 6 7 8 9]] X[:2, ::2]: [[0 2 4] [5 7 9]] X[: , 0]: (取一列) [ 0 5 10] subX: [[0 1 2] [5 6 7]] X: [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]] subX: [[999 1 2] [ 5 6 7]] X: [[999 1 2 3 4] [ 5 6 7 8 9] [ 10 11 12 13 14]] subX: [[999 1 2] [ 5 6 7]] X: [[888 1 2 3 4] [ 5 6 7 8 9] [ 10 11 12 13 14]] x.shape: (10,) x.reshape(2, 5): [[0 1 2 3 4] [5 6 7 8 9]] [[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]]
|