0
$\begingroup$

I'm using a predict function for a linear SVM classifier:

def predict(self, train_file: str, test_file: str, lower_case: bool) -> pd.DataFrame:
"Train model using sklearn pipeline"
train_df = self.read_data(train_file, lower_case)
dev_df = self.read_data(dev_file, lower_case)
learner = self.pipeline.fit(train_df['text'], train_df['truth'])
# Fit the learner to the test data
test_df = self.read_data(test_file, lower_case)
# assuming dev_df is validset
dev_df['pred'] = learner.predict(dev_df['text'])
test_df['pred'] = learner.predict(test_df['text'])
return test_df, dev_df

And the predict function refers also to:

    def run_classifier(files: Tuple[str, str, str],
               method: str,
               method_class: Base,
               model_file: str,
               lower_case: bool) -> None:
"Inherit classes from classifiers.py and apply the predict/accuracy methods"
train, dev, test = files  # Unpack train, dev and test filenames
result = method_class.predict(train, test, lower_case)
method_class.accuracy(result)
# Plot confusion matrix
make_dirs("Plots")
print(result)
fig, ax = plot_confusion_matrix(result['truth'], result['pred'], normalize=True)
ax.set_title("Normalized Confusion Matrix: {}".format(method.title()))
fig.tight_layout()
fig.savefig("Plots/{}.png".format(method))

Where the dev set is not really taken into consideration. When I run predict, I receive this error: predict() missing 1 required positional argument: 'lower_case but I don't understand how to solve it.

$\endgroup$
9
  • $\begingroup$ is method_class the name of your class ? If so, no self is needed in the function declaration and that would explain why he thinks lower_case is missing. Otherwise try method_class.predict(train=train, test=test, lower_case=lower_case) and pls reupload the error $\endgroup$ – mprouveur Sep 15 '20 at 14:07
  • $\begingroup$ hi, method_class helps me because I have many classifiers and specifying it when running the code changes the outcome. What should I do? $\endgroup$ – Anna Sep 15 '20 at 14:16
  • $\begingroup$ I am not sure I quite understood but this seems fine. Have you tried declaring your function as def predict(train_file: str, test_file: str, lower_case: bool) instead of def predict(self, train_file: str, test_file: str, lower_case: bool)? And have you tried calling predict with method_class.predict(train=train, test=test, lower_case=lower_case)? $\endgroup$ – mprouveur Sep 15 '20 at 14:56
  • $\begingroup$ As for the second one, it says it got an unexpected argument (train) or I don't understand what you meant at all... I am basing on this repo (github.com/prrao87/fine-grained-sentiment) $\endgroup$ – Anna Sep 15 '20 at 15:06
  • $\begingroup$ Yes sorry that's a typo, I meant method_class.predict(train_file=train, test_file=test, lower_case=lower_case) $\endgroup$ – mprouveur Sep 15 '20 at 15:09
0
$\begingroup$

Based on the repo you linked in the comment section (github.com/prrao87/fine-grained-sentiment), method_class is meant to be a Class not an instance. Therefore method_class.predict is meant to be a static method not an instance method, Static method are declared without specifying a self (as they are not linked to an instance but a class) declaring your function as def predict(train_file: str, test_file: str, lower_case: bool) should fix your problem.

Some reference on the topic : https://www.makeuseof.com/tag/python-instance-static-class-methods/

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.