删除 'iceemdan分解 逐步分解.ipynb'
This commit is contained in:
parent
584483d854
commit
cef7c8b538
|
@ -1,418 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from math import sqrt\n",
|
||||
"from numpy import concatenate\n",
|
||||
"from matplotlib import pyplot\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from sklearn.preprocessing import LabelEncoder\n",
|
||||
"from sklearn.metrics import mean_squared_error\n",
|
||||
"from tensorflow.keras import Sequential\n",
|
||||
"\n",
|
||||
"from tensorflow.keras.layers import Dense\n",
|
||||
"from tensorflow.keras.layers import LSTM\n",
|
||||
"from tensorflow.keras.layers import Dropout\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 加载数据\n",
|
||||
"path1 = r\"D:\\project\\小论文1-基于ICEEMDAN分解的时序高维变化的短期光伏功率预测模型\\CEEMAN-PosConv1dbiLSTM-LSTM\\模型代码流程\\data6.csv\"#数据所在路径\n",
|
||||
"#我的数据是excel表,若是csv文件用pandas的read_csv()函数替换即可。\n",
|
||||
"datas1 = pd.DataFrame(pd.read_csv(path1))\n",
|
||||
"#我只取了data表里的第3、23、16、17、18、19、20、21、27列,如果取全部列的话这一行可以去掉\n",
|
||||
"# data1 = datas1.iloc[:,np.r_[3,23,16:22,27]]\n",
|
||||
"data1=datas1.interpolate()\n",
|
||||
"values1 = data1.values\n",
|
||||
"print(data1.head())\n",
|
||||
"print(data1.shape)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"data1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from PyEMD import EMD\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"def ICEEMDAN(data, num_siftings=18, Nstd=0.2, NR=100):\n",
|
||||
" \"\"\"\n",
|
||||
" 改进的完全集合经验模态分解与自适应噪声(ICEEMDAN)。\n",
|
||||
" \"\"\"\n",
|
||||
" emd = EMD()\n",
|
||||
" T = len(data)\n",
|
||||
" std_data = np.std(data)\n",
|
||||
" \n",
|
||||
" # 通过添加白噪声获取数据的 IMFs 集合\n",
|
||||
" E_IMFs = np.zeros((NR, T, num_siftings))\n",
|
||||
" for r in range(NR):\n",
|
||||
" wn = np.random.normal(scale=std_data*Nstd, size=T)\n",
|
||||
" noisy_data = data + wn\n",
|
||||
" \n",
|
||||
" # 分解带噪声的数据\n",
|
||||
" imfs = emd.emd(noisy_data)\n",
|
||||
" \n",
|
||||
" # 仅保留前 `num_siftings` 个 IMFs\n",
|
||||
" for i in range(min(num_siftings, imfs.shape[0])):\n",
|
||||
" E_IMFs[r, :, i] = imfs[i]\n",
|
||||
" \n",
|
||||
" # 所有实验的 IMFs 平均值\n",
|
||||
" mean_IMFs = np.mean(E_IMFs, axis=0)\n",
|
||||
" \n",
|
||||
" return mean_IMFs\n",
|
||||
"\n",
|
||||
"# 假设您有一个名为 'data1' 的 DataFrame,其中包含一个 'Power' 列\n",
|
||||
"data_power = data1['Power'].values\n",
|
||||
"\n",
|
||||
"# 定义分解的数据窗口大小\n",
|
||||
"window_size = 104256\n",
|
||||
"\n",
|
||||
"# 遍历以 'window_size' 为单位的数据块\n",
|
||||
"for i in range(0, len(data_power), window_size):\n",
|
||||
" # 提取当前数据块\n",
|
||||
" current_data = data_power[i:i+window_size]\n",
|
||||
" \n",
|
||||
" # 对当前数据块应用 ICEEMDAN 分解\n",
|
||||
" IMFs = ICEEMDAN(current_data)\n",
|
||||
" \n",
|
||||
" # 创建一个新的 DataFrame 来保存 ICEEMDAN 分解结果\n",
|
||||
" data_ICEEMDAN = pd.DataFrame(IMFs, columns=[f'IMF{i}' for i in range(IMFs.shape[1])])\n",
|
||||
" \n",
|
||||
" # 计算 IMFs 的总和\n",
|
||||
" IMFs_sum = data_ICEEMDAN[[f'IMF{i}' for i in range(IMFs.shape[1])]].sum(axis=1)\n",
|
||||
" \n",
|
||||
" # 计算 IMFs 总和与原始信号之间的差值\n",
|
||||
" residue = current_data - IMFs_sum\n",
|
||||
" \n",
|
||||
" # 将残差添加到 DataFrame\n",
|
||||
" data_ICEEMDAN['Residue'] = residue\n",
|
||||
" \n",
|
||||
" # 打印当前数据块的分解结果\n",
|
||||
" print(f\"数据块 {i+1}-{i+len(current_data)} 的分解结果:\")\n",
|
||||
" print(data_ICEEMDAN)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 将分解后的 IMFs 与残差相加以恢复原始数据\n",
|
||||
"reconstructed_signal = IMFs_sum + residue\n",
|
||||
"\n",
|
||||
"# 绘制重构的数据与原始数据进行比较\n",
|
||||
"plt.figure(figsize=(30, 18))\n",
|
||||
"plt.plot(current_data[0:2900], label='Original Signal', color='blue')\n",
|
||||
"plt.plot(reconstructed_signal[0:2900], label='Reconstructed Signal', color='red')\n",
|
||||
"plt.xlabel('Time')\n",
|
||||
"plt.ylabel('Amplitude')\n",
|
||||
"plt.title('Comparison between Original and Reconstructed Signal')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.grid(True)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"# 设置全局字体大小\n",
|
||||
"plt.rcParams.update({'font.size': 60}) # 设置字体大小为 16\n",
|
||||
"\n",
|
||||
"# 在这里放置您的绘图代码\n",
|
||||
"num_imfs = IMFs.shape[1]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize=(64,128))\n",
|
||||
"\n",
|
||||
"# 绘制每个 IMF\n",
|
||||
"for i in range(num_imfs):\n",
|
||||
" if i <= 19: # 只绘制前 14 个 IMF\n",
|
||||
" plt.subplot(num_imfs, 1, i + 1)\n",
|
||||
" plt.plot(data_ICEEMDAN[f'IMF{i}'], label=f'IMF {i}')\n",
|
||||
" plt.legend()\n",
|
||||
"plt.legend()\n",
|
||||
"\n",
|
||||
"# 绘制残差\n",
|
||||
"plt.subplot(num_imfs + 1, 1, num_imfs + 1)\n",
|
||||
"plt.plot(data_ICEEMDAN[f'IMF{17}'], label='Residue', color='red')\n",
|
||||
"plt.legend()\n",
|
||||
"\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.show()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from scipy.signal import welch\n",
|
||||
"\n",
|
||||
"def plot_frequency_spectrum(signal, sampling_rate, title):\n",
|
||||
" \"\"\"\n",
|
||||
" 绘制信号的频谱图。\n",
|
||||
" \n",
|
||||
" 参数:\n",
|
||||
" - signal: 输入信号\n",
|
||||
" - sampling_rate: 信号的采样率\n",
|
||||
" - title: 图表标题\n",
|
||||
" \"\"\"\n",
|
||||
" f, Pxx = welch(signal, fs=sampling_rate, nperseg=len(signal))\n",
|
||||
" plt.figure(figsize=(10, 5))\n",
|
||||
" plt.semilogy(f, Pxx)\n",
|
||||
" plt.title(title)\n",
|
||||
" plt.xlabel('Frequency (Hz)')\n",
|
||||
" plt.ylabel('Power spectral density')\n",
|
||||
" plt.grid(True)\n",
|
||||
" plt.show()\n",
|
||||
"\n",
|
||||
"def compute_zero_crossings(signal):\n",
|
||||
" \"\"\"\n",
|
||||
" 计算信号的过零率。\n",
|
||||
" \"\"\"\n",
|
||||
" zero_crossings = np.where(np.diff(np.sign(signal)))[0]\n",
|
||||
" zero_crossing_rate = len(zero_crossings) / len(signal)\n",
|
||||
" return zero_crossing_rate\n",
|
||||
"\n",
|
||||
"def classify_frequency_component(imfs, threshold):\n",
|
||||
" \"\"\"\n",
|
||||
" 将每个 IMF 分为高频和低频成分。\n",
|
||||
" \"\"\"\n",
|
||||
" high_frequency_imfs = []\n",
|
||||
" low_frequency_imfs = []\n",
|
||||
" for i in range(imfs.shape[1]):\n",
|
||||
" imf = imfs[:, i]\n",
|
||||
" zero_crossing_rate = compute_zero_crossings(imf)\n",
|
||||
" if zero_crossing_rate > threshold:\n",
|
||||
" high_frequency_imfs.append(imf)\n",
|
||||
" else:\n",
|
||||
" low_frequency_imfs.append(imf)\n",
|
||||
" return high_frequency_imfs, low_frequency_imfs\n",
|
||||
"\n",
|
||||
"# 定义过零率的阈值\n",
|
||||
"threshold = 0.2 # 可根据具体情况调整阈值\n",
|
||||
"\n",
|
||||
"# 根据过零率判断高频和低频成分\n",
|
||||
"high_freq_imfs, low_freq_imfs = classify_frequency_component(IMFs, threshold)\n",
|
||||
"sampling_rate = 1000 \n",
|
||||
"# 可选:绘制高频和低频成分的频谱图\n",
|
||||
"for i, imf in enumerate(high_freq_imfs):\n",
|
||||
" plot_frequency_spectrum(imf, sampling_rate, f'High Frequency IMF {i+1}')\n",
|
||||
"\n",
|
||||
"for i, imf in enumerate(low_freq_imfs):\n",
|
||||
" plot_frequency_spectrum(imf, sampling_rate, f'Low Frequency IMF {i+1}')\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 打印高频和低频成分的 IMF 列表\n",
|
||||
"print(\"High Frequency IMFs:\")\n",
|
||||
"for i, imf in enumerate(high_freq_imfs):\n",
|
||||
" print(f\"IMF {i+1}: {imf}\")\n",
|
||||
"\n",
|
||||
"print(\"\\nLow Frequency IMFs:\")\n",
|
||||
"for i, imf in enumerate(low_freq_imfs):\n",
|
||||
" print(f\"IMF {i+1}: {imf}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# 将高频和低频信号相加\n",
|
||||
"high_freq_sum = np.sum(high_freq_imfs, axis=0)\n",
|
||||
"low_freq_sum = np.sum(low_freq_imfs, axis=0)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"high_freq_sum"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"low_freq_sum"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"residue"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"re_low=residue+low_freq_sum"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"re_high=residue+high_freq_sum\n",
|
||||
"re_high"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_low = pd.DataFrame(low_freq_sum, columns=['column_name'])\n",
|
||||
"df_low.to_csv('iceemdan_reconstructed_data_low.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_high = pd.DataFrame(high_freq_sum, columns=['column_name'])\n",
|
||||
"df_high.to_csv('iceemdan_reconstructed_data_high.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_high = pd.DataFrame(residue, columns=['column_name'])\n",
|
||||
"df_high.to_csv('iceemdan_reconstructed_data_residue.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_re_low = pd.DataFrame(re_low, columns=['column_name'])\n",
|
||||
"df_re_low.to_csv('iceemdan_reconstructed_data_re_low.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_re_high = pd.DataFrame(re_high, columns=['column_name'])\n",
|
||||
"df_re_high.to_csv('iceemdan_reconstructed_data_re_high.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMFs_sum"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 指定文件路径和文件名,保存DataFrame到CSV文件中\n",
|
||||
"df_high = pd.DataFrame(IMFs_sum, columns=['column_name'])\n",
|
||||
"df_high.to_csv('iceemdan_reconstructed_data_IMFs_sum.csv', index=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
Loading…
Reference in New Issue