亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python利用CNN實現(xiàn)對時序數(shù)據(jù)進行分類

 更新時間:2023年02月22日 15:48:53   作者:wendy_ya  
這篇文章主要為大家詳細介紹了Python如何利用CNN實現(xiàn)對時序數(shù)據(jù)進行分類功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下

一、數(shù)據(jù)集介紹

數(shù)據(jù)集利用的是CPSC2020數(shù)據(jù)集。

訓(xùn)練數(shù)據(jù)包括從心律失?;颊呤占?0個單導(dǎo)聯(lián)心電圖記錄,每個記錄持續(xù)約24小時。

下載完成后的TrainingSet數(shù)據(jù)集包括兩個文件夾,分別是data和ref。data和ref文件夾內(nèi)分別有10個mat文件。

  • data文件夾存儲數(shù)據(jù)文件,每個文件以mat格式存儲,n ∗ 1 n*1n∗1數(shù)組表示;
  • ref文件夾為標簽文件夾,每個文件以mat文件存儲,結(jié)構(gòu)體存儲,包括S_ref,V_ref兩個n*1數(shù)組,分別存儲對應(yīng)標簽(S,V)的位置;

采樣率為 400。

  • S:室上早搏(SPB);
  • V:心室早搏(PVC);

數(shù)據(jù)集鏈接如下:http://2020.icbeb.org/CSPC2020

二、數(shù)據(jù)預(yù)處理

2.1 獲取原始數(shù)據(jù)

查看一下前1000個心電圖數(shù)據(jù):

datafile = 'E:/Wendy/Desktop/TrainingSet/data/A04.mat'# 采樣率400
data = scio.loadmat(datafile)
#rint(data) # dict

sig = data['ecg']# (x,1)
#print(sig)
sig = np.reshape(sig,(-1)) # (x,)轉(zhuǎn)換為一維向量
print(sig)
sigPlot = sig[1:5*200]# # 獲取前1000個信號
fig = plt.figure(figsize=(20, 10),dpi=400)
plt.plot(sigPlot)
plt.show()

運行結(jié)果:

2.2 獲取原始標簽

將標簽數(shù)據(jù)轉(zhuǎn)化為一維向量

datafile = 'E:/Wendy/Desktop/TrainingSet/ref/R04.mat'# 采樣率400
data = scio.loadmat(datafile)
#print(data)
label = data['ref'][0][0]
S_ref = label[0];
S_ref = np.reshape(S_ref,(-1)) # 轉(zhuǎn)換為一維向量
V_ref = label[1];
V_ref = np.reshape(V_ref,(-1)) # 轉(zhuǎn)換為一維向量

2.3 數(shù)據(jù)分割

數(shù)據(jù)分割為5s一個片段

思路:房早室早心拍和前后兩個心拍均有關(guān)系,按照平均心率72計算,平均每個心拍的時間為60/72,因此5個心拍的時間為60/725=4.1667 4.1667s不好計算,故選擇5s 5 ( 秒 ) s a m p r = 5 ∗ 400 = 2000 個 s a m p l e 5(秒)sampr = 5*400=2000個sample5(秒)sampr=5∗400=2000個sample

定義標簽:0:其他;1:V_ref; 2:S_ref;

a = len(sig)
Fs = 400 # 采樣率為400
segLen = 5*Fs # 2000
num = int(a/segLen)
print(num)

運行結(jié)果:

17650

其中Fs為采樣率,segLen為片段長度,num為片段數(shù)量。

2.4 整合數(shù)據(jù)和標簽

接下來需要整合數(shù)據(jù)和標簽:

all_data=[]
all_label = [];
i=1
while i<num+1: 
    all_data.append(np.array(sig[(i-1)*segLen:i*segLen]))
    # 標簽
    if set(S_ref) & set(range((i-1)*segLen,i*segLen)):
        all_label.append(2)
    elif set(V_ref) & set(range((i-1)*segLen,i*segLen)):
        all_label.append(1)        
    else:
        all_label.append(0)    
    i=i+1
type(all_data)# list類型
type(all_label)# list類型
print((np.array(all_data)).shape) # 17650為數(shù)據(jù)長度,2000為數(shù)據(jù)個數(shù)
print((np.array(all_label)).shape)
#print(all_data)

運行結(jié)果:

(17650, 2000)
(17650,)

17650為數(shù)據(jù)長度,2000為數(shù)據(jù)個數(shù)。

2.5 保存

將數(shù)據(jù)保存為字典類型:

