{"id":4322,"date":"2020-10-15T15:02:04","date_gmt":"2020-10-15T15:02:04","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/10\/15\/how-to-ace-the-data-science-coding-challenge\/"},"modified":"2020-10-15T15:02:04","modified_gmt":"2020-10-15T15:02:04","slug":"how-to-ace-the-data-science-coding-challenge","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/10\/15\/how-to-ace-the-data-science-coding-challenge\/","title":{"rendered":"How to ace the data science coding challenge"},"content":{"rendered":"<div id=\"post-\">\n<p><img class=\"aligncenter size-full wp-image-117579\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Fig1-Tayo-ace-data-science-coding-challenge.jpg\" alt=\"\" width=\"90%\"><\/p>\n<p><em>Image Source: Pexels.<\/em><\/p>\n<h3>The Take-Home Challenge Problem (Coding Exercise)<\/h3>\n<p>\u00a0<\/p>\n<p>So, you\u2019ve successfully gone through the initial screening phase of the interview process. It is now time for the most important step in the interview process, namely, the <strong>take-home coding challenge<\/strong>. This is generally a data science problem, e.g., machine learning model, linear regression, classification problem, time series analysis, etc.<\/p>\n<p>Data science coding projects vary in scope and complexity. Sometimes, the project could be as simple as producing summary statistics, charts, and visualizations. It could also involve building a regression model, classification model, or forecasting using a time-dependent dataset. The project could also be very complex and difficult. In this case, no clear guidance is provided as to the specific type of model to use. In this case, you\u2019ll have to come up with your own model that is best suitable for addressing project goals and objectives.<\/p>\n<p>Generally, the interview team will provide you with project directions and a dataset. If you are fortunate, they may provide a small dataset that is clean and stored in a comma-separated value (CSV) file format. That way, you don\u2019t have to worry about mining the data and transforming it into a form suitable for analysis. For the couple of interviews I had, I worked with 2 types of datasets: one had 160 observations (rows), while the other had 50,000 observations with lots of missing values. The take-home coding exercise clearly differs from companies to companies, as further described below.<\/p>\n<p>In this article, I will share some useful tips from my personal experience that would help you excel in the coding challenge project. Before delving into the tips, let\u2019s first examine some sample coding exercises.<\/p>\n<p>\u00a0<\/p>\n<h3>Sample 1 Coding Exercise: Model for recommending cruise ship crew size<\/h3>\n<p>\u00a0<\/p>\n<blockquote>\n<p><strong><em>Instructions<\/em><\/strong><\/p>\n<p><em>This coding exercise should be performed in python (which is the programming language used by the team). You are free to use the internet and any other libraries. Please save your work in a Jupyter notebook and email it to us for review.<\/em><\/p>\n<p><em>Data file: cruise_ship_info.csv (this file will be emailed to you)<\/em><\/p>\n<p><strong><em>Objective: <\/em><\/strong><em>Build a regressor that recommends the \u201ccrew\u201d size for potential ship buyers. Please do the following steps (hint: use numpy, scipy, pandas, sklearn and matplotlib)<\/em><\/p>\n<ol>\n<li><em> Read the file and display columns.<\/em><\/li>\n<li><em> Calculate basic statistics of the data (count, mean, std, etc) and examine data and state your observations.<\/em><\/li>\n<li><em> Select columns that will probably be important to predict \u201ccrew\u201d size.<\/em><\/li>\n<li><em> If you removed columns, explain why you removed those.<\/em><\/li>\n<li><em> Use one-hot encoding for categorical features.<\/em><\/li>\n<li><em> Create training and testing sets (use 60% of the data for the training and reminder for testing).<\/em><\/li>\n<li><em> Build a machine learning model to predict the \u2018crew\u2019 size.<\/em><\/li>\n<li><em> Calculate the Pearson correlation coefficient for the training set and testing datasets.<\/em><\/li>\n<li><em> Describe hyper-parameters in your model and how you would change them to improve the performance of the model.<\/em><\/li>\n<li><em> What is regularization? What is the regularization parameter in your model?<\/em><\/li>\n<\/ol>\n<p><em>Plot regularization parameter value vs Pearson correlation for the test and training sets, and see whether your model has a bias problem or variance problem.<\/em><\/p>\n<\/blockquote>\n<p>This is an example of a very straightforward problem. The dataset is clean and small (160 rows and 9 columns), and the instructions are very clear. So, all that is needed is to follow the instructions and generate your code. Notice also that the instruction clearly specifies that python must be used as the programming language for model building. The time allowed for completing this coding assignment was three days. Only the final Jupyter notebook has to be submitted, and no formal project report is required.<\/p>\n<p>\u00a0<\/p>\n<h3>Tips for Acing Sample 1 Coding Exercise<\/h3>\n<p>\u00a0<\/p>\n<p>Since the project involves building a machine learning model, the first step is to ensure we understand the machine learning process:<\/p>\n<p><strong><img class=\"aligncenter size-full wp-image-117580\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Fig2-Tayo-ace-data-science-coding-challenge.jpg\" alt=\"\" width=\"90%\"><\/strong><\/p>\n<p><em><strong>Figure 1<\/strong>. Illustrating the Machine Learning Process. Image by Benjamin O. Tayo.<\/em><\/p>\n<p><strong>1. Problem Framing<\/strong><\/p>\n<p><em>Define your project goals. What do you want to find out? Do you have the data to analyze?<\/em><\/p>\n<p><strong>Objective<\/strong><em>:\u00a0<\/em>The goal of this project is to build a regressor model that recommends the \u201ccrew\u201d size for potential cruise ship buyers using the cruise ship dataset\u00a0<a href=\"https:\/\/github.com\/bot13956\/ML_Model_for_Predicting_Ships_Crew_Size\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>cruise_ship_info.csv<\/strong><\/a><strong>.<\/strong><\/p>\n<p><strong>2. Data Analysis<\/strong><\/p>\n<p><em>Import and clean the dataset, analyze features to select the relevant features that correlate with the target variable.<\/em><\/p>\n<p><strong><em>\u00a0<\/em>2.1 Import dataset and display features and the target variable<\/strong><\/p>\n<div>\n<pre>df = pd.read_csv(\"cruise_ship_info.csv\")\r\n\r\ndf.head()\r\n\r\n<\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p><img class=\"aligncenter size-full wp-image-117581\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Fig3-Tayo-ace-data-science-coding-challenge.jpg\" alt=\"\" width=\"90%\"><\/p>\n<p><em><strong>Table 1<\/strong>: Shows first 5 rows of dataset.<\/em><\/p>\n<p>In this example, the dataset is clean and pristine, with no missing values. So, no cleaning is required.<\/p>\n<p><strong>Remarks on Data Quality<\/strong>: One of the major flaws with the dataset is that it does not provide the units for the features. For example, the passenger\u2019s column doesn\u2019t tell if this column is in hundreds or thousands. The units for cabin length, passenger density, and crew are not provided as well. The <em>passenger_density<\/em> feature seems to have been derived from other features, but there is no explanation of how it was derived. These kinds of issues can be addressed by contacting the interview team to ask more about the dataset. It is important to understand the intricacies of your data before using it for building real-world models. Keep in mind that a bad dataset leads to bad predictive models.<\/p>\n<p><strong>2.2 Calculate and visualize the covariance matrix<\/strong><\/p>\n<p>The covariance matrix plot can be used for feature selection and for quantifying the correlation between features (multi-collinearity). We observe from Figure 2 that there are strong correlations between features.<\/p>\n<p><img class=\"aligncenter size-full wp-image-117582\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Fig4-Tayo-ace-data-science-coding-challenge.jpg\" alt=\"\" width=\"90%\"><\/p>\n<p><em><strong>Figure 2<\/strong>. Covariance matrix plot.<\/em><\/p>\n<p>\u00a0<\/p>\n<p><strong>2.3 Perform feature engineering by transforming features into the principal component analysis (PCA) space<\/strong><\/p>\n<p><strong>\u00a0<\/strong>Since the covariance matrix shows multi-collinearity, it is important to transform features into PCA space before training your model. This is important because multi-collinearity between features can lead to a model that is complex and difficult to interpret. PCA can also be used for variable selection and dimensionality reduction. In this case, only components that contribute significantly to the total explained variance can be retained and used for modeled building.<\/p>\n<p><strong>3. Model Building<\/strong><\/p>\n<p><strong>\u00a0<\/strong><em>Pick the machine learning tool that matches your data and desired outcome. Train the model with available data.<\/em><\/p>\n<p><strong>3.1 Model building and evaluation<\/strong><\/p>\n<p><strong>\u00a0<\/strong>Since our goal is to use regression, one could implement different regression algorithms such as\u00a0<strong>Linear Regression (LR)<\/strong>,\u00a0<strong>KNeighbors Regression (KNR)<\/strong>, and\u00a0<strong>Support Vector Regression (SVR)<\/strong>. The dataset has to be divided into training, validation, and test sets.\u00a0Hyperparameter tuning has to be used to fine-tune the model in order to prevent overfitting. Cross-validation is essential to ensure the model performs well on the validation set. After fine-tuning model parameters, the model is applied has to be applied to the test dataset.\u00a0The model\u2019s performance on the test dataset is approximately equal to what would be expected when the model is used for making predictions using unseen data.<\/p>\n<p><strong>3.2 Uncertainty Quantification<\/strong><\/p>\n<p><strong>\u00a0<\/strong>This can be done by training a model using different random partitions of the training dataset, then averaging the cross-validation score for each random state parameter.<\/p>\n<p><img class=\"aligncenter size-full wp-image-117583\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Fig5-Tayo-ace-data-science-coding-challenge.jpg\" alt=\"\" width=\"90%\"><\/p>\n<p><em><strong>Figure 3<\/strong>. Mean cross-validation shows for different regression models.<\/em><\/p>\n<p><strong>4. Application<\/strong><\/p>\n<p><em>Score your final model to generate predictions. Make your model available for production. Retrain your model as needed.<\/em><\/p>\n<p>In this stage, the final machine learning model is selected and put into production. The model is evaluated in a production setting in order to assess its performance. Any mistakes encountered when transforming from an experimental model to its actual performance on the production line has to be analyzed. This can then be used in fine-tuning the original model.<\/p>\n<p>Based on the mean cross-validation score from <strong>Figure 3<\/strong>, we observe that Linear Regression and Support Vector Regression perform almost at the same level and better than KNeighbors Regression. So, the final model selected could either be Linear Regression or Support Vector Regression.<\/p>\n<p><strong>For a complete solution of sample 1 coding exercise, please see the following links:<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/bot13956\/ML_Model_for_Predicting_Ships_Crew_Size\" target=\"_blank\" rel=\"noopener noreferrer\"><em>Sample 1 recommended solution<\/em><\/a><\/p>\n<p><a href=\"https:\/\/medium.com\/swlh\/machine-learning-process-tutorial-222327f53efb\" target=\"_blank\" rel=\"noopener noreferrer\"><em>Machine Learning Process Tutorial<\/em><\/a><\/p>\n<p><strong>Remarks on Sample 1 Coding Exercise<\/strong><\/p>\n<p>Sometimes the coding exercise would ask you to submit a Jupyter notebook only, or it may ask for a full project report. Make sure your Jupyter notebook is well organized to reflect every stage of the machine learning process. A sample Jupyter notebook can be found here<em>: <\/em><a href=\"https:\/\/github.com\/bot13956\/ML_Model_for_Predicting_Ships_Crew_Size\" target=\"_blank\" rel=\"noopener noreferrer\">ML_Model_for_Predicting_Ships_Crew_Size<\/a>.<\/p>\n<p>\u00a0<\/p>\n<h3>Sample 2 Coding Exercise: Model for forecasting loan status<\/h3>\n<p>\u00a0<\/p>\n<blockquote>\n<p><strong><em>Instructions<\/em><\/strong><\/p>\n<p><em>In this problem, you will forecast the outcome of a portfolio of loans. Each loan is scheduled to be repaid over 3 years and is structured as follows:<\/em><\/p>\n<ul>\n<li><em>First, the borrower receives the funds. This event is called origination.<\/em><\/li>\n<li><em>The borrower then makes regular repayments until one of the following happens:<\/em><\/li>\n<\/ul>\n<p><em>(i) The borrower stops making payments, typically due to financial hardship, before the end of the 3-year term. This event is called charge-off, and the loan is then said to have charged off.<\/em><\/p>\n<p><em>(ii) The borrower continues making repayments until 3 years after the origination date. At this point, the debt has been fully repaid.<\/em><\/p>\n<p><em>In the attached CSV, each row corresponds to a loan, and the columns are defined as follows:<\/em><\/p>\n<ul>\n<li><em>The column with header days since origination indicates the number of days that elapsed between origination and the date when the data was collected.<\/em><\/li>\n<li><em>For loans that charged off before the data was collected, the column with header days from origination to charge-off indicates the number of days that elapsed between origination and charge-off. For all other loans, this column is blank.<\/em><\/li>\n<\/ul>\n<p><strong><em>Objective<\/em><\/strong><em>: We would like you to estimate what fraction of these loans will have charged off by the time all of their 3-year terms are finished. Please include a rigorous explanation of how you arrived at your answer, and include any code you used. You may make simplifying assumptions, but please state such assumptions explicitly. Feel free to present your answer in whatever format you prefer; in particular, PDF and Jupyter Notebook are both fine. Also, we expect that this project will not take more than 3\u20136 hours of your time.<\/em><\/p>\n<\/blockquote>\n<p>The dataset here is complex (has 50,000 rows and 2 columns, and lots of missing values), and the problem is not very straightforward. You have to examine the dataset critically and then decide what model to use. This problem was to be solved in a week. It also specifies that a formal project report and an R script or Jupyter notebook file be submitted.<\/p>\n<p>\u00a0<\/p>\n<h3>Tips for Acing Sample 2 Coding Exercise<\/h3>\n<p>\u00a0<\/p>\n<p>As in Sample 1 coding exercise, you need to follow the machine learning steps when tackling this problem. This particle problem does not have a unique solution. I attempted a solution using probabilistic modeling based on Monte-Carlo simulation.<\/p>\n<p>For a complete solution of sample 1 coding exercise, please see the following links:<\/p>\n<p><a href=\"https:\/\/github.com\/bot13956\/Monte_Carlo_Simulation_Loan_Status\" target=\"_blank\" rel=\"noopener noreferrer\"><em>Sample 2 recommended solution<\/em><\/a><\/p>\n<p><a href=\"https:\/\/medium.com\/towards-artificial-intelligence\/r-script-for-data-science-coding-exercise-633800dd63ec\" target=\"_blank\" rel=\"noopener noreferrer\"><em>R Script for Data Science Coding Exercise<\/em><\/a><\/p>\n<p><a href=\"https:\/\/towardsdatascience.com\/project-report-for-data-science-coding-exercise-9a9c76a09be8\" target=\"_blank\" rel=\"noopener noreferrer\"><em>Project Report for Data Science Coding Exercise<\/em><\/a><\/p>\n<p><strong>Remarks on Sample 2 Coding Exercise<\/strong><\/p>\n<p>The solutions presented above are recommended solutions only. Keep in mind that the solution to a data science or machine learning project is not unique. I challenge you to solve these problems before reviewing the sample solutions.<\/p>\n<p>\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>\u00a0<\/p>\n<p>In summary, we\u2019ve discussed some useful tips that could be beneficial for any data science aspirant currently applying for data science openings. The coding exercise varies in scope and complexity, depending on the company you are applying to. The take-home coding exercise provides an excellent opportunity for you to showcase your ability to work on a data science project. You need to use this opportunity to demonstrate exceptional abilities in your understanding of data science and machine learning concepts. Don\u2019t let this wonderful opportunity slip away. If there are certain aspects of the project that you don\u2019t understand, feel free to reach out to the data science interview team if you have questions. They may provide some hints or clues.<\/p>\n<p><b>Related:<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.kdnuggets.com\/2020\/10\/ace-data-science-coding-challenge.html<\/p>\n","protected":false},"author":0,"featured_media":4323,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/4322"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=4322"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/4322\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/4323"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=4322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=4322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=4322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}