我想重塑我的24x20矩阵'A'
,'B'
,'C'
,它们是从文本文件中提取出来的,在for循环中通过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=",")
但是当我在for循环中使用after def normalize()
时有一个问题,尽管它有错误(ValueError),它也有warning FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead
for D = dff.as_matrix().ravel()
,这并不重要,但是现在,因为它是未来警告,尽管如此,我通过使用print(data1.shape)
检查了输出的形状在3个周期内是正确的,它是(31440),这是3行作为3个周期列数应为480=1440的3倍,但总体上不稳定。你知道吗
完整的脚本如下:
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个周期的数据集: Text dataset
注3:为了将A,B,C参数按正确的顺序映射到矩阵中,我使用了print_df()
mkdf()
函数,但我没有提及,因为我把它简化为核心问题,只在本文开头留下了一个最小的例子。如果你需要的话请告诉我。你知道吗
预期结果应该通过分别在'A_norm'
、'B_norm'
、'C_norm'
上完成for循环来完成,这些循环表示'A'
、'B'
、'C'
的规范化版本,并输出我们称之为结果.csv“应该是可逆的以再生'A'
,'B'
,'C'
循环矩阵再次保存在csv中。因此,如果您对反向部分有任何想法,请单独提及,否则只需使用print(data.shape)
控制它,它应该是(31440)。
祝你今天愉快,提前谢谢!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