Start learning free

LESSON 30 OF 165 · MACHINE LEARNING: ANALYSIS PROJECTS

What Moves Together? in Python

Practice

Do customers who visit more also order more? Correlation answers with one number from -1 to 1. Near 1, two columns rise together. Near -1, one rises as the other falls. Near 0, there is no straight-line link.

df[cols].corr() gives back a table with the correlation of every pair of the columns in cols. Down its diagonal, each column with itself is 1.0. .loc[row, column] picks one cell. For example, on two made-up columns:

t = pd.DataFrame({"hours": [1, 2, 3, 4], "score": [50, 60, 65, 80]})
print(round(t.corr().loc["hours", "score"], 2))   # 0.98

To find the column that moves most with another, take that column of the table and .drop(label) its own row: it gives back the Series without that label. .sort_values() gives back the values in order, smallest first. .abs() drops the minus signs, because -0.6 is a stronger link than 0.5, and .idxmax() gives back the label of the biggest value. And remember: two columns that move together don't prove that one causes the other.

Your script must print:

                 visits  orders  spent  days_since_last  returned_item  bought_again
visits             1.00    0.65   0.40            -0.27           0.01          0.50
orders             0.65    1.00   0.68            -0.35          -0.05          0.49
spent              0.40    0.68   1.00            -0.28          -0.04          0.40
days_since_last   -0.27   -0.35  -0.28             1.00           0.01         -0.42
returned_item      0.01   -0.05  -0.04             0.01           1.00         -0.08
bought_again       0.50    0.49   0.40            -0.42          -0.08          1.00
visits and orders: 0.65
days_since_last   -0.42
returned_item     -0.08
spent              0.40
orders             0.49
visits             0.50
visits moves most with bought_again (0.5), but that does not show that one causes the other.

Your task: print the correlation table of six number columns and the visits-orders cell. Then print every column's correlation with bought_again, sorted, and a sentence naming the column that moves most with it, up or down. Print the table and the sorted values with .to_string(). Without it, pandas splits the wide table across several lines when it is wider than 80 characters. It also adds a Name/dtype line under the sorted values.


Assignment
Follow the Coding Tutorial to build the correlation table of six columns and find the one that moves most with bought_again.


Hint
Drop bought_again's 1.0 with itself first, or it always wins. Then compare sizes with .abs() before .idxmax(): -0.42 is a stronger link than 0.40.


STEP 1 OF 8
    Ctrl + Enter to check

    Da Correlation in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free