深度学习笔记-----基于TensorFlow2.2.0代码练习(第二课)

这篇博文是基于TensorFlow2.2.0的深度学习代码练习,按照KGP Talkie的教程进行,涵盖了从数据处理到构建ANN的全过程,包括特征标准化、输入层、隐藏层的建立,以及模型训练和评估。教程链接和数据提供,便于读者跟随操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在正文之前:
这篇紧接着上一篇的博文
深度学习笔记-----基于TensorFlow2.2.0代码练习(第一课)
主要写的是TensorFlow2.0的代码练习,跟随着KGP Talkie的【TensorFlow 2.0】实战进阶教程进行学习,并将其中一些不适用的代码错误进行修改。
本文跟随视频油管非常火的【TensorFlow 2.0】实战进阶教程(中英字幕+代码实战)第二课

课程所需要的数据链接:https://pan.baidu.com/s/1Lpo3l3UaPANOGE_HGJf2TQ
提取码:dqo4
注意:需要把数据放到jupyter目录下

如何建立第一个ANN

1 数据处理
2 建立输入层
3 初始随机化输入权重W
4 建立隐藏层
5 选择优化,损失和精确性指标
6 编译模型
7 使用model.fit 训练模型
8 评估模型
9 如果有需要的话调整模型

#导入库
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Flatten,Dense
#导入包
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split#这是为了把数据分割成训练集和测试集
dataset = pd.read_csv('customer_Churn_Modelling.csv')#读取数据,需要把数据放到和此文件的同一目录
dataset.head()#查看数据
RowNumberCustomerIdSurnameCreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalaryExited
0115634602Hargrave619FranceFemale4220.00111101348.881
1215647311Hill608SpainFemale41183807.86101112542.580
2315619304Onio502FranceFemale428159660.80310113931.571
3415701354Boni699FranceFemale3910.0020093826.630
4515737888Mitchell850SpainFemale432125510.8211179084.100
X = dataset.drop(labels=['CustomerId','Surname','RowNumber','Exited'],axis =1)#删除数据中的一些然后存入X中
y = dataset['Exited']#y的数据
X.head()
CreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalary
0619FranceFemale4220.00111101348.88
1608SpainFemale41183807.86101112542.58
2502FranceFemale428159660.80310113931.57
3699FranceFemale3910.0020093826.63
4850SpainFemale432125510.8211179084.10
y.head()
0    1
1    0
2    1
3    0
4    0
Name: Exited, dtype: int64
#处理标签
#将国家Geography和性别gender中的字符转换为数字
from sklearn.preprocessing import LabelEncoder
label1 = LabelEncoder()
X['Geography'] = label1.fit_transform(X['Geography'])#将国家通过LabelEncoder转换为数值
X.head()
CreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalary
06190Female4220.00111101348.88
16082Female41183807.86101112542.58
25020Female428159660.80310113931.57
36990Female3910.0020093826.63
48502Female432125510.8211179084.10
label2 = LabelEncoder()
X['Gender'] = label1.fit_transform(X['Gender'])#将国家通过LabelEncoder转换为数值
X.head()
CreditScoreGeographyGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalary
0619004220.00111101348.88
16082041183807.86101112542.58
250200428159660.80310113931.57
3699003910.0020093826.63
485020432125510.8211179084.10

CreditScoreGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalaryGeography_1Geography_2
061904220.00111101348.8800
1608041183807.86101112542.5801
25020428159660.80310113931.5700
369903910.0020093826.6300
48500432125510.8211179084.1001
56451448113755.78210149756.7101
682215070.0021110062.8000
73760294115046.74410119346.8810
85011444142051.0720174940.5000
96841272134603.8811171725.7300
#把国家信息转换为0到1 的二进制数字,即为某个国家就显示1否则为0
X = pd.get_dummies(X, drop_first=True, columns=['Geography'])
X.head(30)
CreditScoreGenderAgeTenureBalanceNumOfProductsHasCrCardIsActiveMemberEstimatedSalaryGeography_1Geography_2
061904220.00111101348.8800
1608041183807.86101112542.5801
25020428159660.80310113931.5700
369903910.0020093826.6300
48500432125510.8211179084.1001
56451448113755.78210149756.7101
682215070.0021110062.8000
73760294115046.74410119346.8810
85011444142051.0720174940.5000
96841272134603.8811171725.7300
105281316102016.7220080181.1200
1149712430.0021076390.0101
12476034100.0021026260.9800
1354902550.00200190857.7900
1463503570.0021165951.6501
156161453143129.4120164327.2610
166531581132602.881105097.6710
1754902490.0021114406.4101
1858714560.00100158684.8101
1972602460.0021154724.0300
2073214180.00211170886.1700
2163603280.00210138555.4601
2251003840.00110118913.5301
2366914630.002018487.7500
2484603850.00111187616.1600
2557712530.00201124508.2900
267561362136815.64111170041.9510
2757114490.0020038433.3500
285740433141349.43111100187.4310
29411129059697.1721153483.2100

特征标准化

#用自带的预处理包进行
from sklearn.preprocessing import StandardScaler
X_train, X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2, random_state = 0, stratify = y)#分测试训练比例为20%。随机关闭,并且按y中类的比例进行分配,避免出现类分布不均衡
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)##标准化测试和训练
y_test
1344    1
8167    0
4747    0
5004    1
3124    1
       ..
9107    0
8249    0
8337    0
6279    1
412     0
Name: Exited, Length: 2000, dtype: int64

构建ANN

model = Sequential()#序列模型
model.add(Dense(X.shape[1],activation='relu',input_dim = X.shape[1]))#输入层的建立X_shape是提取其所有特征数量
model.add(Dense(128,activation = 'relu'))#隐藏层建立
model.add(Dense(1,activation = 'sigmoid'))#输出层建立
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
model.compile(optimizer = 'adam',loss ='binary_crossentropy',metrics=['accuracy'])#采用随机梯度优化,
model.fit(X_train,y_train.to_numpy(),batch_size=10,epochs=10,verbose=1)
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/10
8000/8000 [==============================] - 1s 94us/sample - loss: 0.4515 - acc: 0.8049
Epoch 2/10
8000/8000 [==============================] - 1s 80us/sample - loss: 0.4185 - acc: 0.8202
Epoch 3/10
8000/8000 [==============================] - 1s 80us/sample - loss: 0.4057 - acc: 0.8324
Epoch 4/10
8000/8000 [==============================] - 1s 77us/sample - loss: 0.3752 - acc: 0.8431
Epoch 5/10
8000/8000 [==============================] - 1s 79us/sample - loss: 0.3507 - acc: 0.8571
Epoch 6/10
8000/8000 [==============================] - 1s 78us/sample - loss: 0.3415 - acc: 0.8591
Epoch 7/10
8000/8000 [==============================] - 1s 79us/sample - loss: 0.3363 - acc: 0.8620
Epoch 8/10
8000/8000 [==============================] - 1s 84us/sample - loss: 0.3345 - acc: 0.8619
Epoch 9/10
8000/8000 [==============================] - 1s 74us/sample - loss: 0.3328 - acc: 0.8602
Epoch 10/10
8000/8000 [==============================] - 1s 74us/sample - loss: 0.3302 - acc: 0.8626





<tensorflow.python.keras.callbacks.History at 0x1d77c75d248>
y_pred = model.predict_classes(X_test)
y_pred
array([[0],
       [0],
       [0],
       ...,
       [0],
       [1],
       [0]])
y_test
1344    1
8167    0
4747    0
5004    1
3124    1
       ..
9107    0
8249    0
8337    0
6279    1
412     0
Name: Exited, Length: 2000, dtype: int64
model.evaluate(X_test, y_test.to_numpy())#利用测试集测试训练下的模型的准确度
2000/2000 [==============================] - 0s 34us/sample - loss: 0.3583 - acc: 0.8535





[0.3583366745710373, 0.8535]
#另一种计算精度的方法
from sklearn.metrics import confusion_matrix, accuracy_score
confusion_matrix(y_test,y_pred)
array([[1525,   68],
       [ 225,  182]], dtype=int64)
accuracy_score(y_test,y_pred)
0.8535

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zdswyh123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值