import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from torch import nn
data = pd.read_csv("dataset/HR.csv")
print("data.head():\t", data.head())
data.info()
print("data.info():\t", data.info())
data.part.unique()
print("data.part.unique()", data.part.unique())
data.salary.unique()
print("data.salary.unique()", data.salary.unique())
data.groupby(["salary", "part"]).size()
print("data.groupby(['salary', 'part']).size()",data.groupby(['salary', 'part']).size())
pd.get_dummies(data.salary)
print("pd.get_dummies(data.salary):\t", pd.get_dummies(data.salary))
data=data.join(pd.get_dummies(data.salary))
print("data.head()):\t", data.head())
data=data.join(pd.get_dummies(data.part))
del data["salary"]
del data["part"]
data.head()
print("data.head():\t", data.head())
data.left.value_counts()
print("data.left.value_counts():\n", data.left.value_counts())
Y_data = data.left.values.reshape(-1, 1)
print("Y_data.shape:\t", Y_data.shape)
Y =torch.from_numpy(Y_data).type(torch.float32)
print("Y.shape", Y.shape)
"""
M = [c for c in data.columns if c!= "left"]
print("M:\t", X_data)
"""
X_data =data[[c for c in data.columns if c != 'left']].values
"""
两种方式进行数据类型转换
如果numpy上转换 则用.astype(np.float32)
如果torch上转换 则用.type(torch.float32)
"""
X = torch.from_numpy(X_data.astype(np.float32))
print("X:\t", X)
print("X.size():\t", X.shape)
"""""""""""""""""""""""""""""""""""""""""""""""
创建模型:
from torch import nn
自定义模型:
nn.Module: 继承这个类
__init__:初始化所有的层
forward: 定义模型的运算过程 (前向传播的过程)
"""""""""""""""""""""""""""""""""""""""""""""""
"""
# 自定义类 方法一
class Model(nn.Module):
def __init__(self):
super().__init__()
self.liner_1 = nn.Linear(20, 64)
self.liner_2 = nn.Linear(64, 64)
self.liner_3 = nn.Linear(64, 1)
self.relu = nn.ReLU() # 初始化relu
self.sigmoid = nn.Sigmoid() # 初始化sigmoid
def forward(self, input):
x = self.Liner_1(input)
x = self.relu(x)
x = self.Liner_2(x)
x = self.rele(x)
x = self.Liner_3(x)
x = self.sigmod(x)
return x
"""
"""""""""""""""""""""""""""""""""""
方法的改写: 方法二
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super().__init__()
self.liner_1 = nn.Linear(20, 64)
self.liner_2 = nn.Linear(64, 64)
self.liner_3 = nn.Linear(64, 1)
def forward(self, input):
x = F.relu(self.Liner_1(input))
x = F.relu(self.Liner_2(x))
x = F.sigmoid(self.Liner_3(x))
return x
"""""""""""""""""""""""""""""""""""
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super().__init__()
self.Liner_1 = nn.Linear(20, 64)
self.Liner_2 = nn.Linear(64, 64)
self.Liner_3 = nn.Linear(64, 1)
def forward(self, input):
x = F.relu(self.Liner_1(input))
x = F.relu(self.Liner_2(x))
x = F.sigmoid(self.Liner_3(x))
return x
"""
model = Model() # 模型的实例化
print("model:\t", model)
"""
lr = 0.0001
def get_model():
model = Model()
opt = torch.optim.Adam(model.parameters(), lr=lr)
return model, opt
model, optim = get_model()
"""
定义损失函数
"""
loss_fn = nn.BCELoss()
batch = 64
no_of_batch = len(data)//batch
epochs = 100
for epoch in range(epochs):
for i in range(no_of_batch):
start = i*batch
end = start+batch
x = X[start: end]
y = Y[start: end]
y_pred=model(x)
loss =loss_fn(y_pred, y)
optim.zero_grad()
loss.backward()
optim.step()
"""
因为以下步骤不需要计算损失
torch类型 查询它的值用.data item()代表将单一元素的Tensor类型转换成数值型的标量
numpy和pandas类型的数据查询它的值用.values
"""
with torch.no_grad():
print("epoch:", epoch, "loss:", loss_fn(model(X), Y).data.item())
loss_fn(model(X), Y)
print("loss_fn(model(X), Y):\t", loss_fn(model(X), Y))
data.head(): satisfaction_level last_evaluation ... part salary
0 0.38 0.53 ... sales low
1 0.80 0.86 ... sales medium
2 0.11 0.88 ... sales medium
3 0.72 0.87 ... sales low
4 0.37 0.52 ... sales low
[5 rows x 10 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14999 entries, 0 to 14998
Data columns (total 10 columns):
--- ------ -------------- -----
0 satisfaction_level 14999 non-null float64
1 last_evaluation 14999 non-null float64
2 number_project 14999 non-null int64
3 average_montly_hours 14999 non-null int64
4 time_spend_company 14999 non-null int64
5 Work_accident 14999 non-null int64
6 left 14999 non-null int64
7 promotion_last_5years 14999 non-null int64
8 part 14999 non-null object
9 salary 14999 non-null object
dtypes: float64(2), int64(6), object(2)
memory usage: 1.1+ MB
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14999 entries, 0 to 14998
Data columns (total 10 columns):
--- ------ -------------- -----
0 satisfaction_level 14999 non-null float64
1 last_evaluation 14999 non-null float64
2 number_project 14999 non-null int64
3 average_montly_hours 14999 non-null int64
4 time_spend_company 14999 non-null int64
5 Work_accident 14999 non-null int64
6 left 14999 non-null int64
7 promotion_last_5years 14999 non-null int64
8 part 14999 non-null object
9 salary 14999 non-null object
dtypes: float64(2), int64(6), object(2)
memory usage: 1.1+ MB
data.info(): None
data.part.unique() ['sales' 'accounting' 'hr' 'technical' 'support' 'management' 'IT'
'product_mng' 'marketing' 'RandD']
data.salary.unique() ['low' 'medium' 'high']
data.groupby(['salary', 'part']).size() salary part
high IT 83
RandD 51
accounting 74
hr 45
management 225
marketing 80
product_mng 68
sales 269
support 141
technical 201
low IT 609
RandD 364
accounting 358
hr 335
management 180
marketing 402
product_mng 451
sales 2099
support 1146
technical 1372
medium IT 535
RandD 372
accounting 335
hr 359
management 225
marketing 376
product_mng 383
sales 1772
support 942
technical 1147
dtype: int64
pd.get_dummies(data.salary): high low medium
0 0 1 0
1 0 0 1
2 0 0 1
3 0 1 0
4 0 1 0
... ... ... ...
14994 0 1 0
14995 0 1 0
14996 0 1 0
14997 0 1 0
14998 0 1 0
[14999 rows x 3 columns]
data.head()): satisfaction_level last_evaluation number_project ... high low medium
0 0.38 0.53 2 ... 0 1 0
1 0.80 0.86 5 ... 0 0 1
2 0.11 0.88 7 ... 0 0 1
3 0.72 0.87 5 ... 0 1 0
4 0.37 0.52 2 ... 0 1 0
[5 rows x 13 columns]
data.head(): satisfaction_level last_evaluation ... support technical
0 0.38 0.53 ... 0 0
1 0.80 0.86 ... 0 0
2 0.11 0.88 ... 0 0
3 0.72 0.87 ... 0 0
4 0.37 0.52 ... 0 0
[5 rows x 21 columns]
data.left.value_counts():
0 11428
1 3571
Name: left, dtype: int64
Y_data.shape: (14999, 1)
Y.shape torch.Size([14999, 1])
X: tensor([[0.3800, 0.5300, 2.0000, ..., 1.0000, 0.0000, 0.0000],
[0.8000, 0.8600, 5.0000, ..., 1.0000, 0.0000, 0.0000],
[0.1100, 0.8800, 7.0000, ..., 1.0000, 0.0000, 0.0000],
...,
[0.3700, 0.5300, 2.0000, ..., 0.0000, 1.0000, 0.0000],
[0.1100, 0.9600, 6.0000, ..., 0.0000, 1.0000, 0.0000],
[0.3700, 0.5200, 2.0000, ..., 0.0000, 1.0000, 0.0000]])
X.size(): torch.Size([14999, 20])
E:\Professional Software\Anconda\envs\pytracking\lib\site-packages\torch\nn\functional.py:1350: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
epoch: 0 loss: 0.7312576770782471
epoch: 1 loss: 0.7727270722389221
epoch: 2 loss: 0.7783536314964294
epoch: 3 loss: 0.7793934345245361
epoch: 4 loss: 0.7771381139755249
epoch: 5 loss: 0.7743889689445496
epoch: 6 loss: 0.7647398114204407
epoch: 7 loss: 0.7545432448387146
epoch: 8 loss: 0.7476876974105835
epoch: 9 loss: 0.7408889532089233
epoch: 10 loss: 0.7297168374061584
epoch: 11 loss: 0.7265633344650269
epoch: 12 loss: 0.8101921081542969
epoch: 13 loss: 0.7332798838615417
epoch: 14 loss: 0.716731607913971
epoch: 15 loss: 0.7073214650154114
epoch: 16 loss: 0.696769654750824
epoch: 17 loss: 0.690093457698822
epoch: 18 loss: 0.6791982054710388
epoch: 19 loss: 0.6695009469985962
epoch: 20 loss: 0.6602796912193298
epoch: 21 loss: 0.6475608944892883
epoch: 22 loss: 0.639624297618866
epoch: 23 loss: 0.6317117214202881
epoch: 24 loss: 0.6239936351776123
epoch: 25 loss: 0.6263017058372498
epoch: 26 loss: 0.6041481494903564
epoch: 27 loss: 0.5972230434417725
epoch: 28 loss: 0.5907231569290161
epoch: 29 loss: 0.585003137588501
epoch: 30 loss: 0.5898972153663635
epoch: 31 loss: 0.5837881565093994
epoch: 32 loss: 0.5768670439720154
epoch: 33 loss: 0.5715934038162231
epoch: 34 loss: 0.5673941969871521
epoch: 35 loss: 0.5641562342643738
epoch: 36 loss: 0.5610777139663696
epoch: 37 loss: 0.5590518116950989
epoch: 38 loss: 0.5567962527275085
epoch: 39 loss: 0.5620077252388
epoch: 40 loss: 0.5546135306358337
epoch: 41 loss: 0.5531746745109558
epoch: 42 loss: 0.55274897813797
epoch: 43 loss: 0.5511655807495117
epoch: 44 loss: 0.5524853467941284
epoch: 45 loss: 0.5519489645957947
epoch: 46 loss: 0.5520744323730469
epoch: 47 loss: 0.5514076352119446
epoch: 48 loss: 0.5520205497741699
epoch: 49 loss: 0.5527095794677734
epoch: 50 loss: 0.5532509684562683
epoch: 51 loss: 0.5529757738113403
epoch: 52 loss: 0.5533396005630493
epoch: 53 loss: 0.5538538098335266
epoch: 54 loss: 0.5545904636383057
epoch: 55 loss: 0.5555252432823181
epoch: 56 loss: 0.5529634952545166
epoch: 57 loss: 0.5549248456954956
epoch: 58 loss: 0.5557812452316284
epoch: 59 loss: 0.5557355284690857
epoch: 60 loss: 0.5564326643943787
epoch: 61 loss: 0.5556958317756653
epoch: 62 loss: 0.5536842942237854
epoch: 63 loss: 0.5552572011947632
epoch: 64 loss: 0.5563111305236816
epoch: 65 loss: 0.5552616715431213
epoch: 66 loss: 0.5558472275733948
epoch: 67 loss: 0.5550410151481628
epoch: 68 loss: 0.5725806951522827
epoch: 69 loss: 0.5583928227424622
epoch: 70 loss: 0.5544316172599792
epoch: 71 loss: 0.5551984906196594
epoch: 72 loss: 0.5552375912666321
epoch: 73 loss: 0.5546104907989502
epoch: 74 loss: 0.5539920926094055
epoch: 75 loss: 0.5583476424217224
epoch: 76 loss: 0.575699508190155
epoch: 77 loss: 0.556276798248291
epoch: 78 loss: 0.5547029972076416
epoch: 79 loss: 0.5523102879524231
epoch: 80 loss: 0.5545598864555359
epoch: 81 loss: 0.5762888193130493
epoch: 82 loss: 0.5546205043792725
epoch: 83 loss: 0.5516649484634399
epoch: 84 loss: 0.5519871115684509
epoch: 85 loss: 0.550129234790802
epoch: 86 loss: 0.5504892468452454
epoch: 87 loss: 0.5495496988296509
epoch: 88 loss: 0.5545125603675842
epoch: 89 loss: 0.5482568144798279
epoch: 90 loss: 0.566870927810669
epoch: 91 loss: 0.5604091882705688
epoch: 92 loss: 0.5596952438354492
epoch: 93 loss: 0.5566151738166809
epoch: 94 loss: 0.5397318601608276
epoch: 95 loss: 0.5474585890769958
epoch: 96 loss: 0.5286720395088196
epoch: 97 loss: 0.5305213332176208
epoch: 98 loss: 0.5286723375320435
epoch: 99 loss: 0.525821328163147
loss_fn(model(X), Y): tensor(0.5258, grad_fn=<BinaryCrossEntropyBackward>)