拿神经网络写一下泰坦尼克
- 导入数据,用dataset&dataloader
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd
class MyDataset(Dataset):
def __init__(self, filepath):
# 从中提取5个特征
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare"]
data = pd.read_csv(filepath)
self.len = data.shape[0]
#将dataframe转为张量并分为x和y
#get_dummies会自动帮你处理好特征的转换,好东西!
self.x_data = torch.from_numpy(np.array(pd.get_dummies(data[features])))
self.y_data = torch.from_numpy(np.array(data["Survived"]))
def __getitem__(self, index):
return self.x_data[index], self.y_data[index]
def __len__(self):
return self.len
dataset = MyDataset(r'.\titanic\train.csv')
train_loader = DataLoader(dataset = dataset
,batch_size = 1
,shuffle = True
,num_workers = 0
)
注意,之前的各种数据预处理在这里直接调用pd.get_dummies就可以完成了,挺方便的
- 定义模型
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(6, 3)
self.linear2 = torch.nn.Linear(3, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
return x
# 定义一个预测函数
def predict(self, x):
with torch.no_grad():
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
y = []
for i in x:
if i>0.5:
y.append(1)
else :
y.append(0)
return y
model = Model()
- 构造损失函数和优化器
criterion = torch.nn.BCELoss(reduction="mean")
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
- 进行训练
if __name__ == '__main__':
loss_list = []
for epoch in range(200):
for i, data in enumerate(train_loader, 0):
inputs, labels = data
#进行类型转换不然报错
inputs = inputs.float()
labels = labels.float()
print(inputs, labels)
y_pred = model(inputs)
#压缩成1维
y_pred = y_pred.squeeze(-1)
loss = criterion(y_pred, labels)
print(epoch, i, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_list.append(loss.item())
# 画图
plt.plot(np.arange(epoch), loss_list)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
跑代码的时候没加画图的部分,而且用Jupyter跑的话很慢。。。还是用pycharm跑吧以后
- 进行预测
test_data = pd.read_csv("./titanic/test.csv")
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare"]
test = torch.from_numpy(np.array(pd.get_dummies(test_data[features])))
#预测
y = model.predict(test.float())
y
#输出预测结果
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y})
output.to_csv('my_predict.csv', index=False)
输出全是0,结果上传之后有0.6+的分数。。。我不太明白
还要继续优化的话就是多加一个隐藏层来提升性能,但这也意味着要更多的时间