深度学习中常用numpy操作(3.计算)

# -*- coding:utf-8 -*-

import numpy as np

# math
x = np.arange(1, 5).reshape(2, 2)
y = np.arange(5, 9).reshape(2, 2)
add = np.add(x, y)  # x+y
subtract = np.subtract(x, y)  # x-y
multiply = np.multiply(x, y)  # x*y
divide = np.divide(x, y)  # x/y
sqrt = np.sqrt(x)  # 开平方根
print("add:\n", add, '\n')
print("subtract:\n", subtract, '\n')
print("multiply:\n", multiply, '\n')
print("divide:\n", divide, '\n')
print("sqrt:\n", sqrt, '\n')
# 均表示在各个相同的位置做运算,不是矩阵运算

# 矩阵运算
v = np.array([9, 10])
w = np.array([11, 12])
print("np.dot(v, w):\n", np.dot(v, w), '\n')  # 等价于v.dot(w)
print("np.dot(x, v):\n", np.dot(x, v), '\n')  # 等价于x.dot(v)
print("np.dot(x, y):\n", np.dot(x, y), '\n')  # 等价于x.dot(y)

z = np.array([[1, 2], [3, 4]])
print("np.sum(z):\n", np.sum(z), '\n')  # Compute sum of all elements; prints "10"
print("np.sum(z, axis=0):\n", np.sum(z, axis=0), '\n')  # Compute sum of each column; prints "[4 6]"
print("np.sum(z, axis=1):\n", np.sum(z, axis=1), '\n')  # Compute sum of each row; prints "[3 7]"
print("z.T:\n", z.T, "\n")  # 转置
print("v.T:\n", v.T, '\n')  # 不变

输出:

add:
 [[ 6  8]
 [10 12]] 

subtract:
 [[-4 -4]
 [-4 -4]] 

multiply:
 [[ 5 12]
 [21 32]] 

divide:
 [[ 0.2         0.33333333]
 [ 0.42857143  0.5       ]] 

sqrt:
 [[ 1.          1.41421356]
 [ 1.73205081  2.        ]] 

np.dot(v, w):
 219 

np.dot(x, v):
 [29 67] 

np.dot(x, y):
 [[19 22]
 [43 50]] 

np.sum(z):
 10 

np.sum(z, axis=0):
 [4 6] 

np.sum(z, axis=1):
 [3 7] 

z.T:
 [[1 3]
 [2 4]] 

v.T:
 [ 9 10] 

more:
https://docs.scipy.org/doc/numpy/reference/routines.math.html

https://docs.scipy.org/doc/numpy/reference/routines.array-manipulation.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值