“Python numpy的简单应用”的版本间的差异
来自CloudWiki
(创建页面,内容为“导入模块 >>> import numpy as np 生成数组 <nowiki>>>> np.array([1, 2, 3, 4, 5]) # 把列表转换为数组 array([1, 2, 3, 4, 5]) >>> np.array((1,…”) |
(→数组与数组的运算) |
||
(未显示同一用户的4个中间版本) | |||
第1行: | 第1行: | ||
− | 导入模块 | + | ==numpy的安装== |
+ | pip3 install numpy | ||
+ | |||
+ | ==numpy的简单使用== | ||
+ | |||
+ | ===导入模块=== | ||
>>> import numpy as np | >>> import numpy as np | ||
− | + | ===创建数组=== | |
<nowiki>>>> np.array([1, 2, 3, 4, 5]) # 把列表转换为数组 | <nowiki>>>> np.array([1, 2, 3, 4, 5]) # 把列表转换为数组 | ||
第17行: | 第22行: | ||
>>> np.arange(1, 10, 2) | >>> np.arange(1, 10, 2) | ||
array([1, 3, 5, 7, 9])</nowiki> | array([1, 3, 5, 7, 9])</nowiki> | ||
+ | |||
+ | ===数组属性=== | ||
+ | <nowiki>>>> import numpy as np | ||
+ | >>> a = np.ones((4,5)) | ||
+ | >>> print(a) | ||
+ | [[1. 1. 1. 1. 1.] | ||
+ | [1. 1. 1. 1. 1.] | ||
+ | [1. 1. 1. 1. 1.] | ||
+ | [1. 1. 1. 1. 1.]] | ||
+ | >>> a.ndim | ||
+ | 2 | ||
+ | >>> a.shape | ||
+ | (4, 5) | ||
+ | >>> a.dtype | ||
+ | dtype('float64')</nowiki> | ||
+ | ===创建各种数组=== | ||
+ | <nowiki>>>> np.linspace(0,10,11) | ||
+ | array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> a = np.logspace(0,9,10) | ||
+ | >>> a | ||
+ | array([ 1.00000000e+00, 1.00000000e+01, 1.00000000e+02, | ||
+ | 1.00000000e+03, 1.00000000e+04, 1.00000000e+05, | ||
+ | 1.00000000e+06, 1.00000000e+07, 1.00000000e+08, | ||
+ | 1.00000000e+09]) | ||
+ | >>> a = np.logspace(0,9,10,base=2) | ||
+ | >>> a | ||
+ | array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.]) | ||
+ | >>> np.zeros(3) | ||
+ | array([0., 0., 0.]) | ||
+ | >>> np.ones(3) | ||
+ | array([1., 1., 1.]) | ||
+ | >>> np.zeros((3,3)) | ||
+ | array([[0., 0., 0.], | ||
+ | [0., 0., 0.], | ||
+ | [0., 0., 0.]]) | ||
+ | >>> np.zeros((3,1)) | ||
+ | array([[0.], | ||
+ | [0.], | ||
+ | [0.]]) | ||
+ | >>> np.zeros((1,3)) | ||
+ | array([[0., 0., 0.]]) | ||
+ | >>> np.ones((1,3)) | ||
+ | array([[1., 1., 1.]]) | ||
+ | >>> np.ones((3,3)) | ||
+ | array([[1., 1., 1.], | ||
+ | [1., 1., 1.], | ||
+ | [1., 1., 1.]]) | ||
+ | </nowiki> | ||
+ | |||
+ | |||
+ | <nowiki>>>> np.identity(3) #单位矩阵 | ||
+ | array([[1., 0., 0.], | ||
+ | [0., 1., 0.], | ||
+ | [0., 0., 1.]]) | ||
+ | >>> np.identity(2) #单位矩阵 | ||
+ | array([[1., 0.], | ||
+ | [0., 1.]]) | ||
+ | >>> np.hamming(20) #Hamming窗口 | ||
+ | array([0.08 , 0.10492407, 0.17699537, 0.28840385, 0.42707668, | ||
+ | 0.5779865 , 0.7247799 , 0.85154952, 0.94455793, 0.9937262 , | ||
+ | 0.9937262 , 0.94455793, 0.85154952, 0.7247799 , 0.5779865 , | ||
+ | 0.42707668, 0.28840385, 0.17699537, 0.10492407, 0.08 ]) | ||
+ | >>> np.blackman(10) #Blackman窗口 | ||
+ | array([-1.38777878e-17, 5.08696327e-02, 2.58000502e-01, 6.30000000e-01, | ||
+ | 9.51129866e-01, 9.51129866e-01, 6.30000000e-01, 2.58000502e-01, | ||
+ | 5.08696327e-02, -1.38777878e-17]) | ||
+ | >>> np.kaiser(12,5) #Kaiser窗口 | ||
+ | array([0.03671089, 0.16199525, 0.36683806, 0.61609304, 0.84458838, | ||
+ | 0.98167828, 0.98167828, 0.84458838, 0.61609304, 0.36683806, | ||
+ | 0.16199525, 0.03671089]) | ||
+ | >>> np.random.randint(0,50,5) #随机数组,5个0到50之间的整数 | ||
+ | array([ 9, 36, 27, 32, 45]) | ||
+ | >>> np.random.randint(0,50,(3,5)) #3行5列,15个介于0和50之间的整数 | ||
+ | array([[24, 37, 39, 0, 2], | ||
+ | [35, 30, 3, 44, 42], | ||
+ | [16, 18, 10, 28, 32]]) | ||
+ | >>> np.random.rand(10) #10个随机小数 | ||
+ | array([0.52956314, 0.14924464, 0.17543161, 0.88862925, 0.81509813, | ||
+ | 0.92279044, 0.05609938, 0.39233243, 0.11169266, 0.92278519])</nowiki> | ||
+ | |||
+ | ==numpy的简单使用== | ||
+ | ===测试两个数组是否足够接近=== | ||
+ | |||
+ | <nowiki>>>> x = np.array([1, 2, 3, 4.001, 5]) | ||
+ | >>> y = np.array([1, 1.999, 3, 4.01, 5.1]) | ||
+ | >>> np.allclose(x, y) | ||
+ | False | ||
+ | >>> np.allclose(x, y, rtol=0.2) # 设置相对误差参数 | ||
+ | True | ||
+ | >>> np.allclose(x, y, atol=0.2) # 设置绝对误差参数 | ||
+ | True</nowiki> | ||
+ | |||
+ | ===改变数组元素值=== | ||
+ | |||
+ | <nowiki>>>> x = np.arange(8) | ||
+ | >>> x | ||
+ | array([0, 1, 2, 3, 4, 5, 6, 7]) | ||
+ | >>> np.append(x, 8) # 返回新数组,增加元素 | ||
+ | array([0, 1, 2, 3, 4, 5, 6, 7, 8]) | ||
+ | >>> np.append(x, [9,10]) | ||
+ | array([0, 1, 2, 3, 4, 5, 6, 7, 9, 10]) | ||
+ | >>> x # 不影响原来的数组 | ||
+ | array([0, 1, 2, 3, 4, 5, 6, 7]) | ||
+ | >>> x[3] = 8 # 原地修改元素值 | ||
+ | >>> x | ||
+ | array([0, 1, 2, 8, 4, 5, 6, 7]) | ||
+ | >>> np.insert(x, 1, 8) # 返回新数组,插入元素 | ||
+ | |||
+ | >>> x.repeat(3) # 元素重复,返回新数组 | ||
+ | array([0, 0, 0, 1, 1, 1, 2, 2, 2, 8, 8, 8, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7]) | ||
+ | >>> x.put(0, 9) # 修改指定位置上的元素值 | ||
+ | >>> x | ||
+ | array([9, 1, 2, 8, 4, 5, 6, 7]) | ||
+ | >>> x = np.array([[1,2,3], [4,5,6], [7,8,9]]) | ||
+ | >>> x[0, 2] = 4 # 修改第0行第2列的元素值 | ||
+ | >>> x | ||
+ | array([[1, 2, 4], | ||
+ | [4, 5, 6], | ||
+ | [7, 8, 9]]) | ||
+ | </nowiki> | ||
+ | |||
+ | |||
+ | ==numpy运算== | ||
+ | ===数组与数值的运算=== | ||
+ | |||
+ | <nowiki>>>> x = np.array((1, 2, 3, 4, 5)) # 创建数组对象 | ||
+ | >>> x | ||
+ | array([1, 2, 3, 4, 5]) | ||
+ | >>> x * 2 # 数组与数值相乘,返回新数组 | ||
+ | array([ 2, 4, 6, 8, 10]) | ||
+ | >>> x / 2 # 数组与数值相除 | ||
+ | array([ 0.5, 1. , 1.5, 2. , 2.5]) | ||
+ | >>> x // 2 # 数组与数值整除 | ||
+ | array([0, 1, 1, 2, 2], dtype=int32) | ||
+ | >>> x ** 3 # 幂运算 | ||
+ | array([1, 8, 27, 64, 125], dtype=int32) | ||
+ | >>> x + 2 # 数组与数值相加 | ||
+ | array([3, 4, 5, 6, 7]) | ||
+ | >>> x % 3 # 余数 | ||
+ | array([1, 2, 0, 1, 2], dtype=int32)</nowiki> | ||
+ | |||
+ | |||
+ | ===数组与数组的运算=== | ||
+ | |||
+ | <nowiki>>>> a = np.array((1, 2, 3)) | ||
+ | >>> b = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9])) | ||
+ | >>> c = a * b # 数组与数组相乘 | ||
+ | >>> c # a中的每个元素乘以b中的对应列元素 | ||
+ | array([[ 1, 4, 9], | ||
+ | [ 4, 10, 18], | ||
+ | [ 7, 16, 27]]) | ||
+ | >>> c / b # 数组之间的除法运算 | ||
+ | array([[ 1., 2., 3.], | ||
+ | [ 1., 2., 3.], | ||
+ | [ 1., 2., 3.]]) | ||
+ | >>> c / a | ||
+ | array([[ 1., 2., 3.], | ||
+ | [ 4., 5., 6.], | ||
+ | [ 7., 8., 9.]])</nowiki> | ||
+ | |||
+ | 返回 [[Python程序设计艺术]] |
2018年5月30日 (三) 10:22的最新版本
目录
numpy的安装
pip3 install numpy
numpy的简单使用
导入模块
>>> import numpy as np
创建数组
>>> np.array([1, 2, 3, 4, 5]) # 把列表转换为数组 array([1, 2, 3, 4, 5]) >>> np.array((1, 2, 3, 4, 5)) # 把元组转换成数组 array([1, 2, 3, 4, 5]) >>> np.array(range(5)) # 把range对象转换成数组 array([0, 1, 2, 3, 4]) >>> np.array([[1, 2, 3], [4, 5, 6]]) # 二维数组 array([[1, 2, 3], [4, 5, 6]]) >>> np.arange(8) # 类似于内置函数range() array([0, 1, 2, 3, 4, 5, 6, 7]) >>> np.arange(1, 10, 2) array([1, 3, 5, 7, 9])
数组属性
>>> import numpy as np >>> a = np.ones((4,5)) >>> print(a) [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] >>> a.ndim 2 >>> a.shape (4, 5) >>> a.dtype dtype('float64')
创建各种数组
>>> np.linspace(0,10,11) array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) >>> >>> >>> a = np.logspace(0,9,10) >>> a array([ 1.00000000e+00, 1.00000000e+01, 1.00000000e+02, 1.00000000e+03, 1.00000000e+04, 1.00000000e+05, 1.00000000e+06, 1.00000000e+07, 1.00000000e+08, 1.00000000e+09]) >>> a = np.logspace(0,9,10,base=2) >>> a array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.]) >>> np.zeros(3) array([0., 0., 0.]) >>> np.ones(3) array([1., 1., 1.]) >>> np.zeros((3,3)) array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) >>> np.zeros((3,1)) array([[0.], [0.], [0.]]) >>> np.zeros((1,3)) array([[0., 0., 0.]]) >>> np.ones((1,3)) array([[1., 1., 1.]]) >>> np.ones((3,3)) array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])
>>> np.identity(3) #单位矩阵 array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) >>> np.identity(2) #单位矩阵 array([[1., 0.], [0., 1.]]) >>> np.hamming(20) #Hamming窗口 array([0.08 , 0.10492407, 0.17699537, 0.28840385, 0.42707668, 0.5779865 , 0.7247799 , 0.85154952, 0.94455793, 0.9937262 , 0.9937262 , 0.94455793, 0.85154952, 0.7247799 , 0.5779865 , 0.42707668, 0.28840385, 0.17699537, 0.10492407, 0.08 ]) >>> np.blackman(10) #Blackman窗口 array([-1.38777878e-17, 5.08696327e-02, 2.58000502e-01, 6.30000000e-01, 9.51129866e-01, 9.51129866e-01, 6.30000000e-01, 2.58000502e-01, 5.08696327e-02, -1.38777878e-17]) >>> np.kaiser(12,5) #Kaiser窗口 array([0.03671089, 0.16199525, 0.36683806, 0.61609304, 0.84458838, 0.98167828, 0.98167828, 0.84458838, 0.61609304, 0.36683806, 0.16199525, 0.03671089]) >>> np.random.randint(0,50,5) #随机数组,5个0到50之间的整数 array([ 9, 36, 27, 32, 45]) >>> np.random.randint(0,50,(3,5)) #3行5列,15个介于0和50之间的整数 array([[24, 37, 39, 0, 2], [35, 30, 3, 44, 42], [16, 18, 10, 28, 32]]) >>> np.random.rand(10) #10个随机小数 array([0.52956314, 0.14924464, 0.17543161, 0.88862925, 0.81509813, 0.92279044, 0.05609938, 0.39233243, 0.11169266, 0.92278519])
numpy的简单使用
测试两个数组是否足够接近
>>> x = np.array([1, 2, 3, 4.001, 5]) >>> y = np.array([1, 1.999, 3, 4.01, 5.1]) >>> np.allclose(x, y) False >>> np.allclose(x, y, rtol=0.2) # 设置相对误差参数 True >>> np.allclose(x, y, atol=0.2) # 设置绝对误差参数 True
改变数组元素值
>>> x = np.arange(8) >>> x array([0, 1, 2, 3, 4, 5, 6, 7]) >>> np.append(x, 8) # 返回新数组,增加元素 array([0, 1, 2, 3, 4, 5, 6, 7, 8]) >>> np.append(x, [9,10]) array([0, 1, 2, 3, 4, 5, 6, 7, 9, 10]) >>> x # 不影响原来的数组 array([0, 1, 2, 3, 4, 5, 6, 7]) >>> x[3] = 8 # 原地修改元素值 >>> x array([0, 1, 2, 8, 4, 5, 6, 7]) >>> np.insert(x, 1, 8) # 返回新数组,插入元素 >>> x.repeat(3) # 元素重复,返回新数组 array([0, 0, 0, 1, 1, 1, 2, 2, 2, 8, 8, 8, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7]) >>> x.put(0, 9) # 修改指定位置上的元素值 >>> x array([9, 1, 2, 8, 4, 5, 6, 7]) >>> x = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> x[0, 2] = 4 # 修改第0行第2列的元素值 >>> x array([[1, 2, 4], [4, 5, 6], [7, 8, 9]])
numpy运算
数组与数值的运算
>>> x = np.array((1, 2, 3, 4, 5)) # 创建数组对象 >>> x array([1, 2, 3, 4, 5]) >>> x * 2 # 数组与数值相乘,返回新数组 array([ 2, 4, 6, 8, 10]) >>> x / 2 # 数组与数值相除 array([ 0.5, 1. , 1.5, 2. , 2.5]) >>> x // 2 # 数组与数值整除 array([0, 1, 1, 2, 2], dtype=int32) >>> x ** 3 # 幂运算 array([1, 8, 27, 64, 125], dtype=int32) >>> x + 2 # 数组与数值相加 array([3, 4, 5, 6, 7]) >>> x % 3 # 余数 array([1, 2, 0, 1, 2], dtype=int32)
数组与数组的运算
>>> a = np.array((1, 2, 3)) >>> b = np.array(([1, 2, 3], [4, 5, 6], [7, 8, 9])) >>> c = a * b # 数组与数组相乘 >>> c # a中的每个元素乘以b中的对应列元素 array([[ 1, 4, 9], [ 4, 10, 18], [ 7, 16, 27]]) >>> c / b # 数组之间的除法运算 array([[ 1., 2., 3.], [ 1., 2., 3.], [ 1., 2., 3.]]) >>> c / a array([[ 1., 2., 3.], [ 4., 5., 6.], [ 7., 8., 9.]])
返回 Python程序设计艺术