import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
if __name__ == '__main__':
data = pd.read_csv('dataset/Advertising.csv')
print(data.head())#显示前五行数据
"""
TV radio newspaper sales
TV,radio,newspaper预测sales
"""
x = data.iloc[:,1:-1]#取所有行,第一列到倒数第二列的数据
y = data.iloc[:,-1]#取所有行,最后一列的数据
modle = tf.keras.Sequential(
[tf.keras.layers.Dense(10,input_shape=(3,),activation='relu'),#第一层,10个神经元,输入数据3维
tf.keras.layers.Dense(1)]#输出层
)
print(modle.summary())
modle.compile(
optimizer='adam',
loss='mse'
)
modle.fit(x,y,epochs=1000)#训练模型
test = data.iloc[:10,1:-1]
print(modle.predict(test))#预测结果
数据集
0 TV radio newspaper sales
0 1 230.1 37.8 69.2 22.1
1 2 44.5 39.3 45.1 10.4
2 3 17.2 45.9 69.3 9.3
3 4 151.5 41.3 58.5 18.5
4 5 180.8 10.8 58.4 12.9
modle.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 10) 40
_________________________________________________________________
dense_1 (Dense) (None, 1) 11
=================================================================
Total params: 51
Trainable params: 51
Non-trainable params: 0
_________________________________________________________________
40 = 310+b10
b为每一层的偏置
数据集链接
链接:https://pan.baidu.com/s/1uO2OM8qio4tiYUR8c7TDeg
提取码:jabx