diff --git a/examples/RPCA.md b/examples/RPCA.md index 05f8b75..0a4fbe8 100644 --- a/examples/RPCA.md +++ b/examples/RPCA.md @@ -34,6 +34,10 @@ from qolmat.imputations.rpca import rpca_utils from qolmat.utils.data import generate_artificial_ts ``` +```python +from qolmat.imputations.imputers import ImputerRpcaNoisy, ImputerRpcaPcp +``` + **Generate synthetic data** ```python tags=[] @@ -46,16 +50,33 @@ amp_noise = 0.1 X_true, A_true, E_true = generate_artificial_ts(n_samples, periods, amp_anomalies, ratio_anomalies, amp_noise) signal = X_true + A_true + E_true +signal = 10 + signal * 40 # Adding missing data signal[120:180] = np.nan signal[:20] = np.nan +for i in range(10): + signal[i::365] = np.nan # signal[80:220] = np.nan # mask = np.random.choice(len(signal), round(len(signal) / 20)) # signal[mask] = np.nan ``` +```python +import pandas as pd +df = pd.DataFrame({"signal": signal}) +irn = ImputerRpcaPcp(period=100) +df_imp = irn.fit_transform(df) +``` + +```python +plt.plot(df_imp["signal"]) +plt.plot(df["signal"]) + +plt.xlim(0, 200) +``` + ```python tags=[] fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(4, 1, 1) diff --git a/qolmat/imputations/imputers.py b/qolmat/imputations/imputers.py index a255070..1eccabd 100644 --- a/qolmat/imputations/imputers.py +++ b/qolmat/imputations/imputers.py @@ -1515,13 +1515,10 @@ def _transform_element( D_scale = (D - means) / stds M, A = model.decompose(D_scale, Omega) M = M * stds + means - A = A * stds + means M_final = utils.get_shape_original(M, X.shape) - A_final = utils.get_shape_original(A, X.shape) - X_imputed = M_final + A_final - df_imputed = pd.DataFrame(X_imputed, index=df.index, columns=df.columns) + df_imputed = pd.DataFrame(M_final, index=df.index, columns=df.columns) df_imputed = df.where(~df.isna(), df_imputed) return df_imputed @@ -1705,7 +1702,6 @@ def _transform_element( D_scale = (D - means) / stds M, A = model.decompose_on_basis(D_scale, Omega, Q) M = M * stds + means - A = A * stds + means M_final = utils.get_shape_original(M, X.shape)