Search code examples
pythondecision-treepermission-denied

How to fix permission denied error in python?


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


Solution

  • 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()