I'm trying to resample my dataset after splitting it into train and test partitions using SMOTE. Here's my code:
smote_X = df[cols]
smote_Y = df[target_col]
#Split train and test data
smote_train_X,smote_test_X,smote_train_Y,smote_test_Y = train_test_split(smote_X,smote_Y,test_size = .25,random_state = 111)
smote_train_Y_series = smote_train_Y.iloc[:,0]
#oversampling minority class using smote
os = SMOTE(random_state = 0)
os_smote_X,os_smote_Y = os.fit_sample(smote_train_X_matrix,smote_train_Y_series)
I added line #5 to convert the DataFrame coming out of train_test_split
to Series as the newer version of SMOTE fit_sample
(docs) wants this data type but it now throws the following error.
Any ideas how to fix it?
-------------------------------------------------------------------------- KeyError Traceback (most recent call last) in 16 #oversampling minority class using smote 17 os = SMOTE(random_state = 0) ---> 18 os_smote_X,os_smote_Y = os.fit_sample(smote_train_X_matrix,smote_train_Y_series) 19 os_smote_X = pd.DataFrame(data = os_smote_X,columns=cols) 20 os_smote_Y = pd.DataFrame(data = os_smote_Y,columns=target_col)
/opt/conda/lib/python3.6/site-packages/imblearn/base.py in fit_resample(self, X, y) 86 if self.X_columns is not None: 87 X = pd.DataFrame(output[0], columns=self.X_columns) ---> 88 X = X_.astype(self.X_dtypes) 89 else: 90 X = output[0]
/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs) 5863
results.append( 5864 col.astype( -> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs 5866 ) 5867
)/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs) 5846 if len(dtype) > 1 or self.name not in dtype: 5847
raise KeyError( -> 5848 "Only the Series name can be used for " 5849 "the key in Series dtype mappings." 5850 )KeyError: 'Only the Series name can be used for the key in Series dtype mappings.'