本文共 1456 字,大约阅读时间需要 4 分钟。
本文实例总结了Python中numpy模块常见用法。分享给大家供大家参考,具体如下:
'''想要学习Python?Python学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载!'''import numpy as nparr = np.array([[1,2,3], [2,3,4]])print(arr)print(type(arr))print('number of dim:', arr.ndim)print('shape:', arr.shape)print('size:', arr.size)
[[1 2 3]
[2 3 4]] number of dim: 2 shape: (2, 3) size: 6
a32 = np.array([1,23,456], dtype=np.int)print(a32.dtype)a64 = np.array([1,23,456], dtype=np.int64)print(a64.dtype)f64 = np.array([1,23,456], dtype=np.float)print(f64.dtype)
int32
int64 float64
z = np.zeros((3, 4))print(z)print(z.dtype)print()one = np.ones((3, 4), dtype=int)print(one)print(one.dtype)print()emt = np.empty((3, 4), dtype=int)print(emt)print(emt.dtype)print()ran = np.arange(12).reshape((3,4))print(ran)print(ran.dtype)print()li = np.linspace(1, 10, 6).reshape(2, 3)print(li)print(li.dtype)
[[0. 0. 0. 0.]
[0. 0. 0. 0.] [0. 0. 0. 0.]] float64 [[1 1 1 1] [1 1 1 1] [1 1 1 1]] int32 [[ 0 1072693248 1717986918 1074161254] [ 1717986918 1074947686 -1717986918 1075419545] [ 1717986918 1075865190 0 1076101120]] int32 [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] int32 [[ 1. 2.8 4.6] [ 6.4 8.2 10. ]] float64
a = np.array([10,20,30,40])b = np.arange(4)print(a)print(b)print()print(a+b)print(a-b)print(a*b)print()print(a**b)print()print(10*np.sin(a))print()print(b<3)print()
[10 20 30 40]
[0 1 2 3] [10 21 32 43] [10 19 28 37] [ 0 20 60 120] [ 1 20 900 64000] [-5.44021111 9.12945251 -9.88031624 7.4511316 ] [ True True True False]
转载地址:http://usnmf.baihongyu.com/