@@ -482,118 +482,9 @@ def fit_transform_element(self, df: pd.DataFrame) -> pd.DataFrame:
482482 )
483483 results = imputer .fit_transform (df )
484484 return pd .DataFrame (data = results , columns = df .columns , index = df .index )
485-
486-
487- class ImputerRPCA (Imputer ):
488- """
489- This class implements the RPCA imputation
490-
491- Parameters
492- ----------
493- method : str
494- name of the RPCA method:
495- "PCP" for basic RPCA
496- "temporal" for temporal RPCA, with regularisations
497- "online" for online RPCA
498- columnwise : bool
499- for RPCA method to be applied columnwise (with reshaping of each column into an array)
500- or to be applied directly on the dataframe. By default, the value is set to False.
501- """
502-
503- def __init__ (
504- self ,
505- method : str = "temporal" ,
506- groups : List [str ] = [],
507- columnwise : bool = False ,
508- ** hyperparams
509- ) -> None :
510- super ().__init__ (groups = groups , columnwise = columnwise , hyperparams = hyperparams )
511-
512- self .method = method
513-
514- def fit_transform_element (self , df : pd .DataFrame ) -> pd .DataFrame :
515- """
516- Fit/transform to impute with RPCA methods
517-
518- Parameters
519- ----------
520- df : pd.DataFrame
521- dataframe to impute
522-
523- Returns
524- -------
525- pd.DataFrame
526- imputed dataframe
527- """
528- if not isinstance (df , pd .DataFrame ):
529- raise ValueError ("Input has to be a pandas.DataFrame." )
530-
531- if self .method == "PCP" :
532- rpca = RPCA (** self .hyperparams_element )
533- elif self .method == "temporal" :
534- rpca = TemporalRPCA (** self .hyperparams_element )
535- elif self .method == "onlinetemporal" :
536- rpca = OnlineTemporalRPCA (** self .hyperparams_element )
537-
538- df_imputed = pd .DataFrame (rpca .fit_transform (X = df .values ), index = df .index , columns = df .columns )
539-
540- return df_imputed
541-
542485
543- class ImputeEM (_BaseImputer ):
544- def __init__ (
545- self ,
546- strategy : Optional [str ] = "mle" ,
547- method : Optional [str ] = "multinormal" ,
548- max_iter_em : Optional [int ] = 200 ,
549- n_iter_ou : Optional [int ] = 50 ,
550- ampli : Optional [int ] = 1 ,
551- random_state : Optional [int ] = 123 ,
552- dt : Optional [float ] = 2e-2 ,
553- tolerance : Optional [float ] = 1e-4 ,
554- stagnation_threshold : Optional [float ] = 5e-3 ,
555- stagnation_loglik : Optional [float ] = 2 ,
556- ):
557- if method == "multinormal" :
558- self .model = em_sampler .ImputeMultiNormalEM (
559- strategy = strategy ,
560- max_iter_em = max_iter_em ,
561- n_iter_ou = n_iter_ou ,
562- ampli = ampli ,
563- random_state = random_state ,
564- dt = dt ,
565- tolerance = tolerance ,
566- stagnation_threshold = stagnation_threshold ,
567- stagnation_loglik = stagnation_loglik ,
568- )
569- elif method == "VAR1" :
570- self .model = em_sampler .ImputeVAR1EM (
571- strategy = strategy ,
572- max_iter_em = max_iter_em ,
573- n_iter_ou = n_iter_ou ,
574- ampli = ampli ,
575- random_state = random_state ,
576- dt = dt ,
577- tolerance = tolerance ,
578- stagnation_threshold = stagnation_threshold ,
579- stagnation_loglik = stagnation_loglik ,
580- )
581- else :
582- raise ValueError ("Strategy '{strategy}' is not handled by ImputeEM!" )
583-
584- def fit (self , df ):
585- X = df .values
586- self .model .fit (X )
587- return self
588486
589- def transform (self , df ):
590- X = df .values
591- X_transformed = self .model .transform (X )
592- df_transformed = pd .DataFrame (X_transformed , columns = df .columns , index = df .index )
593- return df_transformed
594-
595-
596- class ImputeMICE (Imputer ):
487+ class ImputerMICE (Imputer ):
597488 """
598489 This class implements an iterative imputer in the multivariate case.
599490 It imputes each Series within a DataFrame multiple times using an iteration of fits
@@ -728,7 +619,7 @@ def fit_transform_element(self, df: pd.DataFrame) -> pd.DataFrame:
728619 hyperparams [hyperparam ] = value
729620
730621 model = self .type_model (** hyperparams )
731-
622+
732623 if self .fit_on_nan :
733624 X = df .drop (columns = col )
734625 else :
@@ -802,3 +693,111 @@ def fit_transform_element(self, df: pd.DataFrame) -> pd.Series:
802693 df_imp .loc [is_na , col ] = random_pred [is_na ]
803694
804695 return df_imp
696+
697+
698+ class ImputerRPCA (Imputer ):
699+ """
700+ This class implements the RPCA imputation
701+
702+ Parameters
703+ ----------
704+ method : str
705+ name of the RPCA method:
706+ "PCP" for basic RPCA
707+ "temporal" for temporal RPCA, with regularisations
708+ "online" for online RPCA
709+ columnwise : bool
710+ for RPCA method to be applied columnwise (with reshaping of each column into an array)
711+ or to be applied directly on the dataframe. By default, the value is set to False.
712+ """
713+
714+ def __init__ (
715+ self ,
716+ method : str = "temporal" ,
717+ groups : List [str ] = [],
718+ columnwise : bool = False ,
719+ ** hyperparams
720+ ) -> None :
721+ super ().__init__ (groups = groups , columnwise = columnwise , hyperparams = hyperparams )
722+
723+ self .method = method
724+
725+ def fit_transform_element (self , df : pd .DataFrame ) -> pd .DataFrame :
726+ """
727+ Fit/transform to impute with RPCA methods
728+
729+ Parameters
730+ ----------
731+ df : pd.DataFrame
732+ dataframe to impute
733+
734+ Returns
735+ -------
736+ pd.DataFrame
737+ imputed dataframe
738+ """
739+ if not isinstance (df , pd .DataFrame ):
740+ raise ValueError ("Input has to be a pandas.DataFrame." )
741+
742+ if self .method == "PCP" :
743+ rpca = RPCA (** self .hyperparams_element )
744+ elif self .method == "temporal" :
745+ rpca = TemporalRPCA (** self .hyperparams_element )
746+ elif self .method == "onlinetemporal" :
747+ rpca = OnlineTemporalRPCA (** self .hyperparams_element )
748+
749+ df_imputed = pd .DataFrame (rpca .fit_transform (X = df .values ), index = df .index , columns = df .columns )
750+
751+ return df_imputed
752+
753+
754+ class ImputeEM (Imputer ):
755+ def __init__ (
756+ self ,
757+ groups : List [str ]= [],
758+ method : Optional [str ] = "multinormal" ,
759+ columnwise : bool = False ,
760+ ** hyperparams
761+
762+ ):
763+ super ().__init__ (groups = groups , columnwise = columnwise , hyperparams = hyperparams )
764+ self .method = method
765+ # if method == "multinormal":
766+ # self.model = em_sampler.ImputeMultiNormalEM(
767+ # **hyperparams
768+ # )
769+ # elif method == "VAR1":
770+ # self.model = em_sampler.ImputeVAR1EM(
771+ # **hyperparams
772+ # )
773+ # else:
774+ # raise ValueError("Strategy '{strategy}' is not handled by ImputeEM!")
775+
776+ def fit_transform_element (self , df : pd .DataFrame ) -> pd .DataFrame :
777+ if self .method == "multinormal" :
778+ model = em_sampler .MultiNormalEM (
779+ ** self .hyperparams_element
780+ )
781+ elif self .method == "VAR1" :
782+ model = em_sampler .VAR1EM (
783+ ** self .hyperparams_element
784+ )
785+ else :
786+ raise ValueError ("Strategy '{strategy}' is not handled by ImputeEM!" )
787+ X = df .values
788+ model .fit (X )
789+
790+ X_transformed = model .transform (X )
791+ df_transformed = pd .DataFrame (X_transformed , columns = df .columns , index = df .index )
792+ return df_transformed
793+
794+ # def fit(self, df):
795+ # X = df.values
796+ # self.model.fit(X)
797+ # return self
798+
799+ # def transform(self, df):
800+ # X = df.values
801+ # X_transformed = self.model.transform(X)
802+ # df_transformed = pd.DataFrame(X_transformed, columns=df.columns, index=df.index)
803+ # return df_transformed
0 commit comments