Python - list the content of an algorithm

csehz used Ask the Experts™
Dear Experts,

Actually trying to analyze the machine learning logics behind in algorithms, for example the KNeighborsClassifier():

import pandas
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier()

Open in new window

Could you please advise would it be possible to list the content of an algorithm in Python and with which syntax concretely? The above print(model) of course just provides some overall information about the algorithm, but not the content

Thanks in advance,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
- You can check the official documentation.

- Some IDEs support clicking on the object (eg. on `KNeighborsClassifier`) while holding Ctr key. This would take you to the source file containing this object and then you can inspect the code. (personally I've only used PyCharm; I don't know which other IDEs support this functionality. When it comes to an IDE make sure you pick one that is not outdated and fits best your use case.).

- Also with those IDEs you can write (note the dot):

Open in new window

and it will suggest (that is, autocomplete) all attributes of that object.

- Alternatively, when it comes to open-source projects, you can check the source code on the internet.

There are several other ways to explore the contents of an object like:
- printing the contents of `dir(KNeighborsClassifier)`
- printing `help(KNeighborsClassifier)`
- printing `dir(KNeighborsClassifier())` or `dir(model)` in your code above.
csehzIT consultant


Thank you for the answer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial