波士顿房价

多变量 属于多变量线性回归

数据读取

数据保存在csv文件中
2019-05-14-01-08-36----

2019-05-14-01-09-11----

import tensorflow as tf
# 载入plt
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv("data/boston.csv", header=0)
print(df)

建模

2019-05-14-01-52-01----

2019-05-14-01-52-46----

线性代数

标量

单一的数字

向量

有序的数字数组在一行,一列

矩阵

二维数组

矩阵标量运算

2019-05-14-01-57-40----

import tensorflow as tf
# 载入plt
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv("data/boston.csv", header=0)

# 标量
scalar_value = 18
scalar_np = np.array(scalar_value) # 转换为数组
print(scalar_np.shape)  # shape 为矩阵

# 向量  有序数字数组
vector_value = [1,2,3]
vector_np = np.array(vector_value)
print(vector_np.shape) # shape为一维数组

# 矩阵为二维数组
matrix_list = [[1,2,3], [4,5,6]]
matrix_np = np.array(matrix_list)
print(matrix_np.shape)  # 2 * 3 矩阵

# 行向量
vector_row = np.array([[1,2,3]])
print(vector_row.shape)

# 列向量
vector_column = np.array([[4], [5], [6]])
print(vector_column.shape)

# 矩阵标量运算
matrix_a = np.array([[1,2,3], [4,5,6]])
print(matrix_a.shape)

# 矩阵 × 标量
matrix_b = matrix_a * 2
print(matrix_b.shape)

# 矩阵 + 标量
matrix_c = matrix_a + 2
print(matrix_c.shape)

代数运算规则

2019-05-14-02-11-31----

# 矩阵 + 矩阵
matrix_a = np.array([
    [1,2,3],
    [4,5,6]
])
matrix_b = np.array(
    [
        [1,2,-3],
        [3,5,-6]
    ]
)
print(matrix_b + matrix_a)

2019-05-14-02-13-52----

2019-05-14-02-14-14----

# 点积
matrix_a = np.array([
    [1,2,3],
    [4,5,6]
])
matrix_b = np.array([
    [-2,-3,-5],
    [-4,-5,-6]
])
print(matrix_a * matrix_b)

# 叉乘
matrix_a = np.array([
    [1,2,3],
    [4,5,6]
])
matrix_b = np.array([
    [-2,-3,-5],
    [-4,-5,-6],
    [23,45,2]
])
print(np.matmul(matrix_a, matrix_b))

2019-05-14-02-20-39----

2019-05-14-02-21-04----

2019-05-14-02-21-25----

2019-05-14-02-22-14----

# 矩阵向量
matrix_a = np.array([
    [1,2,3],
    [4,5,6]
])
matrix_b = ([
    [1],
    [2],
    [3]
])
np.matmul(matrix_a, matrix_b)

# 矩阵转置
matrix_a = np.array([
    [1,2,3],
    [4,5,6]
])
print(matrix_a.shape, matrix_a.T)

# 行列转职
vector_row = np.array([[1,2,3]])
print(vector_row.T)