import pickle
res = {'data':all_data, 'label':all_label} # 字典類型dict
with open('./cpsc2020.pkl', 'wb') as fout: # #將結(jié)果保存為cpsc2020.pkl
    pickle.dump(res, fout)

三、數(shù)據(jù)訓(xùn)練

3.1 讀取數(shù)據(jù)并進行處理

將數(shù)據(jù)歸一化并進行標簽編碼,劃分訓(xùn)練集和測試集,訓(xùn)練集為90%,測試集為10%,打亂數(shù)據(jù)并將其擴展為二維:

import numpy as np
import pandas as pd
import scipy.io
from matplotlib import pyplot as plt
import pickle
from sklearn.model_selection import train_test_split
from collections import Counter
from tqdm import tqdm

def read_data_physionet():
    """
    only N V, S
    """
    # read pkl
    with open('./cpsc2020.pkl', 'rb') as fin:
        res = pickle.load(fin) # 加載數(shù)據(jù)集
    ## 數(shù)據(jù)歸一化
    all_data = res['data']
    for i in range(len(all_data)):
        tmp_data = all_data[i]
        tmp_std = np.std(tmp_data) # 獲取數(shù)據(jù)標準差
        tmp_mean = np.mean(tmp_data) # 獲取數(shù)據(jù)均值
        if(tmp_std==0):   # i=1239-1271均為0
            tmp_std = 1 
        all_data[i] = (tmp_data - tmp_mean) / tmp_std  # 歸一化
    all_data = []
    ## 標簽編碼
    all_label = []
    for i in range(len(res['label'])):
        if res['label'][i] == 1:
            all_label.append(1)
            all_data.append(res['data'][i])
        elif res['label'][i] == 2:
            all_label.append(2)
            all_data.append(res['data'][i])
        else:
            all_label.append(0)
            all_data.append(res['data'][i])       
    all_label = np.array(all_label)
    all_data = np.array(all_data)

    # 劃分訓(xùn)練集和測試集,訓(xùn)練集90%,測試集10%
    X_train, X_test, Y_train, Y_test = train_test_split(all_data, all_label, test_size=0.1, random_state=15)
    
 
    print('訓(xùn)練集和測試集中 其他類別(0);室早(1);房早(2)的數(shù)量: ')
    print(Counter(Y_train), Counter(Y_test))
    
    # 打亂訓(xùn)練集
    shuffle_pid = np.random.permutation(Y_train.shape[0])
    X_train = X_train[shuffle_pid]
    Y_train = Y_train[shuffle_pid]

    # 擴展為二維(x,1)
    X_train = np.expand_dims(X_train, 1)
    X_test = np.expand_dims(X_test, 1)

    return X_train, X_test, Y_train, Y_test
X_train, X_test, Y_train, Y_test = read_data_physionet()

運行結(jié)果:

訓(xùn)練集和測試集中 其他類別(0);室早(1);房早(2)的數(shù)量:
Counter({1: 8741, 0: 4605, 2: 2539}) Counter({1: 1012, 0: 478, 2: 275})

3.2 構(gòu)建數(shù)據(jù)結(jié)構(gòu)

自行構(gòu)建數(shù)據(jù)集:

# 構(gòu)建數(shù)據(jù)結(jié)構(gòu) MyDataset
# 單條數(shù)據(jù)信號的形狀為:1*2000
import numpy as np
from collections import Counter
from tqdm import tqdm
from matplotlib import pyplot as plt
from sklearn.metrics import classification_report 

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader

class MyDataset(Dataset):
    def __init__(self, data, label):
        self.data = data
        self.label = label
    #把numpy轉(zhuǎn)換為Tensor
    def __getitem__(self, index):
        return (torch.tensor(self.data[index], dtype=torch.float), torch.tensor(self.label[index], dtype=torch.long))

    def __len__(self):
        return len(self.data)

3.3 搭建神經(jīng)網(wǎng)絡(luò)

搭建CNN網(wǎng)絡(luò)結(jié)構(gòu):

# 搭建神經(jīng)網(wǎng)絡(luò)
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(         # input shape (1, 1, 2000)
            nn.Conv1d(
                in_channels=1,
                out_channels=16,
                kernel_size=5,
                stride=1, 
                padding=2,
            ),                              # output shape (16, 1, 2000)
            nn.Dropout(0.2),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=5),    # choose max value in 1x5 area, output shape (16, 1, 400)2000/5
        )
        self.conv2 = nn.Sequential(         # input shape (16, 1, 400)
            nn.Conv1d(16, 32, 5, 1, 2),     # output shape (32, 1, 400)
            nn.Dropout(0.2),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=5),    # output shape (32, 1, 400/5=80)
        )
        self.out = nn.Linear(32 *  80, 3)   # fully connected layer, output 3 classes

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.out(x)
        #output.Softmax()
        return output, x
