-1

I have the following classifiers:

DT = sklearn.tree.DecisionTreeClassifier()
LR = sklearn.linear_model.LogisticRegression()
SVC = sklearn.svm.SVC()

So I created this list of classifiers:

classifiers = [DT, LR, SVC]

I want to create directories, one for each classifier in the list, so that I have these directories:

working_dir
├── DT
├── LR
├── SVC

Doing this in a way like:

for clf in classifiers:
    # create a dir named clf (e.g. DT) if not exists

How can this be done.

Amina Umar
  • 502
  • 1
  • 9
  • For the directory creation, see: https://stackoverflow.com/questions/273192/how-can-i-safely-create-a-directory-possibly-including-intermediate-directories/14364249. For your data structure, a dict might be more appropriate. Something like `classifiers = {'DT': DecisionTreeClassifier(), ... }`. Then you can use the dictionary keys as your directory names. – slothrop Mar 06 '23 at 18:06
  • See [Facts and myths about Python names and values](//nedbatchelder.com/text/names.html). A variable's value is unaware of its name. Once you put the value in the list, there's no way to retrieve its previous name given just the value. You could get what you want by making `classifiers` a _dictionary_ that looks like `{"DT": DT, "LR": LR, "SVC": SVC}` and then iterating over `for name, val in classifiers.items()`, but since you hardcoded your variable names, why not also hardcode the directory names and create paths, as demonstrated by the link in the comment above? – Pranav Hosangadi Mar 06 '23 at 19:20

1 Answers1

0

Per the linked comment thread here

from pathlib import Path

classifiers = ["DT", "LR", "SVC"]

for clf in classifiers:
    Path(clf).mkdir(exist_ok=True)

Assuming you are already in working_dir

wjmccann
  • 502
  • 6
  • 18