优化这段代码for i in range(14): x_train_YS, y_train_YS = data.iloc[0:418+20*i,list(range(0))+list(range(8,95))],data.iloc[0:418+20*i,95] x_test_YS, y_test_YS = data.iloc[418+20*i:438+20*i,list(range(0))+list(range(8,95))],data.iloc[418+20*i:438+20*i,95] x_train_YS_1,y_train_YS_1 = x_train_YS.iloc[1:418+20*i,1:87].values,y_train_YS.iloc[1:418+20*i,87].values x_test_YS_1,y_test_YS_1 = x_test_YS.iloc[418+20*i:438+20*i,1:87].values,y_test_YS.iloc[418+20*i:438+20*i,87].values
时间: 2024-02-24 21:53:21 浏览: 66
可以对这段代码进行如下优化:
```
start = 0
end = 418
cols = list(range(0)) + list(range(8, 95))
cols_1 = list(range(1, 87))
train_data = []
test_data = []
for i in range(14):
x_train_YS, y_train_YS = data.iloc[start:end, cols], data.iloc[start:end, 95]
x_test_YS, y_test_YS = data.iloc[start+418:end+438, cols], data.iloc[start+418:end+438, 95]
x_train_YS_1, y_train_YS_1 = x_train_YS.iloc[1:end-1, cols_1].values, y_train_YS.iloc[1:end-1].values
x_test_YS_1, y_test_YS_1 = x_test_YS.iloc[:, cols_1].values, y_test_YS.iloc[:, -1].values
train_data.append((x_train_YS_1, y_train_YS_1))
test_data.append((x_test_YS_1, y_test_YS_1))
start = end
end += 20 * (i + 2)
```
这样,我们可以将数据处理部分的代码放入循环中,避免了重复代码,同时将结果存储在列表中,便于后续处理。
阅读全文