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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| a = [1,2,3] b = [4,5,6] c = [4,5,6,7,8] zipped = zip(a,b) zip(a,c) zip(*zipped) X=np.array([[1,2,3][4,5,6]]) a=np.arange(9).reshape(3,3) a Out[31]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' 注意,由于数组可以为高维,所以在此处需要用元组来包裹其尺寸。 ''' Z0 = np.zeros((2,2)) print Z0 z1=np.empty((2,)) b = np.ones((1,2)) print b c = np.full((2,2), 7) print c I = np.eye(2) print I e = np.random.random((2,2)) print e 扩展矩阵函数tile() np.tile(a,(m,n)) >>>x=np.array([0,0,0]) >>> x [[0, 0, 0]] >>> tile(x,(3,1)) matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) >>> tile(x,(2,2)) matrix([[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]) ''' 矩阵合并函数 ''' hstack : Stack arrays in sequence horizontally (column wise). vstack : Stack arrays in sequence vertically (row wise). dstack : Stack arrays in sequence depth wise (along third axis). concatenate : Join a sequence of arrays together. r_ : Translates slice objects to concatenation along the first axis. c_ : Translates slice objects to concatenation along the second axis. ''' 使用np.c_[]和np.r_[]分别添加行和列 注:该方法只能将两个矩阵合并,不会改变原矩阵的维度 ''' np.c_[a,b] ''' 将b以列的形式拼接至a的后面 ''' newarray=numpy.insert(arr, obj, values, axis=None) ''' arr:被插入的矩阵 obj:要被插入的行(列)位置,将会插入到它的前一行(列) values:插入值(矩阵) axis:轴值,若未填入则矩阵会被展开,为0则插入行,1则插入列。 ''' array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) ''' 取矩阵的某一行 ''' a[1] Out[32]: array([3, 4, 5]) ''' 取矩阵的某一列 ''' a[:,1] Out[33]: array([1, 4, 7]) a.reshape(3, 4, -1) a.T a.transpose() numpy.linalg.inv(a) a.diagonal([offset, axis1, axis2]) np.linalg.norm(np_c1 - np_c2) numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) Out:[start,start+step…]
|