马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
现在有一个train1.csv的光谱数据,有855行和890列,现在需要将其转换为三维时间序列数据(855,89,10),将890列平均分成10份,每89列为一个时间维度,每一个时间维度都为其添加时间索引。将转换后的文件保存为train_time.csv
“发生异常: ValueError
Shape of passed values is (855, 890), indices imply (855, 10)
File "D:\0000可见光2\程序\MiniRocket\Test5.py", line 29, in <module>
reshaped_df = pd.DataFrame(reshaped_data_2d, columns=time_index.tolist())
ValueError: Shape of passed values is (855, 890), indices imply (855, 10)”
import pandas as pd
import numpy as np
# 读取CSV文件
data = pd.read_csv('train1.csv')
# 将数据转换为NumPy数组
data_array = data.to_numpy()
# 将数据重新排列成三维数组(855, 89, 10)
num_rows, num_cols = data_array.shape
num_time_dimensions = 10
num_columns_per_time_dimension = num_cols // num_time_dimensions
reshaped_data = np.zeros((num_rows, num_columns_per_time_dimension, num_time_dimensions))
for i in range(num_time_dimensions):
start_col = i * num_columns_per_time_dimension
end_col = (i + 1) * num_columns_per_time_dimension
reshaped_data[:, :, i] = data_array[:, start_col:end_col]
# 为每个时间维度添加时间索引
time_index = np.arange(1, num_time_dimensions + 1)
# 转换为2D数组
reshaped_data_2d = reshaped_data.reshape(num_rows, -1)
# 创建新的DataFrame并保存为train_time.csv
reshaped_df = pd.DataFrame(reshaped_data_2d, columns=time_index.tolist())
reshaped_df.to_csv('train_time.csv', index=False)
|