马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
现在有一个光谱数据,存放在train1.csv中,有855行和890列,每行是一个样本,每列是一个光谱波段的反射率值,现在要求python把光谱数据转化为时间序列数据并保存在新文件中,转化为855行和10个时间维度,每一个时间维度有89个时间步长,将数据转化成一个包含索引和值的Pandas Series 格式
“发生异常: ValueError
Length of values (760950) does not match length of index (8550)
File "D:\0000可见光2\程序\MiniRocket\Test4.py", line 29, in <module>
series = pd.Series(series_data.flatten(), index=index, name='Reflectance')
ValueError: Length of values (760950) does not match length of index (8550)”
import pandas as pd
import numpy as np
# 读取光谱数据
data = pd.read_csv('train1.csv', header=None)
# 定义时间维度和时间步长
num_samples, num_bands = data.shape
num_time_dimensions = 10
num_time_steps = num_bands // num_time_dimensions
# 将数据重新排列为时间序列格式
time_series_data = np.zeros((num_samples, num_time_dimensions, num_time_steps))
for sample_idx in range(num_samples):
for time_dim in range(num_time_dimensions):
start_band = time_dim * num_time_steps
end_band = (time_dim + 1) * num_time_steps
time_series_data[sample_idx, time_dim, :] = data.iloc[sample_idx, start_band:end_band]
# 创建一个Pandas Series
series_data = time_series_data.reshape(num_samples, -1)
# 创建索引
index = [(sample, time_dim) for sample in range(num_samples) for time_dim in range(num_time_dimensions)]
index = pd.MultiIndex.from_tuples(index, names=['Sample', 'Time Dimension'])
# 创建Pandas Series
series = pd.Series(series_data.flatten(), index=index, name='Reflectance')
# 保存转换后的数据为新的CSV文件
series.to_csv('time_series_data.csv', header=True)
print("转换并保存成功")
|