鱼C论坛

 找回密码
 立即注册
查看: 1394|回复: 1

数据转换

[复制链接]
发表于 2023-10-24 16:05:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-24 16:05:57 | 显示全部楼层
问题的原因在于reshaped_data_2d的形状与time_index的形状不匹配。根据错误提示,传递的值的形状为(855, 890),而索引暗示的形状为(855, 10)。

解决这个问题的方法是使用reshape函数将reshaped_data转换为2D数组时,指定正确的形状。在这种情况下,我们需要将reshaped_data从形状(855, 89, 10)转换为形状(855, 890)。

以下是修改后的代码:
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_columns_per_time_dimension + 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)

这样修改后的代码应该能够正确地将数据转换为三维时间序列数据,并保存为train_time.csv文件。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 13:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表