cnn = CNN()
print(cnn)

運行結(jié)果:

CNN(
(conv1): Sequential(
(0): Conv1d(1, 16, kernel_size=(5,), stride=(1,), padding=(2,))
(1): Dropout(p=0.2, inplace=False)
(2): ReLU()
(3): MaxPool1d(kernel_size=5, stride=5, padding=0, dilation=1, ceil_mode=False)
)
(conv2): Sequential(
(0): Conv1d(16, 32, kernel_size=(5,), stride=(1,), padding=(2,))
(1): Dropout(p=0.2, inplace=False)
(2): ReLU()
(3): MaxPool1d(kernel_size=5, stride=5, padding=0, dilation=1, ceil_mode=False)
)
(out): Linear(in_features=2560, out_features=3, bias=True)
)

3.4 開始訓(xùn)練

優(yōu)化器利用的是Adam優(yōu)化器,損失函數(shù)使用crossEntropy函數(shù)。

代碼略

50個epoch的運行效果如下:

以上就是Python利用CNN實現(xiàn)對時序數(shù)據(jù)進行分類的詳細內(nèi)容,更多關(guān)于Python CNN時序數(shù)據(jù)分類的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于Python制作炸金花游戲的過程詳解

    基于Python制作炸金花游戲的過程詳解

    《詐金花》又叫三張牌,是在全國廣泛流傳的一種民間多人紙牌游戲。比如JJ比賽中的詐金花(贏三張),具有獨特的比牌規(guī)則。本文江將通過Python語言實現(xiàn)這一游戲,需要的可以參考一下
    2022-02-02
  • 簡介Python設(shè)計模式中的代理模式與模板方法模式編程

    簡介Python設(shè)計模式中的代理模式與模板方法模式編程

    這篇文章主要介紹了Python設(shè)計模式中的代理模式與模板方法模式編程,文中舉了兩個簡單的代碼片段來說明,需要的朋友可以參考下
    2016-02-02
  • python 制作簡單的音樂播放器

    python 制作簡單的音樂播放器

    這篇文章主要介紹了python 制作簡單的音樂播放器,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • Python使用Matplotlib庫創(chuàng)建3D 圖形和交互式圖形詳解

    Python使用Matplotlib庫創(chuàng)建3D 圖形和交互式圖形詳解

    Matplotlib 是 Python 中最重要的數(shù)據(jù)可視化庫之一,在本文中,我們將深入研究 Matplotlib 的高級特性,特別是如何創(chuàng)建 3D 圖形和交互式圖形,需要的朋友可以參考下
    2023-07-07
  • Python+Tkinter制作專屬圖形化界面

    Python+Tkinter制作專屬圖形化界面

    這篇文章主要是帶著大家通過Python Tkinter制作一個屬于自己的GUI圖形化界面,可以用于設(shè)計簽名的哦,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-04-04
  • python pip安裝包出現(xiàn):Failed building wheel for xxx錯誤的解決

    python pip安裝包出現(xiàn):Failed building wheel for xxx錯誤的解決

    今天小編就為大家分享一篇python pip安裝包出現(xiàn):Failed building wheel for xxx錯誤的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python?海象運算符(?:=)的三種用法

    Python?海象運算符(?:=)的三種用法

    這篇文章主要介紹了Python?海象運算符(:=)的三種用法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-06-06
  • Pycharm導(dǎo)包失敗問題及解決

    Pycharm導(dǎo)包失敗問題及解決

    這篇文章主要介紹了Pycharm導(dǎo)包失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python?教程實現(xiàn)?turtle海龜繪圖

    python?教程實現(xiàn)?turtle海龜繪圖

    這篇文章主要介紹了python?教程實現(xiàn)?turtle繪制海龜繪圖,文章基于python的相關(guān)資料展開turtle繪制海龜繪圖的詳細內(nèi)容,需要的小伙伴可以參考一下
    2022-05-05
  • Python利用flask sqlalchemy實現(xiàn)分頁效果

    Python利用flask sqlalchemy實現(xiàn)分頁效果

    這篇文章主要為大家詳細介紹了利用flask sqlalchemy實現(xiàn)分頁效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論