如何将3个矩阵组合成具有可逆方法的1个矩阵?

时间:2019-02-05 15:16:48

标签: python numpy dataframe reshape reversing

我想对我的24x20矩阵'A''B''C'进行重塑,这些矩阵是从文本文件中提取的,并在通过{{ 1}}在for循环遍历循环中,每个循环将是一行,并排排列3个矩阵的所有元素,如下所示:

def normalize()

到目前为止,根据@odyse的建议,我在for循环末尾使用了以下代码段:

[[A(1,1),B(1,1),C(1,1),A(1,2),B(1,2),C(1,2),...,A(24,20),B(24,20),C(24,20)] #cycle1
 [A(1,1),B(1,1),C(1,1),A(1,2),B(1,2),C(1,2),...,A(24,20),B(24,20),C(24,20)] #cycle2
 [A(1,1),B(1,1),C(1,1),A(1,2),B(1,2),C(1,2),...,A(24,20),B(24,20),C(24,20)]] #cycle3

但是当我在for循环中的for cycle in range(cycles): dff = pd.DataFrame({'A_norm':A_norm[cycle] , 'B_norm': B_norm[cycle] , 'C_norm': C_norm[cycle] } , index=[0]) D = dff.as_matrix().ravel() if cycle == 0: Results = np.array(D) else: Results = np.vstack((Results, D2)) np.savetxt("Results.csv", Results, delimiter=",") 之后使用它时,尽管存在错误(ValueError),但对于def normalize()也有warning FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead的问题,这并不重要,但是现在由于仍然是FutureWarning,因此我使用D = dff.as_matrix().ravel()检查了输出的形状是否正确3个周期,并且它是(3,1440),这是3行,即3个周期,列数应该是480 = 1440的3倍,但总的来说不是稳定解决方案。

完整的脚本如下:

print(data1.shape)

注意1:我的数据是txt文件,如下:

import numpy as np
import pandas as pd
import os

def normalize(value, min_value, max_value, min_norm, max_norm):
    new_value = ((max_norm - min_norm)*((value - min_value)/(max_value - min_value))) + min_norm
    return new_value

#the size of matrices are (24,20)
df1 = np.zeros((24,20))
df2 = np.zeros((24,20))
df3 = np.zeros((24,20))


#next iteration create all plots, change the number of cycles
cycles = int(len(df)/480)
print(cycles)
for cycle in range(3):
    count =  '{:04}'.format(cycle)
    j = cycle * 480
    new_value1 = df['A'].iloc[j:j+480]
    new_value2 = df['B'].iloc[j:j+480]
    new_value3 = df['C'].iloc[j:j+480]
    df1 = print_df(mkdf(new_value1))
    df2 = print_df(mkdf(new_value2))
    df3 = print_df(mkdf(new_value3))              
    for i in df:
        try:
            os.mkdir(i)
        except:
            pass
        min_val = df[i].min()
        min_nor = -1
        max_val = df[i].max()
        max_nor = 1
        ordered_data = mkdf(df.iloc[j:j+480][i])
        csv = print_df(ordered_data)
        #Print .csv files contains matrix of each parameters by name of cycles respectively
        csv.to_csv(f'{i}/{i}{count}.csv', header=None, index=None)            
        if 'C' in i:
            min_nor = -40
            max_nor = 150
            #Applying normalization for C between [-40,+150]
            new_value3 = normalize(df['C'].iloc[j:j+480], min_val, max_val, -40, 150)
            C_norm = print_df(mkdf(new_value3))
            C_norm.to_csv(f'{i}/norm{i}{count}.csv', header=None, index=None)  
        else:
            #Applying normalization for A,B between    [-1,+1]
            new_value1 = normalize(df['A'].iloc[j:j+480], min_val, max_val, -1, 1)
            new_value2 = normalize(df['B'].iloc[j:j+480], min_val, max_val, -1, 1)
            A_norm = print_df(mkdf(new_value1))
            B_norm = print_df(mkdf(new_value2))
            A_norm.to_csv(f'{i}/norm{i}{count}.csv', header=None, index=None) 
            B_norm.to_csv(f'{i}/norm{i}{count}.csv', header=None, index=None)
    dff = pd.DataFrame({'A_norm':A_norm[cycle] , 'B_norm': B_norm[cycle] , 'C_norm': C_norm[cycle] } , index=[0])
    D = dff.as_matrix().ravel()
    if cycle == 0:
        Results = np.array(D)
    else:
        Results = np.vstack((Results, D))
    np.savetxt("Results.csv", Results , delimiter=',', encoding='utf-8')
#Check output shape whether is (3, 1440) or not 
data1 = np.loadtxt('Results.csv', delimiter=',')
print(data1.shape)  

注意2::我在文本文件中提供了3个周期的数据集: Text dataset

注3::为了以正确的顺序将A,B,C参数映射到矩阵中,我使用了id_set: 000 A: -2.46882615679 B: -2.26408246559 C: -325.004619528 print_df()函数,但由于将其简化为核心问题,并在本文开头保留一个最小的示例。让我知道您是否需要。

预期结果应通过在代表标准化版本的mkdf()'A_norm''B_norm'上完成 for循环来完成分别为'C_norm''A''B'的输出,我们将其称为“ Results.csv”应该是可逆的以重新生成'C',{{1 }},'A'遍历循环的矩阵再次将其保存在csv中。用于控制的文件,因此,如果您对反面有任何想法,请单独提及,否则只需使用'B'进行控制即可,它应为(3,1440)。 祝你有美好的一天,并提前致谢!

0 个答案:

没有答案