1

I am getting this error while working on question 4 (Chapter 2) from the "Hands on Machine learning" book. This is the question "Try creating a single pipeline that does the full data preparation plus the final prediction". The solution is available in Github link, but the solution is giving me the error mentioned in the title. I used the housing data for my example. Please help me.

I wrote this command and it fired the following error:

prepare_select_and_predict_pipeline = Pipeline([
('preparation', full_pipeline),
('feature_selection', TopFeatureSelector(feature_importances, k)),
('svm_reg', SVR(**rnd_search.best_params_))
])

prepare_select_and_predict_pipeline.fit(housing,housing_labels)

Error:

TypeError: fit() takes 2 positional arguments but 3 were given

I would like to attach the solution from Github Solution of Question 2 from Github

But this isn't working for me. :(

Bart Van Loon
  • 1,430
  • 8
  • 18
Milan
  • 11
  • 1
  • 1
  • 4
  • can you upload the full code? – Nimish Bansal Oct 06 '17 at 19:46
  • A bit late to the party, but if it helps, check out: [https://stackoverflow.com/questions/46162855/fit-transform-takes-2-positional-arguments-but-3-were-given-with-labelbinarize](https://stackoverflow.com/questions/46162855/fit-transform-takes-2-positional-arguments-but-3-were-given-with-labelbinarize) – Locke Feb 12 '19 at 18:40

1 Answers1

3

Warning, I'm going to grossly simplify here, but I hope this will help.

In Python, if you call a function on an object, the object itself is always passed as the first argument (unless it is a static or class method). This is usually captured by a parameter we call self.

So, if you call object.function(), you are passing an argument to function, namely object itself.

class C:
    def f(self):
        print(self)

o = C()
o.f()         # <__main__.C object at 0x7f1049993f28>
o.f('hello')  # TypeError: f() takes 1 positional argument but 2 were given

In your case, you are calling prepare_select_and_predict_pipeline.fit(housing, housing_labels), so you are passing the function fit three arguments: prepare_select_and_predict_pipeline, housing and housing_labels.

If you check the definition of the fit method, you will probably find that it indeed only takes two arguments. I'm guessing the first one will be called self.

Bart Van Loon
  • 1,430
  • 8
  • 18
  • Thanks for sharing this information, but can you help me what I should do now. – Milan Oct 06 '17 at 20:25
  • To start, remove one of the arguments from your function call. You probably want to either check the documentation or the source code for the `fit` function you're calling. – Bart Van Loon Oct 06 '17 at 22:02
  • Ok. Will look into the documentation. Thank you – Milan Oct 09 '17 at 23:27