[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]
~~~