for i in predictions: if i ==1: 将下列的代码转换成用python写的 good[j] = abstract[i,:,:] j = j+1 else bad[k] = abstract[i,:,:] k = k+1
时间: 2023-02-25 15:56:18 浏览: 57
下面是代码的 Python 版本:
```
j = 0
k = 0
for i in predictions:
if i == 1:
good[j] = abstract[i, :, :]
j += 1
else:
bad[k] = abstract[i, :, :]
k += 1
```
相关问题
predictions = [] for i in range(len(val_dataset.columns)): if i == 0: predictions.append(np.mean(train_dataset[train_dataset.columns[-30:]].values, axis=1)) if i < 31 and i > 0: predictions.append(0.5 * (np.mean(train_dataset[train_dataset.columns[-30+i:]].values, axis=1) + \ np.mean(predictions[:i], axis=0))) if i > 31: predictions.append(np.mean([predictions[:i]], axis=1)) predictions = np.transpose(np.array([row.tolist() for row in predictions])) 作用分析
这段代码的作用是生成一组预测值(predictions)。
首先,通过循环遍历数据集的每一列,其中第一列(i==0)的预测值是训练集(train_dataset)中最后30列数据的平均值(np.mean(train_dataset[train_dataset.columns[-30:]].values, axis=1))。
对于其他列,i在1到31之间时,预测值是前i-1个预测值和训练集中后30+i列数据的平均值的平均值(0.5 * (np.mean(train_dataset[train_dataset.columns[-30+i:]].values, axis=1) + np.mean(predictions[:i], axis=0)))。
当i大于31时,预测值是前i-1个预测值的平均值的平均值(np.mean([predictions[:i]], axis=1))。
最后,将生成的所有预测值转置为矩阵(np.transpose)并返回。
matched_results = [predictions[i] for i in range(len(date_range)) if matching_dates[i].year == 2023]
这是一个Python代码片段,它使用列表推导式从一个名为 `predictions` 的列表中选出元素,并将它们存储在名为 `matched_results` 的新列表中。选择的条件是 `matching_dates` 列表中与每个索引对应的日期的年份等于 2023。具体来说,代码会遍历 `date_range` 列表的索引,检查 `matching_dates` 中对应索引的日期是否是 2023 年,如果是就将 `predictions` 中该索引位置的元素添加到 `matched_results` 中。最终,`matched_results` 中存储的是 `predictions` 中所有对应日期为 2023 年的元素。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)