Numpy基础教程

我们先导入numpy库,并查看版本:

import numpy as np
np.__version__
'1.19.2'

Ⅰ.创建ndarry

1.使用np.array(),由list创建

参数为列表:[1, 4, 2, 5, 3]

注意:

  • numpy默认ndarray的所有元素的类型是相同的
  • 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str > float > int
test = np.array([1, 4, 2, 5, 3])
test
array([1, 4, 2, 5, 3])
  • python的list类型只能一维,而ndarray可以多维,例如:
test = np.array([[1, 2, 3], [4, 5, 6]])
test
array([[1, 2, 3],
       [4, 5, 6]])

两个细节:

  • 参数 ndmin:指定ndarray的维度
  • 参数 dtype:指定ndarray的数据的类型
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([1, 2, 3, 4, 5], ndmin=2, dtype=complex)
print(array1, array2)
[1 2 3 4 5] [[1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j]]

补充:numpy.asarray()函数也可以将python序列转换成ndarray数组

numpy.asarray(a, dtype = None, order = None)

构造器接受下列参数:

序号 参数及描述
1. a 任意形式的输入参数,比如列表、列表的元组、元组、元组的元组、元组的列表
2. dtype 通常,输入数据的类型会应用到返回的ndarray
3. order 'C'为按行的 C 风格数组,'F'为按列的 Fortran 风格数组
m = [1, 2, 3]
mArray = np.asarray(m, dtype=float)
mArray
array([1., 2., 3.])
n = (1, 2, 3)
nArray = np.asarray(n)
nArray
array([1, 2, 3])

嵌套序列类型亦可:

abc = [(1, 2, 3), (4, 5)]
abcArray = np.asarray(abc)
abcArray
D:\DevTools\Python\Anaconda\lib\site-packages\numpy\core\_asarray.py:83: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  return array(a, dtype, copy=False, order=order)





array([(1, 2, 3), (4, 5)], dtype=object)

2.使用np的routines函数创建

包含以下常见创建方法:


  1. np.ones(shape, dtype=None, order=‘C’)
np.ones([3, 3])
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
np.ones([3, 3], dtype = int)
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])

  1. np.zeros(shape, dtype=float, order=‘C’)
np.zeros([3, 3])
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

  1. np.full(shape, full_value, dtype=None, order=‘C’)
np.full([3, 3], 3.14)
array([[3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14]])

  1. np.eye(N, M=None, k=0, dtype=float)
np.eye(3)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

  1. np.arange(start, stop, step, dtype=None) (注意值的范围是左闭右开区间

构造器接受下列参数:

序号 参数及描述
1. start 范围的起始值,默认为0
2. stop 范围的终止值(不包含)
3. step 两个值的间隔,默认为1
4. dtype 返回ndarray的数据类型,如果没有提供,则会使用输入数据的类型。
np.arange(5)
array([0, 1, 2, 3, 4])
np.arange(0, 10, 2)
array([0, 2, 4, 6, 8])
np.arange(0, 20, 5, dtype=float)
array([ 0.,  5., 10., 15.])

  1. np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) (注意默认值的范围包含两端

此函数类似于arange()函数。但在此函数中,指定了范围之间的均匀间隔数量,而不是步长。

构造器接受下列参数:

序号 参数及描述
1. start 序列的起始值
2. stop 序列的终止值,如果endpointtrue,该值包含于序列中
3. num 要生成的等间隔样例数量,默认为50
4. endpoint 序列中是否包含stop值,默认为ture
5. retstep 如果为true,返回样例,以及连续数字之间的步长
6. dtype 输出ndarray的数据类型
np.linspace(0, 10, 5)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值