二. 多层感知器

该博客展示了如何利用Python的TensorFlow库建立一个简单的神经网络模型,对'Advertising.csv'数据集中的销售数据进行预测。数据集包含TV、radio和newspaper三个特征,用于预测sales。模型包括一个隐藏层(10个神经元,ReLU激活)和一个输出层。经过1000次迭代训练后,模型能够对测试数据进行预测。
摘要由CSDN通过智能技术生成
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值