线性回归实现
转自:https://www.cnblogs.com/miraclepbc/p/14287756.html
相关库引用
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
加载数据
data = pd.read_csv("E:/datasets/dataset/Income1.csv") # 获取数据
x = data.Education
y = data.Income
data
输出散点图,看看数据的分布情况
plt.scatter(data.Education, data.Income) # 打印散点图
定义模型
model = tf.keras.Sequential() # 建立一个层叠模型
model.add(tf.keras.layers.Dense(1, input_shape = (1, ))) # 添加一个层,1个单元,输入大小用元组
model.summary() # 查看模型的参数信息
这里总共有2个参数,即 <span class="MathJax_Preview"><span id="MathJax-Element-1-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>y</mi><mo>=</mo><mi>a</mi><mi>x</mi><mo>+</mo><mi>b</mi></math>"><span id="MJXc-Node-1" class="mjx-math"><span id="MJXc-Node-2" class="mjx-mrow"><span id="MJXc-Node-3" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">y<span id="MJXc-Node-4" class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-5" class="mjx-mi MJXc-space3"><span class="mjx-char MJXc-TeX-math-I">a<span id="MJXc-Node-6" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">x<span id="MJXc-Node-7" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">+<span id="MJXc-Node-8" class="mjx-mi MJXc-space2"><span class="mjx-char MJXc-TeX-math-I">b<span class="MJX_Assistive_MathML">