Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

pandas - Python sklearn linear regression error: fit() missing 1 required positional argument: 'y'"

I'm very new to Python and scikit-learn. I'm having difficulty working with the scikit-learn Boston data house prices data set. Please find my code below.

Thanks!

import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import sklearn

bos = pd.DataFrame(boston.data)
bos.head()

bos.columns = boston.feature_names
bos.head()

boston.target[:5]

bos['PRICE'] = boston.target
bos.head()

from sklearn.linear_model import LinearRegression
X = bos.drop('PRICE', axis = 1)
lm = LinearRegression

LinearRegression.fit
lm.fit(X,bos.PRICE) 


TypeError Traceback (most recent call last)
<ipython-input-52-f9496290723b> in <module>
      1 LinearRegression.fit
----> 2 lm.fit(X,bos.PRICE)

TypeError: fit() missing 1 required positional argument: 'y'

question from:https://stackoverflow.com/questions/65877365/python-sklearn-linear-regression-error-fit-missing-1-required-positional-argu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should be using lm = LinearRegression() and not lm = LinearRegression.

You can use lm.fit? to see the signature of the method. You would see that it is

Signature: lm.fit(self, X, y, sample_weight=None)

So your X is being assigned to self, and bos.PRICE to X, and it is complaining that y is not provided. Seeing the self there should give you an indication that the wrong method is being called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...