目录
-
ndarray对象
numpy.array(object, dtype=None, copy=True, order=True, subok=False, ndmin=0)
1.list转ndarray
# list转换ndarray
a = [1, 2, 3]
print(type(a))
A = np.array(a)
print(type(A))
<class 'list'>
<class 'numpy.ndarray'>
2.创建一维数组
b = np.array([1, 2, 3])
print(b)
print(type(b))
[1 2 3]
<class 'numpy.ndarray'>
3.创建多维数组
c = np.array([[1, 2, 3], [4, 5, 6]])
print(c)
print(type(c))
print(c.ndim) # ndim查看数组维度
d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
print(d)
print(d.ndim)
[[1 2 3]
[4 5 6]]
<class 'numpy.ndarray'>
2
[[[1 2 3]
[4 5 6]
[7 8 9]]]
3
4.指定维度创建多维数组
e = np.array([1, 2, 3, 4, 5, 6], ndmin=3, dtype=np.float32) # ndmin指定数组维数
print(e)
[[[1. 2. 3. 4. 5. 6.]]]
5.指定dtype参数
f = np.array([1, 2, 3, 4, 5, 6], dtype=complex) # complex为复数类型
print(f)
[1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j]
-
基础数据类型
numpy.dtype(object, align, copy)
- object:被转换为数据类型的对象
- align:如果为True,则向字段添加间隔,使其类似C的结构体
- copy:生成dtype对象的新副本,如果为False,结果是内建数据类型对象的引用
1.声明一个dtype类型对象
dt = np.dtype(np.int32)
print(dt)
int32
2.使用创建的dt对象
a = np.array([1, 2, 3], dtype=dt)
print(a)
[1 2 3]
3.声明一个float类型的dtype
dt_f = np.dtype(np.float32)
b = np.array([1, 2, 3], dtype=dt_f)
print(b)
[1. 2. 3.]
4.声明一个复数类型的dtype
dt_c = np.dtype(np.complex)
c = np.array([1, 2, 3], dtype=dt_c)
print(c)
[1.+0.j 2.+0.j 3.+0.j]
-
自定义结构化类型
1.利用dtype创建自定义结构化类型
dt_struct = np.dtype([('age', np.int32)])
a = np.array([(10, ), (20, ), (30, )], dtype=dt_struct)
print(a)
print(a['age'])
[(10,) (20,) (30,)]
[10 20 30]
2.利用dtype创建自定义实体对象类型
dt_person = np.dtype([('name', 'S20'), ('age', np.int32)])
a = np.array([('han', 22), ('celine', 22)])
print(a)
[['han' '22']
['celine' '22']]
-
数组属性
1.ndarray.shape以元组的形式返回数组的形状
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(a.shape)
[[1 2 3]
[4 5 6]]
(2, 3)
2.通过shape来改变数组的形状
a.shape = (3, 2)
print(a)
[[1 2]
[3 4]
[5 6]]
3.ndarray中的ndim
b = np.array([1, 2, 3, 4, 5, 6])
print(b.ndim)
1
4.通过reshape来改变数组的维度
c = b.reshape(2, 1, 3)
print(c)
print(c.ndim)
[[[1 2 3]]
[[4 5 6]]]
3
5.ndarray.itemsize返回数组中每个元素单位字节长度
x = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
print(x.itemsize)
8
-
数组创建
1.numpy.empty以随机的方式创建数组
a = np.empty([3, 2], dtype=np.int32)
print(a)
[[-905364472 32519]
[-905364472 32519]
[-887577680 32519]]
2.numpy.zeros返回指定大小的全零数组
b = np.zeros([5, 6], dtype=np.int32)
print(b)
c = np.array([[1, 2, 3], [4, 5 ,6]], dtype=np.float32)
print(c)
d = np.zeros_like(c)
print(d)
[[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
[[1. 2. 3.]
[4. 5. 6.]]
[[0. 0. 0.]
[0. 0. 0.]]
3.numpy.ones返回指定大小的全一数组
f = np.ones([3, 4], dtype=np.float32)
print(f)
g = np.array([[1, 2, 3], [4, 5, 6]])
print(g)
h = np.ones_like(g)
print(h)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1 2 3]
[4 5 6]]
[[1 1 1]
[1 1 1]]
4.numpy.eye函数返回对角线为1,其余为0的数组(即单位矩阵)
i = np.eye(5, dtype=np.int32)
print(i)
[[1 0 0 0 0]
[0 1 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]
5.numpy.asarray()把python中的list和tuple转换成ndarray
np.asarray(a, dtype=None, order=None)
x = [1, 2, 3, 4, 5]
a = np.asarray(x)
print(a)
y = (1, 2, 3, 4, 5)
b = np.asarray(y)
print(b)
[1 2 3 4 5]
[1 2 3 4 5]
6.numpy.arange()返回给定范围的数组
np.arange([start,] stop[, step,], dtype=None)
x = np.arange(10)
print(x)
y = np.arange(10, dtype=np.float32)
print(y)
z = np.arange(10, 20, 2, dtype=np.int32)
print(z)
[0 1 2 3 4 5 6 7 8 9]
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[10 12 14 16 18]
7.numpy.linspace()返回指定范围内的均匀间隔
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
x = np.linspace(10, 20, 5)
print(x)
[10. 12.5 15. 17.5 20. ]
学习地址:https://study.163.com/course/courseMain.htm?courseId=1005813001