Difficulty in running python code in command line

I’m doing the below exercise and trying to run the below in the command line on a macbook. The link for the exercise is below.

https://www.codecademy.com/courses/machine-learning/lessons/why-machine-learning/exercises/supervised-learning

I’m trying to do the machine learning class with the below code and run the code in the command line, and I am getting an error message also below. I think the issue comes down to the texts library, which I am having so much difficulty finding anything about online.

Is there a library or install that is necessary to run this codecademy python code? I have python 3 installed already.


from texts import text_counter, text_training

from sklearn.feature_extraction.text import CountVectorizer

from sklearn.naive_bayes import MultinomialNB

intercepted_text = "This hot dog was awful!"

text_counts = text_counter.transform([intercepted_text])

text_classifier = MultinomialNB()

print(text_classifier)

text_labels = [0] * 1000 + [1] * 1000

print(text_labels)

text_classifier.fit(text_training, text_labels)

print(text_classifier)

final_pos = text_classifier.predict_proba(text_counts)[0][1]

final_neg = text_classifier.predict_proba(text_counts)[0][0]

if final_pos > final_neg:

print("The text is positive.")

else:

print("The text is negative.")

I’m getting the following error when using the command line:


Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'text_counter' is not defined

>>> 

>>> text_classifier = MultinomialNB()

>>> 

>>> text_labels = [0] * 1000 + [1] * 1000

>>> 

>>> text_classifier.fit(text_training, text_labels)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'text_training' is not defined

>>> 

>>> final_pos = text_classifier.predict_proba(text_counts)[0][1]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'text_counts' is not defined

>>> 

>>> final_neg = text_classifier.predict_proba(text_counts)[0][0]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'text_counts' is not defined

>>> 

>>> if final_pos > final_neg:

... print("The text is positive.")

... else:

... print("The text is negative.")

... 

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'final_pos' is not defined

I’m having trouble fining documentation about a texts library online.

that’s not a library, you can inspect it from codecademy’s environment to obtain its path and then print out the code

Thank you! How do I do this?

Do specifically what?
The general idea of what I’m suggesting is that the file exists on a computer that you control (you can run python code on it, so you can do mostly anything with it)

Anyway if you follow the rabbit hole then you’ll run into some things where you’d need to get really creative to move that information to your own computer and I’m not sure it’s worth the trouble.

To start, what do you mean by inspect the Codecademy environment?

texts is a module in the environment where your code runs
you send code to that environment, it gets executed
since you execute code in that environment, there’s nothing stopping you from inspecting it same as you would check your local computer’s picture folder to see if there are any cat pics there
python clearly knows where the module is, or it wouldn’t be able to import it. what can python tell you about a module? what would you want to know, can python tell you that?

Ok, feel like I’m really close. Thank you!

If I execute a sys.path for loop command for i in sys, I get a number of results. Let’s say these are 2 examples:

  1. /var/codecademy/runner_contexts/python
  2. /home/ccuser/workspace/why-machine-learning-supervised-learning-1

I’m having trouble figuring out how to append these to the URL to view the text

You can list the contents of a directory with the program ls, or with python itself
Though if you obtain the path for the module then you won’t need to check the directories yourself

if you read the texts module you’ll find that it references some other files. those other files are more difficult to copy because they’re not text, you can’t print them unless you encode them as text for example with base64. you could send them over the network connection since the internet is accessible

Resolved, figured it out. Thank you for the assistance!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.