There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
When defining the correlation variable that will use pearsonr(), you add a p, such as in the following:
corr_sqfeet_beds, p = pearsonr(housing.sqfeet, housing.beds)
print(corr_sqfeet_beds)
This isn’t explained in the exercise, but I’m curious why the extra variable is needed? The output was unchanged with or without it but the exercise would not advance without the p variable.
Thanks! I’m wondering if you ever found anything specifically on that syntax? I’ve never seen a variable declared without the = sign like that and with the comma instead before the p value. I get what it is doing based on what you showed, but I don’t know what it is doing or when I’d do that again except in this very specific situation.
Yes! Assigning multiple variables in one line of code like this is pretty common. Anytime you call a class that returns more than 1 result you’ll need multiple variables to store them. The most common example I can think of that I use regularly is scikit-learn’s train_test_split
Basically, when we want to split a dataset into train and test subsets we would define X_train, X_test, y_train, y_test in one line:
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42)
test_size and random_state are additional parameters you pass in. Each of the variables we defined will hold an output of train_test_split to be used later. Order matters when we do this!
Ahhhhhh that makes so much sense! I did some other learning platform where I went over train_test_split, I had forgotten that this was the same thing. Thank you very much!
I was wondering the same thing, just to confirm my thinking. Is the comma just to distinquish between the two outputs. Pearsonr will provide two outputs, first “corr_price_sqfeet” and the 2nd “p”.
Yes, that true. Comma is used for separating two outputs of a pearsonr() method and also you can use such comma separated assigning operation with another code examples in Python such as: