“Python numpy的简单应用”的版本间的差异
来自CloudWiki
(创建页面,内容为“导入模块 >>> import numpy as np 生成数组 <nowiki>>>> np.array([1, 2, 3, 4, 5]) # 把列表转换为数组 array([1, 2, 3, 4, 5]) >>> np.array((1,…”) |
|||
第1行: | 第1行: | ||
+ | ==numpy的安装== | ||
+ | pip3 install numpy | ||
+ | |||
+ | ==numpy的简单使用== | ||
+ | |||
导入模块 | 导入模块 | ||
>>> import numpy as np | >>> import numpy as np | ||
第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.]) | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> np.logspace(0,100,10) | ||
+ | array([1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469e+033, | ||
+ | 2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250e+077, | ||
+ | 7.74263683e+088, 1.00000000e+100]) | ||
+ | >>> np.logspace(1,6,5,base=2) | ||
+ | array([ 2. , 4.75682846, 11.3137085 , 26.90868529, 64. ]) | ||
+ | >>> 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> |
2018年5月27日 (日) 12:28的版本
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.]) >>> >>> >>> np.logspace(0,100,10) array([1.00000000e+000, 1.29154967e+011, 1.66810054e+022, 2.15443469e+033, 2.78255940e+044, 3.59381366e+055, 4.64158883e+066, 5.99484250e+077, 7.74263683e+088, 1.00000000e+100]) >>> np.logspace(1,6,5,base=2) array([ 2. , 4.75682846, 11.3137085 , 26.90868529, 64. ]) >>> 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.]])