优化这段代码 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 07:53:21 浏览: 61
可以对这段代码进行如下优化:
```
start = 0
end = 418 + 20 * i
cols = list(range(0)) + list(range(8, 95))
x_train_YS = data.iloc[start:end, cols]
y_train_YS = data.iloc[start:end, 95]
start = 418 + 20 * i
end = 438 + 20 * i
x_test_YS = data.iloc[start:end, cols]
y_test_YS = data.iloc[start:end, 95]
cols_1 = list(range(1, 87))
x_train_YS_1 = x_train_YS.iloc[1:end-1, cols_1].values
y_train_YS_1 = y_train_YS.iloc[1:end-1].values
x_test_YS_1 = x_test_YS.iloc[:, cols_1].values
y_test_YS_1 = y_test_YS.iloc[:, -1].values
```
这样,我们可以避免重复代码,减少重复计算,提高代码的可读性和可维护性。同时,将计算结果存储在变量中,可以提高代码的执行效率。
相关问题
优化这段代码 x_train_YS, y_train_YS = data.iloc[0:418+20*i,cols],data.iloc[0:418+20*i,95] x_test_YS, y_test_YS = data.iloc[418+20*i:438+20*i,cols],data.iloc[418+20*i:438+20*i,95] print(x_train_YS) x_train_YS_1,y_train_YS_1 = x_train_YS.values[0:418+20*i,1:85],y_train_YS.values[0:418+20*i,85]
可以将代码进行优化,避免多次索引和切片,可以使用数据切分函数train_test_split()来代替手动切分数据。
具体实现如下:
1. 首先需要导入train_test_split函数:from sklearn.model_selection import train_test_split
2. 对数据进行切分:
```
X = data.iloc[:, cols]
y = data.iloc[:, 95]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05, random_state=42)
```
其中,test_size表示测试数据占比,random_state为随机种子,保证每次运行结果一致。
3. 对训练数据进行切分:
```
X_train_YS = X_train.iloc[:418+20*i, 1:85]
y_train_YS = y_train.iloc[:418+20*i]
```
4. 对测试数据进行切分:
```
X_test_YS = X_test.iloc[418+20*i:438+20*i, 1:85]
y_test_YS = y_test.iloc[418+20*i:438+20*i]
```
这样就可以得到和原代码相同的训练数据和测试数据了。
优化这段代码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
可以对这段代码进行如下优化:
```
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)
```
这样,我们可以将数据处理部分的代码放入循环中,避免了重复代码,同时将结果存储在列表中,便于后续处理。
阅读全文