|
34 | 34 | # print(arr)
|
35 | 35 | '''请注意:arange的长度与ndarray的维度的乘积要相等,即 24 = 2X3X4'''
|
36 | 36 |
|
| 37 | +'''ndarray数组的属性''' |
| 38 | +# np.arange(4, dtype=float) |
| 39 | + |
| 40 | +# # 'D'表示复数类型 |
| 41 | +# np.arange(4, dtype='D') |
| 42 | +# np.array([1.22,3.45,6.779], dtype='int8') |
| 43 | + |
| 44 | +# ndim属性,数组维度的数量 |
| 45 | +# a = np.array([[1,2,3], [7,8,9]]) |
| 46 | +# print(a.ndim) |
| 47 | + |
| 48 | +# shape属性,数组对象的尺度,对于矩阵,即n行m列,shape是一个元组(tuple) |
| 49 | +# print(a.shape) |
| 50 | + |
| 51 | +# size属性用来保存元素的数量,相当于shape中nXm的值 |
| 52 | +# print(a.size) |
| 53 | + |
| 54 | +# itemsize属性返回数组中各个元素所占用的字节数大小。 |
| 55 | +# print(a.itemsize) |
| 56 | + |
| 57 | +# nbytes属性,如果想知道整个数组所需的字节数量,可以使用nbytes属性。其值等于数组的size属性值乘以itemsize属性值。 |
| 58 | +# print(a.nbytes) |
| 59 | +# print(a.size*a.itemsize) |
| 60 | + |
| 61 | +# T属性,数组转置 |
| 62 | +# b = np.arange(24).reshape(4,6) |
| 63 | +# print(b.T) |
| 64 | + |
| 65 | +# 复数的实部和虚部属性,real和imag属性 |
| 66 | +# d = np.array([1.2+2j, 2+3j]) |
| 67 | +# real属性返回数组的实部 |
| 68 | +# print(d.real) |
| 69 | +# imag属性返回数组的虚部 |
| 70 | +# print(d.imag) |
| 71 | + |
| 72 | +# flat属性,返回一个numpy.flatiter对象,即可迭代的对象。 |
| 73 | +# e = np.arange(6).reshape(2,3) |
| 74 | +# f = e.flat |
| 75 | +# for item in f: |
| 76 | +# print(item) |
| 77 | +# 可通过位置进行索引,如下: |
| 78 | + |
| 79 | +# print(f[2]) |
| 80 | +# print(f[[1,4]]) |
| 81 | +# 也可以进行赋值 |
| 82 | +# e.flat=7 |
| 83 | +# e.flat[[1,4]]=1 |
| 84 | + |
| 85 | + |
| 86 | +"""ndarray数组的切片和索引""" |
| 87 | +# 一维数组的切片和索引与python的list索引类似。 |
| 88 | +# 二维数组的切片和索引:1轴方向向右,0轴方向向下 |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +"""处理数组形状""" |
| 93 | +'''形状转换''' |
37 | 94 | '''函数resize()的作用跟reshape()类似,但是会改变所作用的数组,相当于有inplace=True的效果'''
|
38 | 95 | a = np.arange(12).reshape(4, 3)
|
39 | 96 | # print(a.reshape(3,4))
|
|
43 | 100 | #
|
44 | 101 | # print(b.ravel())
|
45 | 102 |
|
| 103 | +# 用tuple指定数组的形状,如下: |
| 104 | +# a.shape=(2,6) |
| 105 | + |
46 | 106 |
|
47 | 107 | '''ravel()和flatten(),将多维数组转换成一维数组,如下:
|
48 | 108 | 两者的区别在于返回拷贝(copy)还是返回视图(view),flatten()返回一份拷贝,
|
|
57 | 117 | # print(b)
|
58 | 118 |
|
59 | 119 |
|
| 120 | +'''转置''' |
| 121 | +# b = np.arange(12).reshape(4,3) |
| 122 | +# print(b.transpose()) |
| 123 | + |
| 124 | +'''堆叠数组''' |
| 125 | +# b = np.arange(12).reshape(4,3) |
| 126 | +# print(b*2) |
| 127 | + |
| 128 | +'''水平叠加hstack()''' |
| 129 | +# b = np.arange(12).reshape(4,3) |
| 130 | +# c = b*2 |
| 131 | +# print(b) |
| 132 | +# print(c) |
| 133 | +# print(np.hstack((b,c))) |
| 134 | +# column_stack()函数以列方式对数组进行叠加,功能类似hstack() |
| 135 | +# print(np.column_stack((b,c))) |
| 136 | + |
| 137 | + |
| 138 | +'''垂直叠加vstack()''' |
| 139 | +# b = np.arange(12).reshape(4,3) |
| 140 | +# c = b*2 |
| 141 | +# print(b) |
| 142 | +# print(c) |
| 143 | +# print(np.vstack((b,c))) |
| 144 | +# row_stack()函数以行方式对数组进行叠加,功能类似vstack() |
| 145 | +# print(np.row_stack((b,c))) |
| 146 | + |
| 147 | + |
| 148 | +'''concatenate()方法,通过设置axis的值来设置叠加方向''' |
| 149 | +# axis=1时,沿水平方向叠加 |
| 150 | +# axis=0时,沿垂直方向叠加 |
| 151 | + |
| 152 | +# b = np.arange(12).reshape(4,3) |
| 153 | +# c = b*2 |
| 154 | +# print(np.concatenate((b,c),axis=1)) |
| 155 | +# print(np.concatenate((b,c),axis=0)) |
| 156 | + |
| 157 | + |
| 158 | +'''深度叠加''' |
| 159 | +# b = np.arange(12).reshape(2,6) |
| 160 | +# c = b*2 |
| 161 | +# print(b) |
| 162 | +# print(c) |
| 163 | +# print('----------------------') |
| 164 | +# arr_dstack = np.dstack((b,c)) |
| 165 | +# print(arr_dstack.shape) |
| 166 | +# print(np.dstack((b,c))) |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +'''数组的拆分''' |
| 172 | +# 跟数组的叠加类似,数组的拆分可以分为横向拆分、纵向拆分以及深度拆分。 |
| 173 | +# 涉及的函数为 hsplit()、vsplit()、dsplit() 以及split() |
| 174 | +# b = np.arange(12).reshape(2,6) |
| 175 | + |
| 176 | +# 沿横向轴拆分(axis=1) |
| 177 | +# print(np.hsplit(b, 3)) |
| 178 | +# print(np.split(b,2, axis=1)) |
| 179 | + |
| 180 | +# 沿纵向轴拆分(axis=0) |
| 181 | +# print(np.vsplit(b, 2)) |
| 182 | +# print(np.split(b,2,axis=0)) |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | +'''深度拆分''' |
| 187 | +# 拆分的结果是原来的三维数组拆分成为两个二维数组。 |
| 188 | +# b = np.arange(12).reshape(2,3,2) |
| 189 | +# print(np.dsplit(b,2)) |
| 190 | + |
60 | 191 |
|
61 | 192 |
|
62 | 193 |
|
| 194 | +""""数组的类型转换""" |
| 195 | +# 数组转换成list,使用tolist() |
| 196 | +b = np.arange(12).reshape(2, 6) |
| 197 | +print(b.tolist()) |
| 198 | +print(type(b.tolist())) |
| 199 | +# 不会改变原来的numpy数组 |
63 | 200 |
|
64 | 201 |
|
| 202 | +'''一些简单练习''' |
65 | 203 |
|
66 | 204 | # weight = [65.4, 59.2, 63.6, 88.4, 68.7] #体重列表
|
67 | 205 | # height = [1.73, 1.68, 1.71, 1.89, 1.79] #身高列表
|
|
0 commit comments