一 补充python库
- pip install numpy pandas matplotlib sklearn -i https://pypi.douban.com/simple
二 keras
pandas库,用来读取数据
matplotlib库,用来画图标使用
传统的算法是不断的尝试来寻找最优的a和b,效率很低
顺序模型:一个输入、一个输出,使用tf.keras.Sequential模式
对应代码理解:
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
print(tf.__version__)
data = pd.read_csv('./income.csv')
plt.scatter(data.Education,data.Income)
x=data.Education
y=data.Income
#初始化keras模型库,模型库使用现行回归模型
model=tf.keras.Sequential()
#模型库增加网络层,dense第一个参数是输出维度
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
#ax+b
model.summary()
#使用梯度优化,均方差的优化方法
model.compile(optimizer='adam',
loss='mse')
#训练,配置训练次数
history = model.fit(x,y,epochs=1000)
#使用训练的模型优化结果
model.predict(pd.Series([20]))