[TOC]
# 乘法
要注意维度要一样,也就是shape值要一样,不然会报错
~~~
import numpy as np
x = np.array([5, 5])
y = np.array([2, 2])
# 对应位置相乘
print(np.multiply(x, y))
# 乘积后求和5*2 + 5*2
print(np.dot(x, y))
~~~
输出
~~~
[10 10]
20
~~~
`*`号乘法
~~~
import numpy as np
x = np.array([1, 1, 1])
y = np.array([[1, 2, 3], [4, 5, 6]])
print(x * y)
~~~
输出
~~~
[[1 2 3]
[4 5 6]]
~~~
发现维度不一样还是能乘的,因为numpy自动做了扩充,一般情况下还是不要这样
# 比较
~~~
import numpy as np
x = np.array([1, 1, 1])
y = np.array([1, 2, 1])
print(x == y)
~~~
输出
~~~
[ True False True]
~~~
注意维度不一样会报错
# 与,或,非
~~~
import numpy as np
x = np.array([1, 1, 1])
y = np.array([1, 2, 1])
# 对每个值进行与操作
logical_and = np.logical_and(x, y)
print(logical_and)
# 或操作
logical_or = np.logical_or(x, y)
print(logical_or)
logical_not = np.logical_not(x, y)
print(logical_not)
~~~
输出
~~~
[ True True True]
[ True True True]
[0 0 0]
~~~