{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**This notebook is an exercise in the [Intermediate Machine Learning](https://www.kaggle.com/learn/intermediate-machine-learning) course.  You can reference the tutorial at [this link](https://www.kaggle.com/alexisbcook/data-leakage).**\n\n---\n","metadata":{}},{"cell_type":"markdown","source":"Most people find target leakage very tricky until they've thought about it for a long time.\n\nSo, before trying to think about leakage in the housing price example, we'll go through a few examples in other applications. Things will feel more familiar once you come back to a question about house prices.\n\n# Setup\n\nThe questions below will give you feedback on your answers. Run the following cell to set up the feedback system.","metadata":{}},{"cell_type":"code","source":"# Set up code checking\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.ml_intermediate.ex7 import *\nprint(\"Setup Complete\")","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:10:11.583060Z","iopub.execute_input":"2022-07-22T10:10:11.583994Z","iopub.status.idle":"2022-07-22T10:10:11.658296Z","shell.execute_reply.started":"2022-07-22T10:10:11.583845Z","shell.execute_reply":"2022-07-22T10:10:11.657093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 1: The Data Science of Shoelaces\n\nNike has hired you as a data science consultant to help them save money on shoe materials. Your first assignment is to review a model one of their employees built to predict how many shoelaces they'll need each month. The features going into the machine learning model include:\n- The current month (January, February, etc)\n- Advertising expenditures in the previous month\n- Various macroeconomic features (like the unemployment rate) as of the beginning of the current month\n- The amount of leather they ended up using in the current month\n\nThe results show the model is almost perfectly accurate if you include the feature about how much leather they used. But it is only moderately accurate if you leave that feature out. You realize this is because the amount of leather they use is a perfect indicator of how many shoes they produce, which in turn tells you how many shoelaces they need.\n\nDo you think the _leather used_ feature constitutes a source of data leakage? If your answer is \"it depends,\" what does it depend on?\n\nAfter you have thought about your answer, check it against the solution below.","metadata":{}},{"cell_type":"code","source":"# Check your answer (Run this code cell to receive credit!)\nq_1.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:19:37.506756Z","iopub.execute_input":"2022-07-22T10:19:37.507314Z","iopub.status.idle":"2022-07-22T10:19:37.518644Z","shell.execute_reply.started":"2022-07-22T10:19:37.507268Z","shell.execute_reply":"2022-07-22T10:19:37.517802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 2: Return of the Shoelaces\n\nYou have a new idea. You could use the amount of leather Nike ordered (rather than the amount they actually used) leading up to a given month as a predictor in your shoelace model.\n\nDoes this change your answer about whether there is a leakage problem? If you answer \"it depends,\" what does it depend on?","metadata":{}},{"cell_type":"code","source":"# Check your answer (Run this code cell to receive credit!)\nq_2.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:20:02.290391Z","iopub.execute_input":"2022-07-22T10:20:02.290785Z","iopub.status.idle":"2022-07-22T10:20:02.298703Z","shell.execute_reply.started":"2022-07-22T10:20:02.290753Z","shell.execute_reply":"2022-07-22T10:20:02.297758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 3: Getting Rich With Cryptocurrencies?\n\nYou saved Nike so much money that they gave you a bonus. Congratulations.\n\nYour friend, who is also a data scientist, says he has built a model that will let you turn your bonus into millions of dollars. Specifically, his model predicts the price of a new cryptocurrency (like Bitcoin, but a newer one) one day ahead of the moment of prediction. His plan is to purchase the cryptocurrency whenever the model says the price of the currency (in dollars) is about to go up.\n\nThe most important features in his model are:\n- Current price of the currency\n- Amount of the currency sold in the last 24 hours\n- Change in the currency price in the last 24 hours\n- Change in the currency price in the last 1 hour\n- Number of new tweets in the last 24 hours that mention the currency\n\nThe value of the cryptocurrency in dollars has fluctuated up and down by over $\\$$100 in the last year, and yet his model's average error is less than $\\$$1. He says this is proof his model is accurate, and you should invest with him, buying the currency whenever the model says it is about to go up.\n\nIs he right? If there is a problem with his model, what is it?","metadata":{}},{"cell_type":"code","source":"# Check your answer (Run this code cell to receive credit!)\nq_3.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:20:25.848519Z","iopub.execute_input":"2022-07-22T10:20:25.848939Z","iopub.status.idle":"2022-07-22T10:20:25.859368Z","shell.execute_reply.started":"2022-07-22T10:20:25.848898Z","shell.execute_reply":"2022-07-22T10:20:25.858011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 4: Preventing Infections\n\nAn agency that provides healthcare wants to predict which patients from a rare surgery are at risk of infection, so it can alert the nurses to be especially careful when following up with those patients.\n\nYou want to build a model. Each row in the modeling dataset will be a single patient who received the surgery, and the prediction target will be whether they got an infection.\n\nSome surgeons may do the procedure in a manner that raises or lowers the risk of infection. But how can you best incorporate the surgeon information into the model?\n\nYou have a clever idea. \n1. Take all surgeries by each surgeon and calculate the infection rate among those surgeons.\n2. For each patient in the data, find out who the surgeon was and plug in that surgeon's average infection rate as a feature.\n\nDoes this pose any target leakage issues?\nDoes it pose any train-test contamination issues?","metadata":{}},{"cell_type":"code","source":"# Check your answer (Run this code cell to receive credit!)\nq_4.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:21:06.005483Z","iopub.execute_input":"2022-07-22T10:21:06.005924Z","iopub.status.idle":"2022-07-22T10:21:06.014176Z","shell.execute_reply.started":"2022-07-22T10:21:06.005885Z","shell.execute_reply":"2022-07-22T10:21:06.013392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 5: Housing Prices\n\nYou will build a model to predict housing prices.  The model will be deployed on an ongoing basis, to predict the price of a new house when a description is added to a website.  Here are four features that could be used as predictors.\n1. Size of the house (in square meters)\n2. Average sales price of homes in the same neighborhood\n3. Latitude and longitude of the house\n4. Whether the house has a basement\n\nYou have historic data to train and validate the model.\n\nWhich of the features is most likely to be a source of leakage?","metadata":{}},{"cell_type":"code","source":"# Fill in the line below with one of 1, 2, 3 or 4.\npotential_leakage_feature = 2\n\n# Check your answer\nq_5.check()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:22:45.178711Z","iopub.execute_input":"2022-07-22T10:22:45.179134Z","iopub.status.idle":"2022-07-22T10:22:45.188413Z","shell.execute_reply.started":"2022-07-22T10:22:45.179097Z","shell.execute_reply":"2022-07-22T10:22:45.187610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#q_5.hint()\nq_5.solution()","metadata":{"execution":{"iopub.status.busy":"2022-07-22T10:22:31.746274Z","iopub.execute_input":"2022-07-22T10:22:31.746664Z","iopub.status.idle":"2022-07-22T10:22:31.757031Z","shell.execute_reply.started":"2022-07-22T10:22:31.746634Z","shell.execute_reply":"2022-07-22T10:22:31.755582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion\nLeakage is a hard and subtle issue. You should be proud if you picked up on the issues in these examples.\n\nNow you have the tools to make highly accurate models, and pick up on the most difficult practical problems that arise with applying these models to solve real problems.\n\nThere is still a lot of room to build knowledge and experience. Try out a [Competition](https://www.kaggle.com/competitions) or look through our [Datasets](https://kaggle.com/datasets) to practice your new skills.\n\nAgain, Congratulations!","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/intermediate-machine-learning/discussion) to chat with other learners.*","metadata":{}}]}