我想重塑我的 24x20 矩阵'A'
,这些矩阵是从文本文件中提取的'B'
,并在通过循环'C'
进行规范化之前和之后保存,def normalize()
这样每个循环将是一行,其中 3 个矩阵的所有元素并排,如下所示:
[[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
到目前为止,根据@odyse 的建议,我在 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=",")
但是当我def normalize()
在for循环中使用after时出现问题,尽管它有错误(ValueError)它也有warning FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead
它D = dff.as_matrix().ravel()
并不重要但现在因为它是FutureWarning但是我通过使用检查了3个周期的输出形状是否正确print(data1.shape)
它是(3, 1440),它是 3 行作为 3 个周期,列数应该是 3 倍 480= 1440 但总之不是稳定的解决方案。
完整的脚本如下:
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)
注1:我的数据是txt文件如下:
id_set: 000
A: -2.46882615679
B: -2.26408246559
C: -325.004619528
注2: 我在文本文件中提供了一个数据集 3 个周期: 文本数据集
注意 3:为了将 A、B、C 参数按正确顺序映射到矩阵中,我使用了print_df()
mkdf()
函数,但由于将其简化为核心问题,因此我没有提及,仅在本文开头留下一个最小示例。如果您需要,请告诉我。
预期结果应该通过完成for 循环来完成'A_norm'
,它们分别表示'B_norm'
,'C_norm'
的标准化版本'A'
,输出让我们称之为“Results.csv”应该是可逆的,可以通过循环重新生成, ,矩阵再次将它们保存在 csv 中。用于控制的文件,因此如果您对反向部分有任何想法,请单独提及,否则只需通过使用来控制它,它应该是(3, 1440)。祝你有美好的一天,提前感谢!'B'
'C'
'A'
'B'
'C'
print(data.shape)