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

将20个特征转为numpy数组时出现形状不均错误求助

问题描述

整合20个特征生成x矩阵时触发形状不均错误,此前使用13个特征执行同类操作无异常。PyCharm报错信息如下:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2023.2.5\plugins\python\helpers\pydev\pydevd.py", line 1500, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2023.2.5\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:\Users\Crystal\PycharmProjects\pythonProject\FeaturesBuilding.py", line 159, in
x_vec1 = np.asarray(x_vec)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (124, 20) + inhomogeneous part.

相关代码片段:

## Other features
avg_chargetime_first5cycles = np.mean(battery_cycle['summary']['chargetime'][0:5])  # feature 14 - Avg chargetime (1-5)
MaxTemp_cycles2_100 = battery_cycle['summary']['Tmax'][1:100]  # feature 15 - Maximum Temperature, cycles 2 to 100: [1:100]
MinTemp_cycles2_100 = battery_cycle['summary']['Tmin'][1:100]  # feature 16 - Minimum Temperature, cycles 2 to 100: [1:100]
Intg_T_t_cycles2_100 = np.trapz(battery_cycle['summary']['Tavg'][1:100])  # feature 17 - integral of temperature over time, cycles 2 to 100, the time means the cycle numbers
intresis_2 = battery_cycle['summary']['IR'][1]  # feature 18 - Internal resistance at cycle 2
MinIntResis_cycles2_100 = np.min(battery_cycle['summary']['IR'][1:100])  # feature 19 - minimum internal resistance cycles 2 to 100
ir_100minus2 = battery_cycle['summary']['IR'][99] - battery_cycle['summary']['IR'][1]  # feature 20 - IR 100- IR 2

x_vec.append([logmin_deltaQ, logmean_deltaQ, logvar_deltaQ, logskew_deltaQ, logkurt_deltaQ, log2V_deltaQ, LR_slope_cycles2_100, LR_interc_cycles2_100, LR_slope_cycles91_100, LR_interc_cycles91_100, Qd_at_cycle2, maxQdminusQdatcycle2, Qd_at_cycle100, avg_chargetime_first5cycles, MaxTemp_cycles2_100, MinTemp_cycles2_100, Intg_T_t_cycles2_100, intresis_2, MinIntResis_cycles2_100, ir_100minus2]) # for feature_set3
y_vec.append(bat_dict[tag]['cycle_life'])

x_vec1 = np.asarray(x_vec)
y_vec1 = np.asarray(y_vec)[:,0,:]
feature_number = 20
plt.scatter(x_vec1[:,feature_number-1],y_vec1)
plt.show()

已排查新增的7个特征,确认均为数值型,但仍无法理解错误原因。


问题分析与解决
  • 错误根源:新增的MaxTemp_cycles2_100和MinTemp_cycles2_100并非单个数值,而是长度为99的数组(直接取了[1:100]的切片),而其他特征都是标量值。当把数组和标量混合放入列表后,转换为numpy数组时无法形成规整的二维矩阵,触发形状不均的错误。

  • 修正方案:根据代码注释,这两个特征应该是周期2到100内的最大/最小温度值,需要对切片结果做统计计算:

    # 修正feature 15:计算cycles2-100的最大温度
    MaxTemp_cycles2_100 = np.max(battery_cycle['summary']['Tmax'][1:100])
    # 修正feature 16:计算cycles2-100的最小温度
    MinTemp_cycles2_100 = np.min(battery_cycle['summary']['Tmin'][1:100])
    
  • 验证逻辑:修改后,所有20个特征均为单个标量,np.asarray(x_vec)会生成形状为(124,20)的规整二维矩阵,错误即可消除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 05:26:01