Use of tensorflow numpy

matrix

import numpy as np
data1 = np.array([1,2,3,4,5])
print(data1)

Output [1 2 3 4 5]

data2 = np.array([[1,2],
                  [3,4]])
print(data2)

Output

[[1 2]
 [3 4]]

Matrix dimension

data2 = np.array([[1,2],
                  [3,4]])
# 输出举证的维度
print("输出矩阵的维度:", data2.shape)

The dimensions of the output matrix: (2, 2)

All 0 or all 1 matrix

print(np.zeros([2,2]))
print(np.ones([2,3]))

Output

[[0. 0.]
 [0. 0.]]
[[1. 1. 1.]
 [1. 1. 1.]]

Modify and get the value of a single element

data2 = np.array([[1,2],
                  [3,4]])
# 修改矩阵某个元素的值
data2[0,0] = 8
print(data2[0,0])

# 获取矩阵某个元素的值
print(data2[0,1])

Output

8
2

Addition, subtraction, multiplication and division of a matrix and a certain value

data3 = np.ones([2,3])
print(data3+2)
print(data3-1)
print(data3*2)
print(data3/2)

Output

[[3. 3. 3.]
 [3. 3. 3.]]
[[0. 0. 0.]
 [0. 0. 0.]]
[[2. 2. 2.]
 [2. 2. 2.]]
[[0.5 0.5 0.5]

Matrix and matrix addition, subtraction, multiplication and division

data3 = np.ones([2,3])
data4 = np.array([[1,2,3],[4,5,6]])
print(data3+data4)
print(data3*data4)

[[2. 3. 4.]
 [5. 6. 7.]]
[[1. 2. 3.]
 [4. 5. 6.]]

Published 92 original articles · Likes5 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/xfb1989/article/details/105439159