温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中numpy多维数组的使用案例

发布时间:2020-11-04 09:33:37 来源:亿速云 阅读:149 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Python中numpy多维数组的使用案例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

加法和减法操作要求操作双方的维数信息一致,均为M*N为数组方可正确执行操作。

a = np.arange(4) 输出: array([0, 1, 2, 3]) b = a**2 输出: array([0, 1, 4, 9]) c = 10*np.sin(a) 输出: array([ 0.  , 8.41470985, 9.09297427, 1.41120008]) n < 35 输出: array([ True, True, True, True], dtype=bool) A = np.array([[1,1],[0,1]]) B = np.array([[2,0],[3,4]]) C = A * B # 元素点乘 输出: array([[2, 0],  [0, 4]]) D = A.dot(B) # 矩阵乘法 输出: array([[5, 4],  [3, 4]]) E = np.dot(A,B) # 矩阵乘法 输出: array([[5, 4],  [3, 4]])

多维数组操作过程中的类型转换

When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting)

即操作不同类型的多维数组时,结果自动转换为精度更高类型的数组,即upcasting

a = np.ones((2,3),dtype=int)  # int32 b = np.random.random((2,3))  # float64 b += a # 正确 a += b # 错误

a = np.ones(3,dtype=np.int32) b = np.linspace(0,pi,3) c = a + b d = np.exp(c*1j) 输出: array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,  -0.54030231-0.84147098j]) d.dtype.name 输出: 'complex128'

多维数组的一元操作,如求和、求最小值、最大值等

a = np.random.random((2,3)) a.sum() a.min() a.max() b = np.arange(12).reshape(3,4) 输出: array([[ 0, 1, 2, 3],  [ 4, 5, 6, 7],  [ 8, 9, 10, 11]]) b.sum(axis=0) # 按列求和 输出: array([12, 15, 18, 21]) b.sum(axis=1) # 按行求和 输出: array([ 6, 22, 38]) b.cumsum(axis=0) # 按列进行元素累加 输出: array([[ 0, 1, 2, 3],  [ 4, 6, 8, 10],  [12, 15, 18, 21]]) b.cumsum(axis=1) # 按行进行元素累加 输出: array([[ 0, 1, 3, 6],  [ 4, 9, 15, 22],  [ 8, 17, 27, 38]]) universal functions B = np.arange(3) np.exp(B) np.sqrt(B) C = np.array([2.,-1.,4.]) np.add(B,C)

其他的ufunc函数包括:

all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor,inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var,vdot, vectorize, where

关于Python中numpy多维数组的使用案例就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI