ndarray对象属性的基本操作
1.numpy对象的基本数据类型
1.1基本数据类型
类型表示方法类型字符码(简写)
布尔型bool?有符号整形int8/int16/int32/int64i1/i2/i3/i4无符号整型uint8/uint16/uint32/uint64u1/u2/u3/u4浮点型float16/float32/float64f2/f4/f8复数型complex64/complex128c8/c16字符串型str,每个字符用32位Unicode编码表示M8[Y],M8[M],M8[D],M8[h],M8[m],M8[s]
1.2数据对象的构造方式
-np.array() -np.arrange(起始值,终止值,步长) -np.zeros() -np.ones() -np.zeros_like() -np.ones_like()
1.3自定义复合类型的三种表示方法
data
= [('zs', [90, 70, 88], 15),('ls', [95, 76, 81], 16),('ww', [67, 79, 84], 17)]
a
= np
.array
(data
, dtype
='U2, 3int32, int32')
print(a
)
print("ww age:", a
[2][2])
print("ww grade:", a
[2]["f1"])
b
= np
.array
(data
, dtype
=[("name", "str", 2),("scores", "int32", 3),("age", "int32",1)])
print(b
)
print("ww grade:", b
[2]["scores"])
c
= np
.array
(data
, dtype
={"names": ["name", "scores", "age"],"formats": ["U2", "3int32", "int32"]})
print(c
)
print("ls grade:", c
[1]["scores"])
1.3日期类型数组
data
= ["2011", "2011-01-02", "2012-01-01", "2012-02-01 10:10:10"]
dates
= np
.array
(data
)
print(dates
, dates
.dtype
)
dates
= dates
.astype
("M8[D]")
print(dates
, dates
.dtype
)
print(dates
[2] - dates
[0])
2.numpy属性的基本操作
2.1基本属性如下
基本属性表示方法
维度shape元素类型dtype元素数量size维数ndim,len(shape)元素字节数itemsize总字节数nbytes=size*itemsize复数数组的实部数组real复数数组的虚部数组imag数组对象的转置视图T扁平迭代器flat
a
= np
.array
([[1+1j, 2+4j, 3+7j],[4+2j, 5+5j, 6+8j],[7+3j, 8+6j, 9+9j]])
print(a
.shape
)
print(a
.dtype
)
print(a
.ndim
)
print(a
.size
)
print(a
.itemsize
)
print(a
.nbytes
)
print(a
.real
, a
.imag
, sep
="\n")
print(a
.T
)
print([elem
for elem
in a
.flat
])
b
= a
.tolist
()
print(b
, type(b
))
2.2ndarray数组对象的维度操作
a
= np
.arange
(1, 9)
print(a
, a
.shape
)
b
= a
.reshape
(2, 4)
print(b
, b
.shape
)
b
[0, 0] = 10
print(a
, a
.shape
)
c
= b
.reshape
(2, 2, 2)
print(c
, c
.shape
)
d
= c
.ravel
()
print(d
, d
.shape
)
e
= b
.flatten
()
print("e1:", e
)
b
[0, 0] = 1
print("e2:", e
)
f
= b
.flatten
("F")
print(f
)
g
= b
.copy
()
print(g
)
e
.shape
= (2, 2, 2)
print(e
)
e
.resize
(9)
print(e
)
3.ndarray数据切片操作
3.1基本思想
-数组对象切片的参数设置与列表切面参数类似 -步长+:默认切从首到尾 -步长-:默认切从尾到首 -数组对象[起始位置:终止位置:步长]
3.2一维数组切片
a
= np
.arange
(1, 10)
print(a
)
print(a
[:3])
print(a
[3:6])
print(a
[6:])
print(a
[::-1])
print(a
[:-4:-1])
print(a
[-4:-7:-1])
print(a
[-7::-1])
print(a
[::])
print(a
[:])
print(a
[::3])
print(a
[1::3])
print(a
[2::3])
3.3多维数组切片
a
= np
.arange
(1, 28).reshape
(3, 3, 3)
print(a
)
print(a
[:, :2, :2])
3.4多维数组的组合与切分
a
= np
.arange
(1, 7).reshape
(2, 3)
b
= np
.arange
(7, 13).reshape
(2, 3)
print(a
)
print(b
)
c
= np
.vstack
((a
, b
))
d
, e
= np
.vsplit
(c
, 2)
print(c
)
print(d
)
print(e
)
c
= np
.hstack
((a
, b
))
d
, e
= np
.hsplit
(c
, 2)
print(c
)
print(d
)
print(e
)
c
= np
.dstack
((a
, b
))
d
, e
= np
.dsplit
(c
, 2)
print(c
)
print(d
)
print(e
)
'''
0:垂直方向组合
1:水平方向组合
2:深度方向组合, a,b必须是三维数组
'''
c
= np
.concatenate
((a
, b
), axis
=1)
print(c
)
d
, e
= np
.split
(c
, 2, axis
=0)
print(d
)
print(e
)
3.5简单的一维数组组合方案
a
= np
.arange
(1, 9)
b
= np
.arange
(9, 17)
c
= np
.row_stack
((a
, b
))
d
= np
.column_stack
((a
, b
))
print(c
)
print(d
)
4.ndarray数组的掩码操作
a
= np
.arange
(1, 100)
b
= []
mask
= [True, False, True, False, True, False, True, False, True]
print(a
[(a
% 3 == 0) & (a
% 7 == 0)])
b
= np
.array
(["A", "B", "C", "D"])
mask
= [3, 0, 2, 0, 0]
print(b
[mask
])
本博客仅为自学习使用,如有侵权请联系删除