预处理4

跟風遠走 提交于 2020-01-28 05:05:23
###王博士(1)
import matplotlib.pyplot as plt
import numpy.linalg as LA
import scipy.io as scio
import numpy as np
import xlrd
import os,time,sys
import warnings
warnings.filterwarnings('ignore')
def print_progress(num, max_num):  
    progress = num / max_num
    # Define the length of bar
    barLength = 50
    # Ceck the input!
    assert type(progress) is float, "id is not a float: %r" % id
    assert 0 <= progress <= 1, "variable should be between zero and one!"
    # Empty status while processing.
    status = ""
    # This part is to make a new line when the process is finished.
    if progress >= 1:
        progress = 1
        status = "\r\n"
    # Where we are in the progress!
    indicator = int(round(barLength * progress))
    # Print the appropriate progress phase!
    list = ["#" * indicator , ">" * (barLength - indicator), progress * 100]
    text = "\r{0[0]} {0[1]} {0[2]:.2f}% completed.{1}".format(list, status)
    sys.stdout.write(text)
    sys.stdout.flush()
def get_xlsxlist(xlsx_path, mode):
    return [os.path.join(xlsx_path, f) for f in os.listdir(xlsx_path) if f.endswith(mode)]
# 一维数据固定采样数
def sample_num(x, num=3000):
    dim = x.flatten().shape[0]
    interval = int(dim / num)
    return np.array([x[interval * i] for i in range(num)])
#@markdown **特征提取函数**
def feature_extr(NAME_list, feature_dim=1000, feature_speed=100, max_speed=25):
    num_bin=100
    print('Start to extract the features:')
    init_m = np.zeros(feature_dim) # 轨迹图特征初始化
    init_n = np.zeros(feature_speed) # 速占比初始化
    # inv = 10 #@param {type:"integer"}
    for num, name in enumerate(NAME_list):
        max_num = len(NAME_list)
        print_progress(num + 1, max_num)
        excel_trawl = xlrd.open_workbook(name)
        sheet = excel_trawl.sheet_by_index(0)
        x = sheet.col_values(1)[1:]
        y = sheet.col_values(2)[1:]
        data_position = np.vstack([np.array(x).reshape(1, -1), np.array(y).reshape(1, -1)])
        data_cov = np.cov(data_position)
        w, v = LA.eig(data_cov)
        data_pca = np.dot(v[:, 0].T, data_position).flatten()
    #     data_pca = np.dot(v[:, 1].T, data_position).flatten()
        sample_data = sample_num(data_pca, 1000)
        init_m = np.vstack([init_m, sample_data]) # pca特征采样
        z = sheet.col_values(3)[1:]
        z_lim = [i for i in z if i <= max_speed]       
#         fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 6))
#         n, bins, patches = plt.hist(z_lim, num_bin, normed=True, rwidth=0.8)
        n, bins = np.histogram(z_lim, num_bin, normed=True)
        init_n = np.vstack([init_n, n])   
    #@markdown - **提取的刺网样本1000维特征**
    gill_net_sample = init_m[1:]    
    #@markdown - **速占比归一化特征提取**
    speed_feature = init_n[1:]
    speed_feature_g = speed_feature / speed_feature.max()    
    print('Feature extraction completed.')
    return (gill_net_sample, speed_feature_g)在这里插入代码片
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!