You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改create_dataset函数实现输入与输出的不同时间步长数据集划分

Got it, I've modified the create_dataset function to use separate timesteps for features and target, using loops and empty lists as requested. Here's the solution:

Modified create_dataset Function with Separate Timesteps
import numpy as np

def create_dataset(voltage, current, temp, ah, timestep1, timestep2, max_capacity, features, timesteps):
    # Sample input features with timestep1 using loops
    voltage_sampled = []
    for i in range(0, len(voltage), timestep1):
        voltage_sampled.append(voltage[i])
    
    current_sampled = []
    for i in range(0, len(current), timestep1):
        current_sampled.append(current[i])
    
    temp_sampled = []
    for i in range(0, len(temp), timestep1):
        temp_sampled.append(temp[i])
    
    # Sample target variable with timestep2 using loops
    ah_sampled = []
    for i in range(0, len(ah), timestep2):
        ah_sampled.append(ah[i])
    
    # Align the length of sampled features and target to avoid mismatches
    num_samples = min(len(voltage_sampled), len(ah_sampled))
    voltage_sampled = voltage_sampled[:num_samples]
    current_sampled = current_sampled[:num_samples]
    temp_sampled = temp_sampled[:num_samples]
    ah_sampled = ah_sampled[:num_samples]
    
    # Build combined data list and normalize the target (ah)
    data = []
    for i in range(num_samples):
        data.append(current_sampled[i])
        data.append(voltage_sampled[i])
        data.append(temp_sampled[i])
        data.append((ah_sampled[i] + max_capacity) / max_capacity)
    
    # Reshape data into structured format and truncate to fit sequence timesteps
    data = np.array(data).reshape(num_samples, features + 1)
    truncated_samples = (num_samples // timesteps) * timesteps
    data = data[:truncated_samples]
    
    # Split into feature matrix (X) and target vector (Y)
    X = data[:, :features]
    Y = data[:, features:]
    
    return X, Y

Key Changes Explained:

  • Separate Timestep Sampling: We use loops and empty lists to sample features (voltage, current, temp) with timestep1 and the target (ah) with timestep2, replacing the original uniform slicing. This gives you granular control over how each signal is downsampled.
  • Length Alignment: Different timesteps will produce sampled arrays of varying lengths, so we truncate all arrays to the shortest length to ensure every feature sample has a corresponding target sample.
  • Sequence Compatibility: We retain the truncation step to ensure the number of samples is divisible by timesteps, which is critical if you're using sequence-based models like RNNs or LSTMs later in your pipeline.
  • Efficient Splitting: Instead of looping to split X and Y, we use numpy array slicing for speed and readability, while still honoring your request to use loops for the sampling phase.

This approach should help balance training speed (by reducing redundant feature samples) and accuracy (by retaining more target detail if timestep2 is smaller than timestep1), depending on your specific battery modeling use case.

内容的提问来源于stack exchange,提问作者jolly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 23:44:08