C1_W1_lecture_nb_02 - NameError: name 'ys' is not defined


NameError Traceback (most recent call last)
----> 1 yslist=np.squeeze(ys).tolist()

NameError: name ‘ys’ is not defined

how should I fix this to go on?
thx in advance!

we are using the build_freqs function:

def build_freqs(tweets, ys):

I see that we have already defined ‘tweets’, by combining the positive and negative tweets lists:
tweets = all_positive_tweets + all_negative_tweets

I didn’t define any ‘ys’ name, even thou we have created the array with labels of tweets calling it “labels”:
labels = np.append(np.ones((len(all_positive_tweets))), np.zeros((len(all_negative_tweets))))

I don’t understand if somehow ‘labels’ should be turned into ys or if there’s anything I am missing

Many thanks

I took this class 1+ years ago so maybe it has been updated, buy my version from 2022 has this line in the 3^{rd} executable cell…

from utils import process_tweet, build_freqs

Do you have that? If so, and the build_freqs function is the one imported from utils.py then ys is the name of a parameter passed to the function. The name is local to the function block…you don’t have to do anything to create it other than pass a parameter to the function when you call it. Unless you have modified that function, which you shouldn’t have done (ahem), you shouldn’t be seeing errors from it. You didn’t modify it did you? Or reimplement it yourself?

Hello! Thanks for your reply.
I have the import from utils.py as well, didnt modify anything from the path shown in the course. I tried again this morning, still not working.

I am attaching a screenshot of the code for better clarification, probably I am missing something…

Hey @Gabriele_Moraggi,
I am a little confused. It looks like you have modified the lab, since the lab looks nothing like this. Nonetheless, when using the build_freqs function, you are supposed to use labels as the argument value for the parameter ys. I hope this makes sense.

P.S. - You are not supposed to write the build_freqs function in the UGL. It is already provided for you in the utils.py file.


1 Like

You’re going to need help from a mentor or someone currently taking the class. I took it a long time ago and can’t see the latest class notebook, but I will say that looks very strange and nothing at all like what I have.

If you’re importing a function from another module, in this case utils.py the def function(): line shouldn’t appear in the notebook. You just import it, then use it. Further, in the cell where build_freq is defined, the function has no body. The line giving the error is supposed to be part of that function block, and thus indented, which would give the context for the name.

I don’t think this is an honor code issue since it is provided in the utils file.

That is what the build_freqs() function should look like. Notice the indentation, which is how Python creates namespaces.

Hi Elemento,

this is not the lab screenshot, but the online Jupyter notebook I am using to run the code, following the steps indicated by the course ( the dictionary part is skipped to show you the issue with ys).

When you say that “labels” is the argument value for the parameter “ys”, you mean that I should write something like:

def build_freqs (tweets, labels):
yslist = np.squeeze (labels).tolist()

and then populate the dictionary?
many thanks in advance


So basically this part of the lab doesn’t need to be written in the notebook, I just have to create the frequency dictionary by using

freqs = build_freqs (tweets, labels)

thank you!!


In my old notebook there is a split between train and test. Is it still there? If so, the variable containing the inputs (your tweets) and the variable containing the labels (your labels) need to align with that split. In my code, those are x_train and y_train respectively.

the split of data in the training piece (train_x = train pos + train neg) vs testing piece is in the 3rd lab… will go through it this afternoon :slight_smile:

@ai_curious @Elemento Thank you so much both!!