如何优化此代码以使其更快?

时间:2017-10-03 21:33:00

标签: python performance optimization

我在HDF5文件上执行一组除法运算和重新整形。由于我的数据点数量约为4000,我如何优化此代码以使其更快?

def pre_proc_data():
    jointMatrix = np.array([], dtype=np.float64).reshape(0, 500 * 17)
    hdf5_file = h5py.File("/home/Data.hdf5")

    for j in range(len(hdf5_file["vector"])):
        # Normalization
        norm_vec = hdf5_file["vector"][j]
        norm_vec[:, 0] = (norm_vec[:, 0] - (-3.059)) / 6.117  # W0 - Left and right
        norm_vec[:, 5] = (norm_vec[:, 5] - (-3.059)) / 6.117
        norm_vec[:, 1] = (norm_vec[:, 1] - (-1.5707)) / 3.6647  # W1 
        norm_vec[:, 6] = (norm_vec[:, 6] - (-1.5707)) / 3.6647
        norm_vec[:, 2] = (norm_vec[:, 2] - (-3.059)) / 6.117  # W2 
        norm_vec[:, 14] = (norm_vec[:, 14] - (-3.059)) / 6.117
        norm_vec[:, 3] = (norm_vec[:, 3] - (-1.7016)) / 3.4033  # S0 
        norm_vec[:, 10] = (norm_vec[:, 10] - (-1.7016)) / 3.4033
        norm_vec[:, 4] = (norm_vec[:, 4] - (-2.147)) / 3.194  # s1 
        norm_vec[:, 8] = (norm_vec[:, 8] - (-2.147)) / 3.194

        norm_vec[:, 11] = (norm_vec[:, 11] - (-3.0541)) / 6.1083  # eo 
        norm_vec[:, 15] = (norm_vec[:, 15] - (-3.0541)) / 6.1083
        norm_vec[:, 12] = (norm_vec[:, 12] - (-0.05)) / 2.67  # e1 
        norm_vec[:, 16] = (norm_vec[:, 16] - (-0.05)) / 2.67

        reshaped_vec = hdf5_file["vector"][j].reshape(500 * 17)
        jointMatrix = np.vstack((jointMatrix, reshaped_vec))

    return jointMatrix


jointMatrix = pre_proc_data()

0 个答案:

没有答案