1. ndarray基本属性:arr.dytpe , arr.shape
2. 数组构建
3. 数组类型 type(arr)
4. 数组属性修改:arr.reshape(…), arr.astype(‘…’)
5. str.format()函数
6. np.dtype()结构化数据类型
8){‘names’: …, ‘formats’: …, ‘offsets’: …, ‘titles’: …, ‘itemsize’: …}格式
dt = np.dtype({'names': ['r','g','b','a'],'formats': [uint8, uint8, uint8, uint8]})
dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})
6) T:逗号分隔的多个类型字符串
t = np.dytpe('U14 , i4') # 对数组两列进行数据类型指定
7)T: 结构化数据类型[(名称,类型,维度还是长度???),(字段描述),...] .....需要灵活掌握
t = np.dtype([('name', 'float',14), ('ages', np.int32])
- 基本属性
属性 | 说明 |
---|---|
dtype | 元素类型 |
shape | 维度 |
ndim | 维数 |
size | 元素数,即维度shape元组的各元素乘积(D2:row_num * col_num / a.shape[0]*a.shape[1]) |
len | 数组长度 即第一维数 a.shape[0] (D2: row_num) |
itemsize | 每个元素的字节数 |
nbytes | 数组总字节数 nbytes = itemsize * size |
T | 转置视图 |
real | 复数数组的实部视图 |
imag | 复数数组的虚部视图 |
flat | 扁平迭代器 |
tolist | 转化为python列表 |
a = np.arange(1,13).reshape(2,2,3)
a
Out[45]:
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
a.flat
Out[46]: <numpy.flatiter at 0x5e88150>
for item in a.flat: # a.flat 扁平迭代器(一维数组),可切片,可赋值
print(item, end = '|')
output: 1|2|3|4|5|6|7|8|9|10|11|12|
a.flat[[2,3,7]]
Out[51]: array([3, 4, 8])
a.tolist()
Out[53]: [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]
python内置类型:
bool , int, float, complex, str, tuple, list, dict, setnumpy内置类型:
bool_,
int8(i1) / int16(i2) / int32(i4) / int64(i8)…,
uint8(u1) / uint16 / uint32(u4) / uint64…, (int 和uint 值域不同,且uint不含负数)
float16() / float32 / float64,
complex64 / complex128,
str_(s20 长度20的字符串)自定义类型:(常用1,2,6,7)
t = np.dtype(T)
a = np.array([…], dtype=t)
或
a = np.array([…], dtype=T)1) T:python内置类型 或 numpy内置类型
t = np.dtype(int) # python内置类型, 没有 ' ' t = np.dtype(np.int32) # numpy内置类型
2)T:类型字符串
t = np.dtype('int') # 有引号 ' ' t = np.dtype('int32')
3)T:类型字符编码 (不推荐使用)
t = np.dtype(‘>(2,3)4i4’)
说明:>: 大端字节序 (2,3): 维度 4: 分量数 i: 分量类型 4: 分量字节数
4) T:tuple (变长类型,长度) ….不推荐
t = np.dtype((np.str_, 14))5) T:tuple (定长类型,维度) ….不推荐
t = np.dtype((int32,5))6) T:逗号分隔的多个类型字符串
t = np.dytpe(‘U14 , i4‘)7)T: 结构化数据类型 [(名称,类型,维度),(字段描述),…] …..需要灵活掌握
t = np.dtype([(‘name’, np.str_,14), (‘age’, np.int32)])
NOTE: 相当于MySQL中给字段名进行类型设置
8){‘names’: …, ‘formats’: …, ‘offsets’: …, ‘titles’: …, ‘itemsize’: …}格式:
dt = np.dtype({'names': ['r','g','b','a'],'formats': [uint8, uint8, uint8, uint8]})
dt = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'offsets': [0, 2],'titles': ['Red pixel', 'Blue pixel']})
f = np.array([('abc', 1243) ,('def', 3456)], dtype = 'U14, i4')
f
Out[26]:
array([('abc', 1243), ('def', 3456)],
dtype=[('f0', '<U14'), ('f1', '<i4')])
f[1][0]
Out[27]: 'def'
f.dtype
Out[28]: dtype([('f0', '<U14'), ('f1', '<i4')])
g = np.array([('abc', 1243) ,('def', 3456)], dtype = [('name',np.str_,14),('age',np.int32)])
g
Out[30]:
array([('abc', 1243), ('def', 3456)],
dtype=[('name', '<U14'), ('age', '<i4')])
g[1]['name']
Out[31]: 'def'
- 函数dtype() :结构化数组类型
结构化数据类型
In [3]: student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
In [4]: print student
[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]
将其应用于 ndarray 对象
In [5]: a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
In [6]: print a
[('abc', 21, 50.) ('xyz', 18, 75.)]
采用文件名称来访问name, age, marks列的内容
In [7]: print a['name']
['abc' 'xyz']
In [8]: print a['marks']
[ 50. 75.]
In [9]: print a['age']
[21 18]
- code:
import os
import sys
import numpy as np
def main (argc, argv, envp):
one_dim = np.arange(1,5)
one_dim_list = list(one_dim)
print('array output:', one_dim) # array 输出: [1 2 3 4]
print('list output:', one_dim_list) # list 输出: [1, 2, 3, 4]
print('容器类型:', type(one_dim)) # 容器类型 : <class 'numpy.ndarray'>
# 数组array的基本属性
print('元素类型:', one_dim.dtype) # 元素类型 : int32
print('容器形状:', one_dim.shape) # 容器形状 : (4,)
# array.reshape(x,y,z) 重新shape数组形状
three_dim = np.arange(1,25,1).reshape(2,3,4)
print('数组reshape:', three_dim.shape) # 数组形状 : (2, 3, 4)
# array.astype('float/int..') 修改元素类型
three_dim = np.arange(1,25,1).reshape(2,3,4).astype('float')
print('数组astype:', three_dim.dtype) # 元素类型 : float64
print('three_dim:', '\n', three_dim)
# 循环输出3D-array数组元素
print('\n','循环输出3D-array数组元素:')
for i in range(three_dim.shape[0]): # in range(2): three_dim.shape[0]==2
for j in range(three_dim.shape[1]): # range 一定不能漏掉
for k in range(three_dim.shape[2]):
# str.format函数, int()函数:
# {:^5d}.format(int) 整数 宽度为5 中间对齐
print('{:^5d}'.format(int(three_dim[i,j,k])), end = '/')
print('-', end = '/')
# {:10}'.format() 浮点数 宽度为10 右对齐
print('{:10}'.format(three_dim[i,j,k]), end = '')
print() # 内层循环结束后换行
print() # 内层循环结束后换行
return 0
if __name__ == '__main__':
sys.exit(main(len(sys.argv), sys.argv, os.environ))
输出结果:
array output: [1 2 3 4]
list output: [1, 2, 3, 4]
容器类型: <class 'numpy.ndarray'>
元素类型: int32
容器形状: (4,)
数组reshape: (2, 3, 4)
数组astype: float64
three_dim:
[[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 9. 10. 11. 12.]]
[[ 13. 14. 15. 16.]
[ 17. 18. 19. 20.]
[ 21. 22. 23. 24.]]]
循环输出3D-array数组元素:
1 /-/ 1.0 2 /-/ 2.0 3 /-/ 3.0 4 /-/ 4.0
5 /-/ 5.0 6 /-/ 6.0 7 /-/ 7.0 8 /-/ 8.0
9 /-/ 9.0 10 /-/ 10.0 11 /-/ 11.0 12 /-/ 12.0
13 /-/ 13.0 14 /-/ 14.0 15 /-/ 15.0 16 /-/ 16.0
17 /-/ 17.0 18 /-/ 18.0 19 /-/ 19.0 20 /-/ 20.0
21 /-/ 21.0 22 /-/ 22.0 23 /-/ 23.0 24 /-/ 24.0
[Finished in 1.3s]
- str.format() 参考:
http://www.runoob.com/python/att-string-format.html
补充常用np方法:
1、np.append()方法:数组增加元素
a
Out[66]: array([1, 3, 5, 7, 9])
c = np.append(a,100)
c
Out[68]: array([ 1, 3, 5, 7, 9, 100])
2、np.linspace()语法:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
help(np.linspace)
Help on function linspace in module numpy.core.function_base:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Return evenly spaced numbers over a specified interval.
Returns `num` evenly spaced samples, calculated over the
interval [`start`, `stop`].
The endpoint of the interval can optionally be excluded.
Parameters
----------
start : scalar
The starting value of the sequence.
stop : scalar
The end value of the sequence, unless `endpoint` is set to False.
In that case, the sequence consists of all but the last of ``num + 1``
evenly spaced samples, so that `stop` is excluded. Note that the step
size changes when `endpoint` is False.
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
endpoint : bool, optional
If True, `stop` is the last sample. Otherwise, it is not included.
Default is True.
retstep : bool, optional
If True, return (`samples`, `step`), where `step` is the spacing
between samples.
dtype : dtype, optional
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.
.. versionadded:: 1.9.0
Returns
-------
samples : ndarray
There are `num` equally spaced samples in the closed interval
``[start, stop]`` or the half-open interval ``[start, stop)``
(depending on whether `endpoint` is True or False).
step : float, optional
Only returned if `retstep` is True
Size of spacing between samples.
See Also
--------
arange : Similar to `linspace`, but uses a step size (instead of the
number of samples).
logspace : Samples uniformly distributed in log space.
Examples
--------
np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
Graphical illustration:
import matplotlib.pyplot as plt
N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.plot(x1, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
plt.ylim([-0.5, 1])
(-0.5, 1)
plt.show()