There are 2 transfomers in ColumnTransformer. But the result of ColumnTransformer.fit_transform()
only contains the later transfromer's result:
pos_time
array([[1.24100000e+03, 6.27000000e+02, 1.56279701e+09],
[1.27100000e+03, 6.90000000e+02, 1.56279701e+09],
[1.30200000e+03, 7.49000000e+02, 1.56279701e+09],
...,
[1.81600000e+03, 8.60000000e+01, 1.56279703e+09],
[1.81600000e+03, 8.60000000e+01, 1.56279703e+09],
[1.81600000e+03, 8.60000000e+01, 1.56279703e+09]])
I think this is because this line X=X.drop('Time',axis=1)
in TimeTransformer
. if I comment out the line, the result of ColumnTransformer.fit_transform()
will be :
pos_time
array([[1241.0, 627.0, Timestamp('2019-07-10 22:16:46.036385'),
1562797006.036385],
[1271.0, 690.0, Timestamp('2019-07-10 22:16:46.052012'),
1562797006.052012],
[1302.0, 749.0, Timestamp('2019-07-10 22:16:46.067638'),
1562797006.067638],
...,
[1816.0, 86.0, Timestamp('2019-07-10 22:17:08.327709'),
1562797028.327709],
[1816.0, 86.0, Timestamp('2019-07-10 22:17:08.496155'),
1562797028.496155],
[1816.0, 86.0, Timestamp('2019-07-10 22:17:08.585392'),
1562797028.585392]], dtype=object)
But I don't want to get the 3rd column. I would like to know why this happened and how to fix it. thank you!
Here is my code.
this PositionTransformer
convert a string into x, y coordinates:
def position(string):
x_,y_=string.lstrip('(').rstrip(')').split(',')
x_,y_=float(x_),float(y_)
return x_,y_
class PositionTransformer(BaseEstimator,TransformerMixin):
def __init__(self):
pass
def fit(self,X,y=None):
return self
def transform(self,X,y=None):
print(type(X))
print(X.head())
print(X.shape)
X=X['Position']
xy=X.apply(position)
x=xy.apply(lambda x:x[0])
y=xy.apply(lambda x:x[1])
xy=np.c_[x.values,y.values]
return xy
this TimeTransformer
convert string into timestamp:
class TimeTransformer(BaseEstimator,TransformerMixin):
def __init__(self):
pass
def fit(self,X,y=None):
return self
def transform(self,X,y=None):
X['Time']=pd.to_datetime(X['Time'])
X['UnixTime']=X.apply(lambda row:row['Time'].timestamp(), axis=1)
X=X.drop('Time',axis=1)
return X
this is ColumnTransformer
:
time=['Time']
pos=['Position']
time_pos_transformer=ColumnTransformer([
('pos_transformer',PositionTransformer(),pos),
('time_transformer',TimeTransformer(),time),
])
pos_time=time_pos_transformer.fit_transform(df)
sorry, the result of ColumnTransformer.fit_transform()
is right. The reason I made a mistake was that when I printed the pos_time
, the console output only part of it.