5
16
0
1. 云栖社区>
2. 博客>
3. 正文

深度学习中的基础线性代数-初学者指南

【方向】 2017-03-29 21:05:51 浏览5613

``````# Multiply two arrays
x = [1,2,3]
y = [2,3,4]
product = []
for i in range(len(x)):
product.append(x[i]*y[i])
# Linear algebra version
x = numpy.array([1,2,3])
y = numpy.array([2,3,4])
x * y``````

``````y = np.array([1,2,3])
x = np.array([2,3,4])
y + x = [3, 5, 7]
y - x = [-1, -1, -1]
y / x = [.5, .67, .75]``````

``````y = np.array([1,2,3])
x = np.array([2,3,4])
np.dot(y,x) = 20``````

``````y = np.array([1,2,3])
x = np.array([2,3,4])
y * x = [2, 6, 12]``````

``````a = np.array([
[1,2,3],
[4,5,6]
])
a.shape == (2,3)
b = np.array([
[1,2,3]
])
b.shape == (1,3)``````

``````a = np.array([
[1,2],
[3,4]
])
b = np.array([
[1,2],
[3,4]
])
a + b
[[2, 4],
[6, 8]]
a — b
[[0, 0],
[0, 0]]``````

1.Â Â Â Â  两个矩阵维度相等，或

2.Â Â  一个矩阵的维度为1

``````a = np.array([
[1],
[2]
])
b = np.array([
[3,4],
[5,6]
])
c = np.array([
[1,2]
])
# Same no. of rows
# Different no. of columns
# but a has one column so this works
a * b
[[ 3, 4],
[10, 12]]
# Same no. of columns
# Different no. of rows
# but c has one row so this works
b * c
[[ 3, 8],
[5, 12]]
# Different no. of columns
# Different no. of rows
# but both a and c meet the
# size 1 requirement rule
a + c
[[2, 3],
[3, 4]]``````

``````a = np.array(
[[2,3],
[2,3]])
b = np.array(
[[3,4],
[5,6]])
# Uses python's multiply operator
a * b
[[ 6, 12],
[10, 18]]``````

1. 矩阵旋转90°

2.反转每行元素的顺序（例如[a b c]变为[c b a]

``````a = np.array([
[1, 2],
[3, 4]])
a.T
[[1, 3],
[2, 4]]``````

1.Â Â Â Â  第一矩阵的列数必须等于第二个矩阵的行数

2.Â Â  M×N矩阵和N×K矩阵的乘积是M×K矩阵。 新矩阵取第一个矩阵的行和第二个矩阵的列。

Numpy使用函数np.dotAB）进行向量和矩阵乘法运算。 它有一些其他有趣的功能和问题，所以我希望大家能在使用前阅读一下相关文档。

【方向】
+ 关注