numpy还是蛮好用的,一直没记录,赶紧记录一下。
取出b的在a中大于4的索引的元素
a = np.array([1,2,3,44,4])
b = np.array([1,1,0,100,6])
print(a > 4)
print(b[a > 4])
import numpy as np
a = np.array([[1,2,3]])
b = np.array([[1,2,3]])
# 纵向拼接
c = np.r_[a, b]
# 横向拼接
d = np.c_[a, b]
print(c)
print(d)
import numpy as np
a = np.array([[1,2,3]])
b = np.array([[1,2,3]])
# 纵向拼接
c = np.vstack((a, b))
# 横向拼接
d = np.hstack((a, b))
print(c)
print(d)
矩阵乘法
import numpy as np
a = np.array([[1, 2],
[2, 4]])
b = np.array([[1, 1],
[2, 2]])
c = np.matmul(a, b)
print(c)
求对应元素点积
import numpy as np
a = np.array([[1, 2],
[2, 4]])
b = np.array([[1, 1],
[2, 2]])
c = np.multiply(a, b)
print(c)
当矩阵与一个常数进行运算时,会进行广播。
当维度相同时,则对应元素进行计算。
import numpy as np
a = np.array([[1, 2],
[2, 4]])
b = np.array([[1, 1],
[2, 2]])
print(a - 1)
print(a - b)
使用numpy中的运算方法,当传入数组时,会自动进行对应元素进行计算
import numpy as np
a = np.array([[1, 2],
[2, 4]])
b = np.power(a, 2)
print(b)