self.predict_y = self.reg.predict(_X) AttributeError: 'NoneType' object has no attribute 'predict'
时间: 2023-10-13 10:12:44 浏览: 100
This error message suggests that the object "self.reg" is of type "NoneType", which means it has no attribute called "predict". Therefore, when the code tries to call the "predict" method on "self.reg", it raises an AttributeError.
To fix this error, you need to make sure that "self.reg" is initialized properly and is not None. You can check if "self.reg" is None by adding a print statement before the line that raises the error:
```
print(self.reg) # add this line to check if self.reg is None
self.predict_y = self.reg.predict(_X)
```
If the output of the print statement is "None", then you need to initialize "self.reg" before calling the "predict" method. For example, if you are using scikit-learn's linear regression model, you can initialize "self.reg" as follows:
```
from sklearn.linear_model import LinearRegression
class MyModel:
def __init__(self):
self.reg = LinearRegression()
def fit(self, X, y):
self.reg.fit(X, y)
def predict(self, X):
_X = self._transform(X)
self.predict_y = self.reg.predict(_X)
return self.predict_y
```
This initializes "self.reg" to a new instance of the LinearRegression class, which has the "predict" method you need to call later.
阅读全文