data00=data m,n = np.shape(data00) a = np.array(data00) Data00 = a[1:m,2:n] Data00 = Data00.astype(np.float64) Power = Data00[:,13] Power_train = Power[0:96] P_min = np.min(Power_train) P_gap = np.max(Power_train)-np.min(Power_train) Power_uni = (Power-P_min)/P_gap # 提取imfs和剩余信号res emd = EMD() emd.emd(Power_uni) imfs, res = emd.get_imfs_and_residue() N = len(imfs) P_H = np.sum(imfs[0:6,:],axis=0) P_M = np.sum(imfs[6:12,:],axis=0) P_L = res P_H =np.expand_dims(P_H,axis=1) P_M =np.expand_dims(P_M,axis=1) P_L =np.expand_dims(P_L,axis=1) Nwp = Data00[:,0:7] Nwp_train = Nwp[0:96] N_min = np.min(Nwp_train,axis=0) N_gap = np.max(Nwp_train,axis=0)-np.min(Nwp_train,axis=0) Nwp_uni = (Nwp-N_min)/N_gap#(N,7) Weather = Data00[:,7:13] Weather_train = Weather[0:96] W_min = np.min(Weather_train,axis=0) W_gap = np.max(Weather_train,axis=0)-np.min(Weather_train,axis=0) Weather_uni = (Weather-W_min)/W_gap#(N,6) 优化代码
时间: 2024-02-24 11:53:21 浏览: 93
系统更新-在keras中利用np.random.shuffle()打乱数据集实例
以下是部分代码的优化建议:
1. 对于以下代码段:
```
a = np.array(data00)
Data00 = a[1:m,2:n]
```
可以合并为一行:
```
Data00 = np.array(data00)[1:m,2:n]
```
2. 对于以下代码段:
```
P_H = np.sum(imfs[0:6,:],axis=0)
P_M = np.sum(imfs[6:12,:],axis=0)
P_L = res
P_H =np.expand_dims(P_H,axis=1)
P_M =np.expand_dims(P_M,axis=1)
P_L =np.expand_dims(P_L,axis=1)
```
可以使用 `np.newaxis` 替代 `np.expand_dims` 来实现:
```
P_H = np.sum(imfs[0:6,:],axis=0)[:, np.newaxis]
P_M = np.sum(imfs[6:12,:],axis=0)[:, np.newaxis]
P_L = res[:, np.newaxis]
```
3. 对于以下代码段:
```
N_min = np.min(Nwp_train,axis=0)
N_gap = np.max(Nwp_train,axis=0)-np.min(Nwp_train,axis=0)
Nwp_uni = (Nwp-N_min)/N_gap#(N,7)
```
可以使用 `np.ptp` 函数(peak-to-peak)来计算最大值和最小值的差:
```
N_min = np.min(Nwp_train,axis=0)
N_gap = np.ptp(Nwp_train,axis=0)
Nwp_uni = (Nwp-N_min)/N_gap#(N,7)
```
4. 对于以下代码段:
```
Weather = Data00[:,7:13]
Weather_train = Weather[0:96]
W_min = np.min(Weather_train,axis=0)
W_gap = np.max(Weather_train,axis=0)-np.min(Weather_train,axis=0)
Weather_uni = (Weather-W_min)/W_gap#(N,6)
```
可以使用与第三个优化建议类似的方法:
```
Weather = Data00[:,7:13]
Weather_train = Weather[0:96]
W_min = np.min(Weather_train,axis=0)
W_gap = np.ptp(Weather_train,axis=0)
Weather_uni = (Weather-W_min)/W_gap#(N,6)
```
当然,以上只是一些简单的优化建议,具体的优化效果还需要根据实际情况进行评估。
阅读全文