西儲大學軸承故障數據 python3使用

轉載於:參考鏈接
西儲大學軸承故障數據官網
python2 版本 cwru庫github地址
Github數據下載地址
python2 可以直接安裝使用CWRU庫,該庫的功能是下載數據,並且切分成可供訓練和評估的訓練集和測試集數據

python2安裝方式:

pip安裝:

$ pip install --user cwru

github下載源代碼安裝:
`$ python setup.py install
使用

import cwru
data = cwru.CWRU("12DriveEndFault", "1797", 384)

可以使用data.X_train, data.y_train, data.X_test, data.y_test, data.labels, data.nclasses 來訓練和評估模型

CWRU的參數:

exp:‘12DriveEndFault’, ‘12FanEndFault’, ‘48DriveEndFault’

rpm:‘1797’, ‘1772’, ‘1750’, ‘1730’

length:信號的長度

python3版本:

由於python3 和python2 版本的差異,對原python2代碼修改:
這產生的路徑名和原文不同 生成到項目目錄下

import os
import glob
import errno
import random
import urllib.request as urllib
import numpy as np
from scipy.io import loadmat


class CWRU:
    def __init__(self, exp, rpm, length):
        if exp not in ('12DriveEndFault', '12FanEndFault', '48DriveEndFault'):
            print("wrong experiment name: {}".format(exp))
            exit(1)
        if rpm not in ('1797', '1772', '1750', '1730'):
            print("wrong rpm value: {}".format(rpm))
            exit(1)
        # root directory of all data
        rdir = os.path.join('Data/CWRU')
        print(rdir)
        fmeta = os.path.join(os.path.dirname(__file__), 'metadata.txt')
        all_lines = open(fmeta).readlines()
        lines = []
        for line in all_lines:
            l = line.split()
            if (l[0] == exp or l[0] == 'NormalBaseline') and l[1] == rpm:
                lines.append(l)
        self.length = length  # sequence length
        self._load_and_slice_data(rdir, lines)
        # shuffle training and test arrays
        self._shuffle()
        self.labels = tuple(line[2] for line in lines)
        self.nclasses = len(self.labels)  # number of classes

    def _mkdir(self, path):
        try:
            os.makedirs(path)
        except OSError as exc:
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                pass
            else:
                print("can't create directory '{}''".format(path))
                exit(1)

    def _download(self, fpath, link):
        print("Downloading to: '{}'".format(fpath))
        urllib.URLopener().retrieve(link, fpath)

    def _load_and_slice_data(self, rdir, infos):
        self.X_train = np.zeros((0, self.length))
        self.X_test = np.zeros((0, self.length))
        self.y_train = []
        self.y_test = []
        for idx, info in enumerate(infos):

            # directory of this file
            fdir = os.path.join(rdir, info[0], info[1])
            self._mkdir(fdir)
            fpath = os.path.join(fdir, info[2] + '.mat')
            if not os.path.exists(fpath):
                self._download(fpath, info[3].rstrip('\n'))

            mat_dict = loadmat(fpath)
            # key = filter(lambda x: 'DE_time' in x, mat_dict.keys())[0]
            fliter_i = filter(lambda x: 'DE_time' in x, mat_dict.keys())
            fliter_list = [item for item in fliter_i]
            key = fliter_list[0]
            time_series = mat_dict[key][:, 0]
            idx_last = -(time_series.shape[0] % self.length)
            clips = time_series[:idx_last].reshape(-1, self.length)
            n = clips.shape[0]
            n_split = int((3 * n / 4))
            self.X_train = np.vstack((self.X_train, clips[:n_split]))
            self.X_test = np.vstack((self.X_test, clips[n_split:]))
            self.y_train += [idx] * n_split
            self.y_test += [idx] * (clips.shape[0] - n_split)

    def _shuffle(self):
        # shuffle training samples
        index = list(range(self.X_train.shape[0]))
        random.Random(0).shuffle(index)
        self.X_train = self.X_train[index]
        self.y_train = tuple(self.y_train[i] for i in index)

        # shuffle test samples
        index = list(range(self.X_test.shape[0]))
        random.Random(0).shuffle(index)
        self.X_test = self.X_test[index]
        self.y_test = tuple(self.y_test[i] for i in index)

由於下載出現302錯誤,所以自己打包下載了相關鏈接中的所有數據並且進行了數據轉換

#include <bits/stdc++.h>
#include <io.h>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
char a[100],b[100],c[100],d[100];
void getAllFiles(string path, vector<string>& files)
{
    // 文件句柄
    long hFile = 0;
    // 文件信息
    struct _finddata_t fileinfo;

    string p;

    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            // 保存文件的全路徑
            files.push_back(p.assign(path).append("\\").append(fileinfo.name));

        }
        while (_findnext(hFile, &fileinfo) == 0);   //尋找下一個,成功返回0,否則-1

        _findclose(hFile);
    }
}
void make_dir(string dir)
{
    if (_access(dir.c_str(), 0) == -1)
    {
        cout << dir << " is not existing" << endl;
        int flag = _mkdir(dir.c_str());

        if (flag == 0)
        {
            cout << "make successfully" << endl;
        }
        else
        {
            cout << "make fsiled" << endl;
        }
    }
    else if (_access(dir.c_str(), 0) == 0)
    {
        cout << dir << " exists" << endl;
    }
    else
    {
        cout<<"erro!"<<endl;
    }
}

int CopyFile(string SourceFile,string NewFile)
{
    ifstream in;
    ofstream out;
    in.open(SourceFile,ios::binary);//打開源文件
    if(in.fail())//打開源文件失敗
    {
        cout<<"Error 1: Fail to open the source file."<<endl;
        in.close();
        out.close();
        return 0;
    }
    out.open(NewFile,ios::binary);//創建目標文件
    if(out.fail())//創建文件失敗
    {
        cout<<"Error 2: Fail to create the new file."<<endl;
        out.close();
        in.close();
        return 0;
    }
    else//複製文件
    {
        out<<in.rdbuf();
        out.close();
        in.close();
        return 1;
    }
}

int main()
{
    freopen("metadata.txt","r",stdin);
    int count1 = 0,count2 =0;
    while(scanf("%s %s %s %s",&a,&b,&c,&d)!=EOF)
    {
        count2++;
        int len=strlen(d),x=0;
        for(int i=0; i<len; i++)
            if(d[i]<='9' && d[i] >='0')
                x=x*10+d[i]-'0';
        string A=string(a),B=string(b),C=string(c);
        cout<<A<<" "<<B<<" "<<C<<" "<<x<<endl;
        string filePath1 = "D:\\ACM_CODE\\ACMtask\\data";
        string filePath2 = "D:\\ACM_CODE\\ACMtask\\datas";
        vector<string> temp;
        int y=0;
        getAllFiles(filePath1, temp);
        for (int i = 0; i < temp.size(); i++ )
        {
            y=0;
            //  cout<<i<<"  "<<temp[i]<<endl;
            for(int j=0; j<temp[i].length(); j++)
            {
                if(temp[i][j] >= '0' && temp[i][j] <= '9')
                    y=y*10+temp[i][j]-'0';
                else if(temp[i][j] =='.' && temp[i][j+1] =='m' &&
                        temp[i][j+2] =='a' && temp[i][j+3] =='t')
                {
                    break;
                }
                else
                    y=0;
            }
            if(x == y)
            {
                y=i;
                break;
            }
        }
        cout<<y<<"  "<<temp[y]<<endl;
        string dir=filePath2+"\\"+A;
        make_dir(dir);
        dir=dir+"\\"+B;
        make_dir(dir);
        string source=temp[y];
        string NewFile=dir+"\\"+C+".mat";
        if(CopyFile(source,NewFile))
        {
            cout<<"文件已成功複製..."<<endl;
            count1++;
        }
        else
        {
            cout<<"文件複製失敗..."<<endl;

        }
    }
    printf("YES %d %d\n",count1,count2);
    return 0;
}

這裏提供下載分類好的數據集合
下載鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章