将时序序列转为频域

因为240维运动特征类似声波,因为人的运动不同步用时刻作为特征是不对的应转为频域。

模型在验证集最准是0.8

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


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

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(600):
    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 test_acc==0.50 and acc>0.93:
    if acc==1:
        break
ax1 = plt.subplot(2,1,1)
ax2 = plt.subplot(2,1,2)
out=network(feature)
out=out.detach().numpy()
plt.sca(ax1)
for i in range(out.shape[0]):
    if label[i]==1:
        plt.scatter(out[i],0,color='red')
    if label[i]==0:
        plt.scatter(out[i],0,color='blue')        
out=network(test_feature)
out=out.detach().numpy()
plt.sca(ax2)
for i in range(out.shape[0]):
    if test_label[i]==1:
        plt.scatter(out[i],0,color='red')
        #if out[i]>0.9:
            #print(i)
    if test_label[i]==0:
        plt.scatter(out[i],0,color='blue')  
        #if out[i]>0.9:
            #print(i)


plt.show()
print('执行成功')
posted @ 2022-12-01 10:55  祥瑞哈哈哈  阅读(170)  评论(0)    收藏  举报