TensorFlow实现线性回归
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
# 线性回归
print(tf.__version__)
# 数据集 - 受教育年限与收入的关系
data = pd.read_csv('Income1.csv')
x = data.Education
y = data.Income
print(data)
# 数据可视化
plt.scatter(x, y)
# plt.show()
# 构建模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1, input_shape=(1,)))
print(model.summary())
# 梯度下降算法 - adam和损失函数 - 均方差mse 最小二乘法
model.compile(optimizer='adam', loss='mse')
# 训练
history = model.fit(x, y, epochs=5000)
# 预测
pre = model.predict(x)
print(pre)
pre_20 = model.predict(pd.Series([20]))
print(pre_20)