My utils.py.
#!/usr/bin/env python #-*-coding:utf-8-*-from datetime import datetime from datetime import Timedelta import Pandas a s PD import OS, copy, math, time, h5py, JSON, Shutil import NumPy as NP from Sklearn import preprocessing from Sklearn.dat
Asets Import dump_svmlight_file #from svmutil import Svm_read_problem # general matrix normalized def my_normalization (Data_ary, axis=0): # axis = 0 normalized by column; 1 O'Clock by line normalization if Axis = = 1:data_ary = Np.matrix (data_ary). T ans = preprocessing.scale (data_ary) Min_max_scaler = preprocessing. Minmaxscaler (feature_range= ( -1.0,1.0)) ans = min_max_scaler.fit_transform (ans) ans = np.matrix (ans). T ans = np.array (ans) Else:ans = Preprocessing.scale (data_ary) Min_max_scaler = preprocessing . Minmaxscaler (feature_range= ( -1.0,1.0)) ans = min_max_scaler.fit_transform (ans) return ans def one_hot (data_a Ry, One_hot_len): # data_ary = Array ([1,2,3,5,6,7,9]) # one_hot_len:one_hot longest column Max_num = np.mAx (data_ary); Ans = Np.zeros ((len (data_ary), One_hot_len), Dtype=np.float64) for I in Range (len (data_ary)): ans[i, int (data_ary[
I])] = 1.0 return ans def re_onehot (data_ary): # data_ary = Array ([[0,0,0,1.0],[1.0,0,0,0],...]) Ans = Np.zeros ((len (data_ary),), Dtype=np.float64) for I in Range (len (data_ary)): for J in Range (Len (data_ary[i,:]
)): if data_ary[i,j] = = 1.0:ans[i] = 1.0*j;
Break
Return ans # Write data to h5 file Def write2h5 (h5dumpfile,data): # If not os.path.exists (h5dumpfile): Os.makedir (H5dumpfile) With H5py. File (H5dumpfile, "w") as F:f.create_dataset ("Data", Data=data, Dtype=np.float64) # Exports the data from the H5 file def readH5 (H5dumpf
ile): feat = []; With H5py. File (H5dumpfile, "R") as F:feat.append (f[' data '] [:]) feat = Np.concatenate (feat, 1) print (' ReadH5 Feature . shape= ', Feat.shape) return Feat.astype (Np.float64) # Save Dict data to JSON def Store_json (file_name,data): with open (f Ile_name, ' W ') as Json_file:json_file.write (Json.dumps (data,indent=4)) # reads the data from the JSON file to the Dict def Load_json (file_name): With open (file_name) as Json_file:data = Json.load (json_file) return data #将一个文件copy到指定目录 def Movefilet O (SourceDir, TargetDir): Shutil.copy (SourceDir, targetDir) # Delete all files in directory def removedir (Dirpath): If not os.path.is Dir (dirpath): Return files = Os.listdir (dirpath) try:for file in Files:filepath = Os.path.jo In (Dirpath, file) if Os.path.isfile (FilePath): Os.remove (filePath) elif os.path.is Dir (filePath): RemoveDir (FilePath) os.rmdir (Dirpath) except Exception (): Print ("RemoveDir exce
Ption ")