利用pytorch两层线性网络对titanic数据集进行分类(kaggle)

时间:2024-05-04 07:16:04
import torch from torch.utils.data import Dataset from torch.utils.data import DataLoader import numpy as np from torchvision import datasets from torchvision import transforms import pandas as pd class titanicDataset(Dataset): def __init__(self,filepath): xy=np.loadtxt(filepath,delimiter=',',skiprows=1,usecols=[1,2,7,8],dtype=np.float32) self.len=xy.shape[0] # print(self.len) self.y_data=torch.from_numpy(xy[:,[0]]) self.x_data=torch.from_numpy(xy[:,1:]) def __getitem__(self,index):#获取索引元素 return self.x_data[index],self.y_data[index] def __len__(self): return self.len dataset=titanicDataset('./pytorch/dataset/titanic/train.csv') train_loader=DataLoader(dataset=dataset,batch_size=32,shuffle=True,num_workers=0) # print(dataset.x_data,dataset.y_data) test_loader=DataLoader(dataset=np.loadtxt('./pytorch/dataset/titanic/test.csv',delimiter=',',skiprows=1,usecols=[1,6,7],dtype=np.float32),batch_size=32,shuffle=False,num_workers=0) print(next(iter(test_loader))) class Model(torch.nn.Module): def __init__(self): super(Model,self).__init__() # self.linear1=torch.nn.Linear(4,3) self.linear2=torch.nn.Linear(3,2) self.linear3=torch.nn.Linear(2,1) self.sigmoid=torch.nn.Sigmoid() def forward(self,x): # x=self.sigmoid(self.linear1(x)) x=self.sigmoid(self.linear2(x)) x=self.sigmoid(self.linear3(x)) return x model=Model() criterion=torch.nn.BCELoss(size_average=True) optimizer=torch.optim.SGD(model.parameters(),lr=0.1,momentum=0.9) for epoch in range(10000): acc_num=0 for i,data in enumerate(train_loader,0): #1.Prepare data inputs,labels=data # print(inputs.shape[0]) #2.Forward y_pred=model(inputs) loss=criterion(y_pred,labels) # print(epoch,i,loss.item()) #3.Backward optimizer.zero_grad() loss.backward() #4.Update optimizer.step() y_pred_label=torch.where(y_pred>0.5,torch.tensor([1.0]),torch.tensor([0.0])) acc_num+=torch.eq(y_pred_label,labels).sum().item() # print(acc_num,len(dataset),len(train_loader.dataset)) acc=acc_num/len(dataset) print(acc) # print(test_loader) # print(test_loader.dataset.shape) out = model(torch.tensor(test_loader.dataset)) y_pred = torch.where(out>0.5,torch.tensor([1.0]),torch.tensor([0.0]))[:,0] print(y_pred) print(pd.Series(y_pred)) id=pd.read_csv('./pytorch/dataset/titanic/test.csv',usecols=['PassengerId']).iloc[:,0] # print(type(id)) pd.DataFrame({'PassengerId':id,'Survived':pd.Series(y_pred,dtype=int)}).to_csv('pred.csv',index=None) a=pd.DataFrame([id,pd.Series(y_pred)]) print(a) # print(y_pred[-10:]) # for x in test_loader: # print(x.shape) # out = model(x) # y_pred = torch.where(out>0.5,torch.tensor([1.0]),torch.tensor([0.0])) # print(y_pred)