Auto_ML自动机器学习之TPOT——学习笔记(2)

Auto_ML自动机器学习之TPOT——学习笔记(2)

  • 数据预处理
    • mat文件格式转化为csv
    • 批量读取文件路径
    • 整合——批量mat转csv多文件合并

数据预处理

mat文件格式转化为csv

环境 Win10+Python3.6.8+Pycharm

import pandas as pd
import scipy
from scipy import io
features_struct = scipy.io.loadmat('E:\Auto_ML_datasets\12k Drive End Bearing Fault Data\A_1hp\A_data_0.mat')
features = features_struct['X098_DE_time']
dfdata = pd.DataFrame(features)
datapath1 = 'E:\\Auto_ML_datasets\\12k Drive End Bearing Fault Data\\A_1hp\\A_data_0.csv'
dfdata.to_csv(datapath1, index=False)

批量读取文件路径

import os
def iterbrowse(path):
    for home, dirs, files in os.walk(path):
        for filename in files:
            yield os.path.join(home, filename)
for fullname in iterbrowse('E:\\Auto_ML_datasets\\12k Drive End Bearing Fault Data\\A_1hp\\'):
    print (fullname)

整合——批量mat转csv多文件合并

import pandas as pd
import numpy as np
import scipy
from scipy import io
import os

#批量读取文件路径
def iterbrowse(path):
    for home, dirs, files in os.walk(path):
        for filename in files:
            yield os.path.join(home, filename)
#mat转csv
def mat_to_csv(mat_read_path,csv_save_path):
    features_struct = scipy.io.loadmat(mat_read_path)
    for features_key in list(features_struct.keys()):
        if 'DE' in features_key:
            features = features_struct[features_key]
            dfdata = pd.DataFrame(features)
            dfdata = dfdata.T
            features_T=np.transpose(features)
            features_list=(features_T.tolist())[0]
            DE_all_list.append(features_list)
            datapath1 =[csv_save_path+'\\'+str(i)+'DE.csv']
            dfdata.to_csv(datapath1[0], header=False, index=False)

i=0
DE_all_list=[]
for fullname in iterbrowse('E:\\Auto_ML_datasets\\12k Drive End Bearing Fault Data\\A_1hp'):
    mat_to_csv(fullname,'E:\\Auto_ML_datasets\\12k Drive End Bearing Fault Data\\csv')
    i = i + 1
DE_all_array=pd.DataFrame(DE_all_list)
DE_all_array.to_csv('E:\\Auto_ML_datasets\\12k Drive End Bearing Fault Data\\csv\\DE_all_array.csv', header=False, index=False)

你可能感兴趣的:(Auto_ML自动机器学习之TPOT——学习笔记(2))