Solution doesn't make any sense to me

The assignment is VERY unclear on this page. We are told to use the formula from the top of the page, which, from the looks of it, should use the nr_of_transactions and the amount_spent columns, based on the formula AND the description. Why do we use the scaled columns in order to create the score column?

Furthermore, the solution itself is unlike anything I’ve ever seen at DQ or anywhere else for that matter. Why are there backslashes \ ahead of the minus - symbols in the scaling calculations? Are we escaping the minus symbols? I included the code from the solution below, but I would’ve expected something like this to work:

best_churn['scaled_tran'] = (best_churn['nr_of_transactions'] - best_churn['nr_of_transactions'].min()) / (best_churn['nr_of_transactions'].max() - best_churn['nr_of_transactions'].min()) 

Why doesn’t that work?

Screen Link: Learn data science with Python and R projects

best_churn["scaled_tran"] = (best_churn["nr_of_transactions"] \
                             - best_churn["nr_of_transactions"].min()) \
                             / (best_churn["nr_of_transactions"].max() \
                             - best_churn["nr_of_transactions"].min())

best_churn["scaled_amount"] = (best_churn["amount_spent"] \
                               -best_churn["amount_spent"].min()) \
                               / (best_churn["amount_spent"].max() \
                               - best_churn["amount_spent"].min())

best_churn["score"] = 100*(.5*best_churn["scaled_tran"] \
                           + .5*best_churn["scaled_amount"])

best_churn.sort_values("score", inplace=True, ascending=False)
1 Like

According to PEP8, a line should not have more than 79 characters. \ tells python that the code continues on the next line.

You may need to scale your data because the inputs have different scales. For example, you have two explanatory variables, price and quantity of say a car. The price may be several hundreds of dollars while the quantity may be in tens.

You scale so that both variables would be on the same scale, usually between 0 and 1.


Does that mean that the code doesn’t work unless you use \?

It works. But for readability and following the PEP8 guidelines.

I believe what’s happening behind the scenes here is that we are actually escaping the return character at the end of the line so that python thinks it’s just one big long line of code. But you are correct, your long line of code should work. Do you get an error when you try it?

I did! It seemed weird to me that it wouldn’t work.

That seems weird to me too. Can you post the error you get to see if we can figure out why it doesn’t work?

That said, I do agree with @monorienaghogho that for readability purposes and in order to adhere to PEP8 Guidelines as best we can, we should be escaping the return characters so that we don’t exceed 79 characters per line.

I haven’t made it to this mission yet but I just copy/pasted your code in the link you provided and didn’t get an error…

1 Like