import pandas as pd import numpy as np from scipy.optimize import minimize # 加载数据 data = pd.read_csv('E:/lunwenpython/m

动态 未结 0 87
小小草
小小草 LV1 2023年9月23日 23:40 发表
这个错误和之前的错误类似,是由于 `composite_index` 和 `weights` 的长度不一致导致的。在第 41 行代码中,`np.exp(-0.5 * t) * composite_index` 返回的是一个长度为 248576 的数组,而 `weights` 的长度为 248575。因此在执行 `np.dot(weights, W)` 时,两个数组无法进行广播(broadcasting)操作。 解决方法同样是将 `weights` 的长度扩展到和 `composite_index` 相同,可以使用 `np.repeat()` 和 `np.concatenate()` 函数来实现。具体做法是先将 `weights` 中的元素重复若干次,使得其长度等于 `composite_index` 的长度,然后再将多余的部分填充为 0。修改代码如下: ``` weights = np.repeat(weights, len(composite_index) // len(weights)) weights = np.concatenate([weights, np.zeros(len(composite_index) % len(weights)))].astype(float) Z = np.exp(-0.5 * t) * composite_index + np.dot(weights, W) ``` 这样就可以避免上述错误了。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复