Numpy基础

目录

ndarray对象

基础数据类型

自定义结构化类型

数组属性

数组创建


  • 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值