udacity / ud120-projects

Starter project code for students taking Udacity ud120
1.61k stars 5.66k forks source link

File not found Error in Naïve Bayes Project #361

Open handyworkz opened 1 year ago

handyworkz commented 1 year ago

This is the code

import sys
from time import time
sys.path.append("C:/Users/ADWIN/Documents/GitHub/ud120-projects/tools")
from email_preprocess import preprocess

features_train, features_test, labels_train, labels_test = preprocess()

from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
t0 =time()
clf.fit(features_train, labels_train)
print ("Training time: ", round(time()-t0, 3), "s")

t1 =time()
predictions = clf.predict(features_test)
print ("Prediction time: ", round(time()-t1, 3), "s")

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(labels_test, predictions)
print(accuracy)

And this, the error

c:/Users/ADWIN/Documents/GitHub/ud120-projects/naive_bayes/nb_author_id.py Traceback (most recent call last): File "c:\Users\ADWIN\Documents\GitHub\ud120-projects\naive_bayes\nb_author_id.py", line 22, in <module> features_train, features_test, labels_train, labels_test = preprocess() File "C:\Users/ADWIN/Documents/GitHub/ud120-projects/tools\email_preprocess.py", line 30, in preprocess authors_file_handler = open(authors_file, "rb") FileNotFoundError: [Errno 2] No such file or directory: '../tools/email_authors.pkl'

Anytime I try to run the code, this error pops up, I have tried moving the email_preprocess file into the naive_bayes directory but the issue persists.

Can any one help?

Badrgamalahmed commented 1 year ago

You can see this code

from sklearn.naive_bayes import GaussianNB

""" this is the code to accompany the Lesson 1 (Naive Bayes) mini-project use a Naive Bayes Classifier to identify emails by their authors authors and labels: Sara has label 0 Chris has label 1 """ import sysfrom time import timesys.path.append("../tools/")from email_preprocess import preprocess

features_train and features_test are the features for the

training### and testing datasets, respectively### labels_train and labels_test are the corresponding item labelsfeatures_train, features_test, labels_train, labels_test = preprocess() clf = GaussianNB()t0 = time()clf.fit(features_train,labels_train)print 'training time',round(time()-t0,3) ,'s't0 = time()clf.score(features_test,labels_test)print 'predicting time', round(time()-t0,3),'s'

في الأحد، ٦ نوفمبر ٢٠٢٢ ٩:١٠ ص HandyWorkZ @.***> كتب:

This is the code

import sys from time import time sys.path.append("C:/Users/ADWIN/Documents/GitHub/ud120-projects/tools") from email_preprocess import preprocess

features_train, features_test, labels_train, labels_test = preprocess()

from sklearn.naive_bayes import GaussianNB clf = GaussianNB() t0 =time() clf.fit(features_train, labels_train) print ("Training time: ", round(time()-t0, 3), "s")

t1 =time() predictions = clf.predict(features_test) print ("Prediction time: ", round(time()-t1, 3), "s")

from sklearn.metrics import accuracy_score accuracy = accuracy_score(labels_test, predictions) print(accuracy)

And this, the error

c:/Users/ADWIN/Documents/GitHub/ud120-projects/naive_bayes/nb_author_id.py Traceback (most recent call last): File "c:\Users\ADWIN\Documents\GitHub\ud120-projects\naive_bayes\nb_author_id.py", line 22, in features_train, features_test, labels_train, labels_test = preprocess() File "C:\Users/ADWIN/Documents/GitHub/ud120-projects/tools\email_preprocess.py", line 30, in preprocess authors_file_handler = open(authors_file, "rb") FileNotFoundError: [Errno 2] No such file or directory: '../tools/email_authors.pkl'

Anytime I try to run the code, this error pops up, I have tried moving the email_preprocess file into the naive_bayes directory but the issue persists.

Can any one help?

— Reply to this email directly, view it on GitHub https://github.com/udacity/ud120-projects/issues/361, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWXGB6OI2U2LOKZ4PINLJSDWG5KV7ANCNFSM6AAAAAARYJJYIY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

kleiner-fisch commented 1 year ago

For me the problem was that I executed the code in an IDE. Even though I ran nb_author_id.py, the working directory was the root directory of the repository.

For me it worked by first doing

import os
os.chdir("naive_bayes")