First of all the code snippet:
## Packages
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import fbeta_score
from imblearn.over_sampling import RandomOverSampler
from sklearn.datasets import make_classification
## Create dataset
class_weight = list([0.90])
X, Y = make_classification(n_samples = 1000, n_classes = 2, n_clusters_per_class = 2, weights = 0.9,
n_features = 10, n_informative = 10, class_sep = 1, shuffle = True)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, stratify = Y)
## Define Function
def resampled_fit(X, y, model, model_params, sampling, sampling_params):
sampling = sampling(**sampling_params) ## <= here is the issue
X_train_balanced, Y_train_balanced = sampling.fit_resample(X, y)
# Fit the model to the balanced training data
model_obj = model(**model_params).fit(X_train_balanced, Y_train_balanced)
# Compute performance metrics
Y_pred = model_obj.predict(X_train_balanced)
f2_val = fbeta_score(Y_train_balanced, Y_pred, beta = 2)
return f2_val
## Define Inputs
ROS = RandomOverSampler(random_state = 42)
ROS_params = dict(sampling_strategy = 0.8)
SVM = SVC()
SVM_params = dict(kernel = 'rbf', probability = True)
output = resampled_fit(X, Y, SVM, SVM_params, ROS, ROS_params)
Basically I want to input 'sampling_strategy' as a seperate argument (for RandomOverSampler) in my customized function in the same manner as I input the parameters for the classifier object seperately. However it does not work like this.
I get the error message:
TypeError: 'RandomOverSampler' object is not callable
I checked the type of the RandomOverSampler function but it is abc.ABCMeta
in the same way as the classifier object. What is the workaround to overwrite the input arguments of the RandomOverSampler within the function?
PS: Yes, I need to input the arguments seperately because I want to optimize the function with grid search afterwards. Obviously you need to do CV to use balanced sampling but as already mentioned this is only a snippet.
I would be thankful for any help regarding this issue.