def linearInterpolation(db): for i in range(0, db.shape[1]): ts = db[:,i] seq = sq.detectSequences(ts) if len(seq)>1: ##In case there are more than one sequence seq = sq.joinSequences(seq) ##Join sequences in case they are relatively close seq = pointSelector(seq) ##Select points for j in range(0, len(seq)): s = pd.Series(ts[seq[j][0]:seq[j][1]]) db[seq[j][0]:seq[j][1],i] = (s.interpolate(method = "linear")).values ##In case values are lost at the beggining or the end of the TS, db = (pd.DataFrame(db)).fillna(method='pad').as_matrix() db = (pd.DataFrame(db)).fillna(method='bfill').as_matrix() return(db)
def multInterpolation(db): for i in range(0, db.shape[1]): ts = db[:,i] seq = sq.detectSequences(ts) if len(seq)>1: ##In case there are more than one sequence seq = sq.joinSequences(seq) ##Join sequences in case they are relatively close seq = pointSelector(seq) ##Select points for j in range(0, len(seq)): s = pd.Series(ts[seq[j][0]:seq[j][1]]) ##Three types are considered, combining to a simple Multiple Imputation Algorithm. s1 = s.interpolate(method = "polynomial", order = int(min(s.notnull().sum()-1, 4))) s2 = s.interpolate(method = "spline", order = int(min(s.notnull().sum()-1, 3))) s3 = s.interpolate(method = "cubic") db[seq[j][0]:seq[j][1],i] = (s1.values + s2.values + s3.values)/3 ##In case values are lost at the beggining or the end of the TS, db = (pd.DataFrame(db)).fillna(method='pad').as_matrix() db = (pd.DataFrame(db)).fillna(method='bfill').as_matrix() return(db)