# 教程 | 基础入门：深度学习矩阵运算的概念和代码实现

```# 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])
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]])
a + 1
[[2,3],
[4,5]]```

```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
```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 = np.array([
[1, 2],
[3, 4]])```
```a.T
[[1, 3],
[2, 4]]```

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

2. m×n 阶矩阵左乘 n×k 阶矩阵的结果是 m×k 阶矩阵。新得出来矩阵就等于第一个矩阵的行数×第二矩阵的列数。

A 矩阵行向量 a1 与 B 矩阵列向量 b1 的点积，即下图所示：

```a = np.array([
[1, 2]
])
a.shape == (1,2)```
```b = np.array([
[3, 4],
[5, 6]
])
b.shape == (2,2)```
```# Multiply
mm = np.dot(a,b)
mm == [13, 16]
mm.shape == (1,2)```

0 条评论

## 相关文章

17220

38180

38270

### R开发：常用R语言包介绍

r与python差异比较大的一个地方就是，python的机器学习算法集中程度比较高，比如sklearn，就集成了很多的算法，而R语言更多时候需要一个包一个包去了...

12150

### 深入理解感知机

1.模型 感知机的模型如下图所示： ? linear_classifier_structure.png 公式表示如下所示： \$\$ f(x) = sign(...

380100

19220

26440

21850

12100