'Unresolved attribute reference "predict()" using scikit-learn in Pycharm

When using a decision tree classifier from scikit-learn, the docs show you reassigning the variable storing the classifier to the output of itself calling the fit() method:

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)

However, now if I call the predict method:

clf.predict([[1,1]])

Pycharm warms me:

Unresolved attribute reference 'predict' for class 'object'

You can look up the declaration for fit() in Pycharm easily, and the method merely returns self, so the reassignment is not necessary and you can remove it so that I have instead:

clf = tree.DecisionTreeClassifier()
clf.fit(X, Y)

Everything runs smoothly both ways, but Pycharm doesn't give me a warning with the latter. I'm curious, because I'm fairly new to Python and Pycharm, why does it give me this warning? Is there a way to make this IDE recognize that the method returns self and therefore is still the same type with the same method predict()? Otherwise, is there any way to remove this warning?



Solution 1:[1]

You would want to do something like this instead to avoid the warning

km = KMeans(n_clusters=size)
km.fit(x)
predictions = km.predict(x)

For some reason, fit returns a object type. I am unsure if it's intentional. KMeans initializer returns the correct type and fit is in-place.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zack Light