这里我只把时域转为了频域。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
import torch.fft as fft
df = pd.read_csv('train.csv')
df=df.drop(['ID'],axis=1)
nmp=df.to_numpy()
feature=nmp[:-20,:-1]
label=nmp[:-20,-1]#(210,240)
feature=torch.fft.fft(torch.Tensor(feature))
feature=torch.abs(feature)/240*2
feature=feature[:,]
test_feature=nmp[-20:,:-1]
test_label=nmp[-20:,-1]#(210,240)
test_feature=torch.fft.fft(torch.Tensor(test_feature))
test_feature=torch.abs(test_feature)/240*2
test_feature=test_feature[:,]
from torch import nn
import torch
loss=nn.MSELoss()
feature=torch.Tensor(feature)
label=torch.Tensor(label)
label=label.reshape(-1,1)
test_feature=torch.Tensor(test_feature)
test_label=torch.Tensor(test_label)
test_label=test_label.reshape(-1,1)
network=nn.Sequential(nn.Linear(240,1),nn.Sigmoid())
import torch.optim as optim
optimizer = optim.Adam(network.parameters(), lr=0.004)
for epoch in range(6000):
optimizer.zero_grad()
out=network(feature)
l=loss(out,label)
l.backward()
optimizer.step()
Y = torch.ge(out, 0.5).float()
acc=Y.eq(label).float().sum()/len(label)
out=network(test_feature)
Y = torch.ge(out, 0.5).float()
test_acc=Y.eq(test_label).float().sum()/len(test_label)
print(epoch,l,acc,test_acc)
if acc>0.84 and test_acc==0.80:
break
df = pd.read_csv('test.csv')
df=df.drop(['ID'],axis=1)
nmp=df.to_numpy()
feature=nmp[:,:]
feature=torch.fft.fft(torch.Tensor(feature))
feature=torch.abs(feature)/240*2
feature=torch.Tensor(feature)
out=network(feature)
out=out.detach().numpy()
out=out>0.5
out=out.astype(np.int)
out=pd.DataFrame(out)
out.columns = ['CLASS']
w=[]
for k in range(out.shape[0]):
w.append(k+210)
out['ID']=np.reshape(w,(-1,1))
out[['ID','CLASS']].to_csv('out.csv',index=False)