-1

I was trying to create a dot file to visualize my decision tree, but getting the permission denied error

credit_df=pd.read_csv('credit.csv', index_col=0) #importing the dataset
X = credit_df.drop("default" , axis=1) #defining independent variable
Y=credit_df.pop("default") #defining dependent variable

from sklearn.model_selection import train_test_split #spliting the data into train and test data

X_train, X_test, train_labels, test_labels = train_test_split(X, y, test_size=.30, random_state=1) #categorising the data

dt_model = DecisionTreeClassifier(criterion = 'gini' ) #creating the decision tree model

dt_model.fit(X_train,train_labels) #fitting the model into the train data

from sklearn import tree
train_char_label=['NO','YES'] #defining the target variable label
credit_tree_title=open('c:\credit_tree.dot','w') #creating the dot file
dot_data = tree.export_graphviz(dt_model, out_file=Credit_Tree_File, feature_names = list(X_train), class_names = list(train_char_label))

Credit_Tree_File.close() #closing the dot file

Error message

Ranjan
  • 19
  • 1
  • 1
  • 8
  • 1
    Please add the error message to your question, including the complete stack trace – CryptoFool Mar 16 '22 at 04:45
  • 1
    You only have one backslash in the file path in your code, but you show two backslashes in the title of your question. I assume that there are two slashes in the code you are running. If that's not true, that might be your problem. Are you able to create a file at the root of your `C:` drive in other ways? For example, can you save a text file there from your source editor? There should be no reason that Python can't write a file there if you are otherwise able to do so as the same user. – CryptoFool Mar 16 '22 at 04:49
  • 1
    Don't use the root of "C:" for that. Use something in your Documents folder. – Tim Roberts Mar 16 '22 at 04:51
  • 1
    The error message means you don't have write access to the root folder on the C: drive, as really you should not. The real bug here is trying to write to a location where you have no business writing anything. Perhaps see [Difference between `./` and `~/`](https://stackoverflow.com/questions/31435921/difference-between-and/55342466) which contains an answer with an introduction to file systems. – tripleee Mar 16 '22 at 05:29

1 Answers1

0

I used the path of a folder with double slash instead of the c drive as suggested by Tim Roberts and my issue got fixed.

from sklearn import tree
train_char_label=['NO','YES']
credit_tree_file=open('C:\\Users\\jyoti\\Desktop\\Jupyter Notebook\\Data Mining\\CART\\Case study\\Credit\\credit_tree.dot' , 'w')
dot_data = tree.export_graphviz(dt_model, out_file=credit_tree_file, feature_names = list(X_train), class_names = list(train_char_label))

credit_tree_file.close()
Ranjan
  • 19
  • 1
  • 1
  • 8