for j in range(len(one_sample.iloc[i])): if one_m.iloc[i - 1][j] == 1: one_deltaPre.iloc[i][j] = 1.0 elif one_m.iloc[i - 1][j] == 0: one_deltaPre.iloc[i][j] = one_deltaPre.iloc[i - 1][j] + 1.0 if one_m.iloc[i][j] == 1: one_lastvalues.iloc[i][j] = one_sample.iloc[i][j] elif one_m.iloc[i][j] == 0:
时间: 2024-04-26 11:26:27 浏览: 16
这段代码主要是用来处理缺失值的情况,其中one_sample表示一个样本,one_m表示缺失值的mask,one_lastvalues表示填充后的样本,one_deltaPre表示填充前的差值。具体来说,对于每一列的缺失值,如果前一个值存在,则用前一个值替代缺失值;如果前一个值也是缺失值,则用one_deltaPre来记录此时相对于第一个非缺失值的差值;如果当前值不是缺失值,则将其直接赋给one_lastvalues,表示填充后的样本。
相关问题
优化函数,使用GPU加速 def get_index(df, sendtime, idx_initial, temp_upper, temp_low, soc_low, soc_upper, sample_interval, time_interval, curr_interval, min_curr, soc_interval, data_point): index = [0] for i in idx_initial: n = 0 k = i while (n <= data_point) & (i <= len(df) - 100): idx_list = [] idx_list.append(i) for j in np.arange(i + 1, len(df)): if ((sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() >= time_interval): break elif (df['max_temp'].iloc[j] <= temp_upper) & (df['min_temp'].iloc[j] >= temp_low) & \ (df['bat_module_soc_00'].iloc[j] >= soc_low) & (df['bat_module_soc_00'].iloc[j] <= soc_upper) & \ ((sendtime[j] - sendtime[i]).total_seconds() >= sample_interval) & \ ((sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() <= time_interval) & \ (np.abs(total_current[j] - total_current[i]) >= curr_interval) & ( np.abs(soc[j] - soc[i]) <= soc_interval) & (np.abs(total_current[j]) >= min_curr): n += 1 idx_list.append(j) i = j if ((sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() >= time_interval): break if len(idx_list) >= data_point: print(idx_list) index.append(idx_list) return index
为了使用GPU加速,在Python中,您可以使用Numba库来将函数编译为CUDA内核。以下是优化后的代码:
```
from numba import jit, cuda
import numpy as np
@jit(target ="cuda")
def get_index(df, sendtime, idx_initial, temp_upper, temp_low, soc_low, soc_upper, sample_interval, time_interval, curr_interval, min_curr, soc_interval, data_point):
index = [0]
total_current = df['total_current'].values
soc = df['bat_module_soc_00'].values
for i in idx_initial:
n = 0
k = i
while (n <= data_point) & (i <= len(df) - 100):
idx_list = []
idx_list.append(i)
for j in np.arange(i + 1, len(df)):
if ((sendtime[j] - sendtime[k]).total_seconds() >= time_interval):
break
elif (df['max_temp'].iloc[j] <= temp_upper) & (df['min_temp'].iloc[j] >= temp_low) & \
(soc[j] >= soc_low) & (soc[j] <= soc_upper) & ((sendtime[j] - sendtime[i]).total_seconds() >= sample_interval) & \
((sendtime[j] - sendtime[k]).total_seconds() <= time_interval) & (np.abs(total_current[j] - total_current[i]) >= curr_interval) & \
(np.abs(soc[j] - soc[i]) <= soc_interval) & (np.abs(total_current[j]) >= min_curr):
n += 1
idx_list.append(j)
i = j
if ((sendtime[j] - sendtime[k]).total_seconds() >= time_interval):
break
if len(idx_list) >= data_point:
print(idx_list)
index.append(idx_list)
return index
```
请注意,Numba的CUDA目标需要将函数中的所有数据转换为Numpy数组或CUDA设备数组。因此,我们将`total_current`和`soc`列转换为NumPy数组。此外,我们还使用`target ="cuda"`将函数编译为CUDA内核。这将使Numba利用GPU加速函数。
优化代码 data = {'realtime':[], 'cell_volt':[], 'total_current':[]} index = [] # (total_current[j]<=0) for i in tqdm(df.index[temp_condtion(df, temp_upper, temp_low) & soc_condtion(df, soc_upper, soc_low) & current_condtion(df, min_curr, 'discharge')]): n = 0 k = i while (n <= data_point) & (i <= len(df)-100): idx_list = [] idx_list.append(i) for j in np.arange(i+1, len(df)): if ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()>=time_interval): break elif (df['max_temp'].iloc[j]<=temp_upper) & (df['min_temp'].iloc[j]>=temp_low) & \ (df['bat_module_soc_00'].iloc[j]>=soc_low) & (df['bat_module_soc_00'].iloc[j]<=soc_upper) & \ ((sendtime[j]-sendtime[i]).total_seconds()>=sample_interval) & \ ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()<=time_interval) & \ (np.abs(total_current[j]-total_current[i])>=curr_interval) & (np.abs(soc[j]-soc[i])<=soc_interval) & \ (np.abs(total_current[j])>=min_curr): n+=1 idx_list.append(j) i = j if ((sendtime.iloc[j]-sendtime.iloc[k]).total_seconds()>=time_interval): break if len(idx_list) >= data_point: print(idx_list) index.append(idx_list)
可以试着优化的地方包括:
1. 避免多次重复计算相同的值,例如可以将 df['max_temp'], df['min_temp'], df['bat_module_soc_00'], total_current 和 soc 的值先提取出来,避免在循环中多次计算;
2. 可以将一些判断条件提前到循环外面,避免在循环中多次判断,例如可以将 soc 和 total_current 的取值范围提前判断;
3. 可以使用 numba 库来加速代码,将循环部分的代码使用 @jit 装饰器修饰,这样可以使用 JIT 编译来加速循环部分的代码。
以下是可能的优化代码:
```python
# 提前计算需要用到的数据
max_temp = df['max_temp']
min_temp = df['min_temp']
soc = df['bat_module_soc_00']
total_current = df['total_current']
sendtime = df['sendtime']
# 判断条件
total_current_cond = (total_current <= 0) & (np.abs(total_current) >= min_curr)
soc_cond = (soc >= soc_low) & (soc <= soc_upper) & (np.abs(soc - soc[i]) <= soc_interval)
temp_cond = (max_temp <= temp_upper) & (min_temp >= temp_low)
index = []
for i in tqdm(df.index[temp_cond & soc_cond & total_current_cond]):
n = 0
k = i
idx_list = [i]
for j in np.arange(i + 1, len(df)):
if (sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() >= time_interval:
break
elif temp_cond[j] & soc_cond[j] & (sendtime.iloc[j] - sendtime.iloc[i]).total_seconds() >= sample_interval \
& (sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() <= time_interval \
& np.abs(total_current[j] - total_current[i]) >= curr_interval \
& total_current_cond[j]:
n += 1
idx_list.append(j)
i = j
if (sendtime.iloc[j] - sendtime.iloc[k]).total_seconds() >= time_interval:
break
if len(idx_list) >= data_point:
index.append(idx_list)
```
注意,这只是一个可能的优化方案,具体的优化效果需要根据数据量和实际情况来评估。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)