<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[vickdata - Medium]]></title>
        <description><![CDATA[Sharing my journey in Data Science - Medium]]></description>
        <link>https://medium.com/vickdata?source=rss----a4d081aa8b34---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>vickdata - Medium</title>
            <link>https://medium.com/vickdata?source=rss----a4d081aa8b34---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 14:59:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/vickdata" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Highlights from an Analytics and AI in Travel Conference]]></title>
            <link>https://medium.com/vickdata/highlights-from-an-analytics-and-ai-in-travel-conference-d691e756f564?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/d691e756f564</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[travel]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Fri, 29 Mar 2019 17:11:09 GMT</pubDate>
            <atom:updated>2019-03-29T17:11:09.270Z</atom:updated>
            <content:encoded><![CDATA[<h4>In March I travelled to the wonderful city of San Francisco for this conference here are the highlights from the two days</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*clbd23hX89PJ4Yox_captA.jpeg" /><figcaption>View from the Hilton Cityscape Lounge</figcaption></figure><p>Analytics and AI in Travel North America Summit, a conference organised by eye for travel, took place for the first time in San Francisco this year. The summit promised to bring “the Silicon Valley of travel together with analytics leaders”, and was heavily focussed on sharing innovations in personalisation, prediction and reducing data fragmentation across the travel industry. The following post covers some of the highlights from the show:</p><h4>Organising data science teams</h4><p>Many of the talks touched on both how to scale and organise data science teams in your organisation. The universal opinion on this was that the data science team must not exist in a silo and should be integrated into cross functional teams. A specific use case and a sponsor must be available up front in order for the models to be successfully implemented. The result of not having this structure is that models are often either not used, or need heavy refactoring to fit the operational use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QvbrB99KgXxg8DVP" /><figcaption>Brooks Bell model for cross functional teams</figcaption></figure><h4>Recommendation and personalisation approaches</h4><p>There were some common themes around how to successfully use data to make recommendations and create personalised experiences. The need to develop fast through iterative experimentation was a common theme. Many of the talks suggested that personalisation should start with small proof of concept experiments starting with best customers first. An interesting point was around the cold start problem in machine learning — where you do not yet have labelled data. It was suggested that a solution to this problem is to start with basic assumptions, start making recommendations or tailored offers, and learn from the data generated to create profiles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s256CReEsVfH-OhU" /><figcaption>Wyndham Destinations discussed their approach to recommendations</figcaption></figure><h4>AI impact on travel</h4><p>There was some discussion around how AI is enhancing both customer and employee experiences in the travel sector both through automation and personalisation. Tata Crocombe, Managing Director of Aitutaki Lagoon Private Island Resort, gave a talk about how he sees AI being used in hotel operational automation. Examples included automating some of the more routine duties in night auditing and applying AI to understand the requirements of stakeholders. Ultimately the main takeaway from this was that we can automate the “boring work” with AI so that “humans can do the fun stuff”.</p><p>Overall I found this conference extremely useful mainly from an execution strategy perspective rather than technical implementation of AI and machine learning. It was also interesting to hear about how other travel companies are using these technologies. The conference was reasonably small, I think there were around 150 people there, and this size setting meant that I got to meet and talk to some really great people too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d691e756f564" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/highlights-from-an-analytics-and-ai-in-travel-conference-d691e756f564">Highlights from an Analytics and AI in Travel Conference</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Career Paths to Data Science]]></title>
            <link>https://medium.com/vickdata/career-paths-to-data-science-295844db83b2?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/295844db83b2</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[career-paths]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Fri, 22 Feb 2019 16:25:40 GMT</pubDate>
            <atom:updated>2019-02-22T16:22:58.041Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are the different entry routes for a career in data science?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2RfR8j5WGBfL4yiLcu7_w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/aoN3HWLbhdI?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Burst</a> on <a href="https://unsplash.com/search/photos/paths?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The role of a data scientist requires a broad set of skills. Ideally you need a sound knowledge of maths and statistics. Good programming skills in at least one language. A solid understanding of machine and deep learning, and ideally some domain knowledge, and/or business acumen.</p><p>In the following article I am going to describe a number of different routes to move into a data science career. This post will focus on an applied data science role within a business or other large scale enterprise, as opposed to something like a research data science role.</p><h4>Data analyst to data scientist</h4><p>I am going to be biased and list out my own personal route first. I transitioned over the period of about three years from a data analyst role to data scientist. Having spent time in a data analyst role you will already have a really good foundation of understanding around data. How to extract it, clean it, some of the gotchas that you may experience when pulling data together for reporting such as missing or erroneous values. You will also gather an understanding of how to extract insights from data and develop a high level of data curiosity.</p><p>To transition from data analyst to data scientist you will need to develop good programming skills in at least one language. My personal opinion here is that if you are just starting out in programming then python would be the best one to learn. In a poll by<a href="https://www.kdnuggets.com/2018/05/poll-tools-analytics-data-science-machine-learning-results.html"> KDNuggets</a> Analytics in 2018, 65.6% of data scientists surveyed selected python as a tool that they used, up 11% on the same survey carried out in 2017. So python is currently the most popular language for data scientists and this popularity is continuing to grow. Python is highly accessible and fairly easy to learn for beginners in programming. It also has a very active community around it in the data science space, examples include <a href="https://numfocus.org/">NumFocus</a> and <a href="https://pydata.org/">Pydata</a>.</p><p>Chances are that, unless you have already have a highly numerate degree, you will also need to brush up on your knowledge of maths and statistics. You will need to have an enough of an understanding to perform complex data analyses, and have a reasonable understanding of what is going on behind machine learning algorithms for example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-K4bpH1IXEua6hcshfESw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/hpjSkU2UYSU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Carlos Muza</a> on <a href="https://unsplash.com/search/photos/data-analyst?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h4>Graduate degree in maths/engineering/computer science</h4><p>Chances are that if you have an advanced degree in one of these subjects you will already have a lot of the required skills for data science. You might need to learn programming if you don’t already have this, or more maths and statistics if your degree was computer science based for example.</p><p>The learning curve here is likely to be adopting typical business practices such as writing reproducible code, version control and corporate working methodologies e.g. Agile. Developing domain knowledge of the industry you are working within and interpreting, sometimes vague, business questions into useful projects.</p><h4>Software engineering</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmBD9OaRAJPnBYBoZwyZMw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/pjAH2Ax4uWk?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Charles 🇵🇭</a> on <a href="https://unsplash.com/search/photos/data?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I personally think this a great role to transition from. From my experience, data science teams often lack the knowledge of software development best practices, and being able to write the most efficient, production ready code. If you have already spent time working as a software engineer, then the chances are you will have a solid foundation in the sort of programming that data science teams often lack.</p><p>For a transition here you may need to learn one of the popular data science programming languages — as mentioned previously probably python, as well as SQL. You would need to develop a solid understanding of data, extracting, cleansing, processing and analysing. A foundational knowledge of maths and statistics, especially as applied to data analysis and machine/deep learning, is also needed.</p><h4>Other routes</h4><p>I am hugely passionate about diversity in tech and I believe very much that this also includes diversity of backgrounds. Not coming from a “traditional” route can have its advantages, your experience or outlook on life may give a fresh perspective on processes or problems within a team. I have listed three of the most common routes for entering a career in data science in this article, however there are of course many more. If you don’t have a relevant degree, or experience in data, or programming, I don’t believe that this is a barrier, it just means that it may take you a little longer to get there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=295844db83b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/career-paths-to-data-science-295844db83b2">Career Paths to Data Science</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Predicting Animal Shelter Outcomes]]></title>
            <link>https://medium.com/vickdata/predicting-animal-shelter-outcomes-4c5fad5dbb4f?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/4c5fad5dbb4f</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Mon, 18 Feb 2019 20:21:12 GMT</pubDate>
            <atom:updated>2019-02-18T20:19:30.065Z</atom:updated>
            <content:encoded><![CDATA[<h4>A guide to handling categorical variables in supervised machine learning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DaJm04Uu_OuxTJqBglXbeQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/ngqyo2AYYnE?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Berkay Gumustekin</a> on <a href="https://unsplash.com/search/photos/dog?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I have been working quite a lot recently with encoding categorical variables in Python for machine learning. I wanted to write a post covering some of the things that I have learnt along the way. Kaggle have a data set from the Austin Animal Center in which almost all variables are categorical. The following post covers some examples of how we might process these into features that will be useful in a predictive model.</p><p>The data set can be downloaded <a href="https://www.kaggle.com/c/shelter-animal-outcomes">here</a>, I am using the train.csv file in my workflow. The data comprises a number of characteristics about animals that were admitted to the Austin Animal Center between 1st October 2013 and March 2016. You are asked to predict the outcome for each animal of which there are 5 possibilities;</p><ul><li>Adoption</li><li>Transfer</li><li>Return to owner</li><li>Euthanasia</li><li>Died</li></ul><h4>Basic data exploration</h4><p>Firstly I have read in the train.csv file and produced a simple bar plot to visualise the distribution for the outcome types.</p><pre>import pandas as pd<br>import matplotlib.pyplot as plt<br>% matplotlib inline</pre><pre>train = pd.read_csv(&#39;train.csv&#39;)<br>train[&#39;OutcomeType&#39;].value_counts().plot.bar()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*qdaqOB_8pHzqSXEaNvJXlQ.png" /></figure><p>We can see that there are differences in class sizes, in particular, the classes died and euthanasia are, thankfully, much smaller than the others. This could prove difficult to predict these outcomes without handling the imbalances. However, I am not going to cover that here as I would like to focus more on processing the variables.</p><pre>train.dtypes</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/189/1*Cv7rY7TeQWPRNwDfNziG4Q.png" /></figure><p>You can see from the above that all columns in the data are non-numeric. Each one will need some pre-processing performed before I can use them in a classifier.</p><p>In order to get an idea of how I might process them I run the following to get a count of unique values in each column. We can see from the below that some columns, such as “Breed” have a large number of unique values. These will need some more attention in comparison to others.</p><pre>columns = train.columns<br>for column in columns:<br>    print(column)<br>    print(train[column].nunique())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*iK2xpcK-acO5_QRj8Jq6kA.png" /></figure><h4>Processing categorical data</h4><p>Generally there are three main ways to convert categorical data into numeric. These are as follows:</p><h4>1. One hot encoding</h4><p>One hot encoding takes categorical data and makes new columns for each unique value. A 0 or 1 denotes wether or not the row has that value. Lets look at an example using our animal shelter data. Take the column “AnimalTtype”, in this column we have two unique values, cat and dog. When we apply a one hot encoder transformation, two new columns are produced, each containing 1’s and 0’s. This technique works well for columns such as this where the data is of low cardinality. But if the columns are high cardinality or the order of the categories has a meaning, that can be interpreted in a numerical fashion, then more complex approaches should be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*2kSMVGcjzZvoB7I_negqBg.jpeg" /><figcaption>In one hot encoding a new column is produced for each unique value</figcaption></figure><h4>2. Label encoding</h4><p>Label encoding simply maps the unique values to numbers. For example, if we were to apply this to the “SexuponOutcome” column, then each unique value would become a number that could be mapped back to the string representation. This approach however has significant drawbacks for handling categorical variables. The problem is that with categorical data there isn’t actually a relationship between the rows, but if we simply convert it to numbers, the classifier may infer relationships that are not present. For example, if “Neutered Male” was given 1 and “Spayed Female’” 2, the classifier may infer that ‘Spayed Female’ is more important because it has a larger number.</p><h4>3. Converting to ordinal or continuous variables</h4><p>You may find that some categorical columns actually have a numerical meaning. In this data set that is true for the <br>AgeuponOutcome feature, where a higher value should be treated as being greater than others. This isn’t really categorical data, and therefore should be transformed into the numerical representation of the age. I will discuss the method I used for this later on in the post.</p><h4>Missing data</h4><p>Before processing the variables I am going to do a little cleaning of the data. I need to check for any missing values and handle them appropriately. Firstly I run the following code to check for missing values.</p><pre>train.apply(lambda x: sum(x.isnull()/len(train)))</pre><p>This gives the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/1*RoqNJ7uWthtpXTM9lwYP9g.png" /></figure><p>You can see that we have a high number of missing values for two of the features, “Name” and “OutcomeSubtype”. But only a small number of missing values for “SexuponOutcome” and “AgeuponOutcome”. I am therefore going to have to use a number of different methods for handling this missing data.</p><p>Looking at the data I can see that the “OutcomeSubtype” is a further categorisation of the label we are trying to predict. Therefore in a real world use case for this predictive model it is unlikely that we would have this feature in the data. I am therefore going to drop this from my features.</p><pre>train = train.drop(&#39;OutcomeSubtype&#39;, axis=1)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*hl0l0XDXwVBFjZUZ_DoOCg.png" /></figure><p>Missing data in the “Name” field could well turn out to have some impact on the outcome of an animal. Particularly in an outcome such as “return to owner” so rather than seek to fill the missing data with a value I will instead turn it into a new feature “has_name”. This will have a 0 where a name is missing and a 1 where it is present. To do this I fill all missing data with a 0 in the “Name” column. I then use this to create a new column called “has_name”. Finally I drop the “Name” column as this is unlikely to be useful in the final model.</p><pre>train[&#39;Name&#39;] = train[[&#39;Name&#39;]].fillna(value=0)<br>train[&#39;has_name&#39;] = (train[&#39;Name&#39;] != 0).astype(&#39;int64&#39;)<br>train = train.drop(&#39;Name&#39;, axis=1)</pre><p>Finally I will handle the missing values in “SexuponOutcome” and “AgeuponOutcome”. As these are both categorical and have only a small amount of missing data the simplest method will be to fill with the most commonly occurring value. The following code does this.</p><pre>train = train.apply(lambda x:x.fillna(x.value_counts().index[0]))<br>train.apply(lambda x: sum(x.isnull()/len(train)))</pre><p>We can see from the output that we now have no missing values and we have created a new feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*oXJq9gKNAwI030SsVqP5tQ.png" /></figure><p>Finally I am going to drop the AnimalID column as that will not be useful in the model.</p><pre>train = train.drop(&#39;AnimalID&#39;, axis=1)</pre><h4>High cardinality</h4><p>As previously described the most sensible method to convert categorical variables into numerical data is with one hot encoding. However, there is a problem when you have a feature that has high cardinality, or in other words has a large number of unique values. We have two examples of this in our data set — “Breed” and “Color”. If we were to simply use one hot encoding for these features we would end up creating 1,380 new features from “Breed” and 366 from “Color”. This would be unlikely to create a good performing model.</p><p>There are a number of ways to handle this situation and I will talk through them in the next section.</p><h4>1. Most popular values</h4><p>One way to handle this is to encode each of the most commonly occurring values, and place the remaining values into a label called “other”. Lets take “Color” as an example — we can run the code below which takes any values occurring fewer than 300 times and places them into the “other” label. We use this to create a new feature called “top_colors”.</p><pre>color_counts = train[&#39;Color&#39;].value_counts()<br>color_others = set(color_counts[color_counts &lt; 300].index)</pre><pre>train[&#39;top_colors&#39;] = train[&#39;Color&#39;].replace(list(color_others), &#39;Others&#39;)<br>print(train[&#39;top_colors&#39;].nunique())</pre><p>This reduces the number of unique values in the column from 366 to 28. This is a much more manageable number for one hot encoding.</p><h4>2. New features</h4><p>Another method is to create a meaningful categorisation of the values. I will give an example of this here, with the “Breed” column. We can see from the data that some of the values contain the word “Mix” whilst others don’t. We can assume with some limited knowledge of animals that this suggests that mix means that the breed is a cross-breed. This could prove to be a useful feature so we can use the code below to create this.</p><pre>import re<br>train[&#39;breed_type&#39;] = train.Breed.str.extract(&#39;({})&#39;.format(&#39;|&#39;.join([&#39;Mix&#39;])), <br>                        flags=re.IGNORECASE, expand=False).str.lower().fillna(&#39;pure&#39;)</pre><p>I also did something similar with the “Color”. Creating a new feature called “multi_colors” containing a categorisation as to wether the animal has a single colour or not.</p><pre>train[&#39;multi_colors&#39;] = train[&#39;Color&#39;].apply(lambda x : 1 if &#39;/&#39; in x else 0)</pre><h4>3. Numerical representation</h4><p>In this data set the “AgeuponOutcome” feature is not really categorical data. The order of the values has a meaning. Therefore the best way to handle this is to convert it into its numerical representation. I am going to convert this feature into the age in days. I found this fantastic function in this <a href="https://www.kaggle.com/akerlyn/prediction-with-random-forest">Kaggle kernel</a> which does this perfectly. This code creates a new feature and drops the original column.</p><pre>def age_converter(row):<br>    age_string = row[&#39;AgeuponOutcome&#39;]<br>    [age,unit] = age_string.split(&quot; &quot;)<br>    unit = unit.lower()<br>    if(&quot;day&quot; in unit):<br>        if age==&#39;0&#39;: return 1<br>        return int(age)<br>    if(&quot;week&quot; in unit):<br>        if(age)==&#39;0&#39;: return 7<br>        return int(age)*7<br>    elif(&quot;month&quot; in unit):<br>        if(age)==&#39;0&#39;: return 30<br>        return int(age) * 4*7<br>    elif(&quot;year&quot; in unit):<br>        if(age)==&#39;0&#39;: return 365<br>        return int(age) * 4*12*7</pre><pre>train[&#39;age_numeric&#39;] = train.apply(age_converter, axis=1)<br>train = train.drop(&#39;AgeuponOutcome&#39;, axis=1)</pre><p>The remaining columns for the first iteration of this model can be directly converted using one hot encoding. I am using pandas get_dummies to convert all categorical variables in the code below.</p><pre>train = train.drop([&#39;Breed&#39;,&#39;Color&#39;, &#39;DateTime&#39;], axis=1)</pre><pre>numeric_features = train.select_dtypes(include=[&#39;int64&#39;, &#39;float64&#39;]).columns<br>categorical_features = train.select_dtypes(include=[&#39;object&#39;]).drop([&#39;OutcomeType&#39;], axis=1).columns</pre><pre>dummy_columns = pd.get_dummies(train[categorical_features])</pre><pre>final_train = pd.concat([dummy_columns, train],axis=1)</pre><pre>final_train = final_train.drop([&#39;AnimalType&#39;, &#39;breed_type&#39;, &#39;SexuponOutcome&#39;, &#39;top_colors&#39;], axis=1)</pre><p>We now have fully numerical data now lets see how this performs in a classifier.</p><h4>Training a classifier</h4><p>In following code I am specifying the features X and target y, and using scikit-learn train_test_split to create the training and testing data.</p><pre>X = final_train.drop(&#39;OutcomeType&#39;, axis=1)<br>y = final_train[&#39;OutcomeType&#39;]</pre><pre>from sklearn.model_selection import train_test_split</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=1)</pre><p>I am then using a simple RandomForestClassifier to find out roughly how the model will perform.</p><pre>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.metrics import accuracy_score, log_loss</pre><pre>clf = RandomForestClassifier(n_estimators=100, max_depth=2,<br>                            random_state=0)<br>rf_model = clf.fit(X_train, y_train)<br>y_pred = clf.predict(X_test)<br>print(accuracy_score(y_test,y_pred))</pre><pre>y_prob = rf_model.predict_proba(X_test)<br>print(log_loss(y_test, y_prob))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*Pz3s4kL2YWm2GxvmP6DTTQ.png" /></figure><p>This model does not perform particularly well, so it looks like there is more work to be done on feature engineering, model selection or optimisation. I am not going to cover this in depth here but as one final thing lets take a peek at the feature importances for this model. This may inform any further feature transformations or engineering work.</p><pre>import numpy as np<br>features=X.columns<br>importances = rf_model.feature_importances_<br>indices = np.argsort(importances)</pre><pre>plt.figure(figsize=(10,20))<br>plt.title(&#39;Feature Importances&#39;)<br>plt.barh(range(len(indices)), importances[indices], color=&#39;b&#39;, align=&#39;center&#39;)<br>plt.yticks(range(len(indices)), features[indices])<br>plt.xlabel(&#39;Relative Importance&#39;)<br>plt.show</pre><p>The output show that features relating to the presence or absence of a name, age, sex and animal type are of particular importance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*1wggpHvj568ZArhy8U5DVg.png" /></figure><p>There is likely to be a lot more work we can do here to improve the performance of this model. For example there are many more possibilities for transforming the categorical variables than I have listed here. As with a lot of data science work much of this is down to trial and error. I hope that in this article I have given a broad introduction into the possibilities of how to handle categorical variables in a machine learning project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c5fad5dbb4f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/predicting-animal-shelter-outcomes-4c5fad5dbb4f">Predicting Animal Shelter Outcomes</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Simple Guide to Scikit-learn Pipelines]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/vickdata/a-simple-guide-to-scikit-learn-pipelines-4ac0d974bdcf?source=rss----a4d081aa8b34---4"><img src="https://cdn-images-1.medium.com/max/2600/1*jit5trmBsttM0DIxOKhizA.jpeg" width="5131"></a></p><p class="medium-feed-snippet">Learn how to use pipelines in a scikit-learn machine learning workflow</p><p class="medium-feed-link"><a href="https://medium.com/vickdata/a-simple-guide-to-scikit-learn-pipelines-4ac0d974bdcf?source=rss----a4d081aa8b34---4">Continue reading on vickdata »</a></p></div>]]></description>
            <link>https://medium.com/vickdata/a-simple-guide-to-scikit-learn-pipelines-4ac0d974bdcf?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/4ac0d974bdcf</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Tue, 05 Feb 2019 19:47:22 GMT</pubDate>
            <atom:updated>2019-07-26T12:56:32.171Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Maths and Statistics, A Complete Roadmap for Learning Data Science — Part 3]]></title>
            <link>https://medium.com/vickdata/maths-and-statistics-a-complete-roadmap-for-learning-data-science-part-3-e1ec585195cf?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/e1ec585195cf</guid>
            <category><![CDATA[math]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[mathematics]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Mon, 21 Jan 2019 20:01:00 GMT</pubDate>
            <atom:updated>2019-01-21T20:01:00.828Z</atom:updated>
            <content:encoded><![CDATA[<h3>Maths and Statistics, A Complete Roadmap for Learning Data Science — Part 3</h3><h4>Key concepts in maths and statistics for data science, and where to learn them.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2hgRTmLIfcXre0hcYp2r6w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/_zsL306fDck?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Antoine Dautry</a> on <a href="https://unsplash.com/search/photos/mathematics?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Maths and statistics form the basis of most data science techniques. As a data scientist you will need a good foundational knowledge of these core concepts in order to understand how to perform exploratory data analysis, forecast future events, or apply machine learning and deep learning well.</p><p>This is part 3 of a series of posts detailing the roadmap I have been using to learn data science. <a href="https://medium.com/vickdata/programming-skills-a-complete-roadmap-for-learning-data-science-part-1-7913b289751b">Part 1</a> covered programming skills, and <a href="https://medium.com/vickdata/data-analysis-a-complete-roadmap-for-learning-data-science-part-2-25f8d1abc1fd">part 2</a> focussed on learning how to perform data analysis. In the following post I am going to list the core mathematical and statistical concepts that I have on my roadmap, as well as some excellent resources I have found to learn them.</p><p>Data science is a broad and complex subject so the range and breadth of knowledge in maths and statistics is wide. I think however there are core areas that should be learned to create the foundation to be able to start to understand the more nuanced mechanics behind the various areas of data science. This roadmap assumes no more than high school level maths knowledge and is designed to give a basic foundation of the key topics.</p><h4>Statistics</h4><p>As a data scientist you might use statistics to summarise and identify patterns in data, design robust experiments or measure the performance of machine/deep learning models.</p><p>Here is a list of the key concepts you need to know:</p><p><a href="https://en.wikipedia.org/wiki/Descriptive_statistics"><strong><em>Descriptive/Summary statistics</em></strong></a></p><ul><li>How to summarise a sample of data</li><li>Different types of distributions</li><li>Skewness, kurtosis, central tendency (e.g. mean, median, mode)</li><li>Measures of dependence, and relationships between variables such as correlation and covariance</li></ul><p><strong><em>Experiment design</em></strong></p><ul><li>Hypothesis testing</li><li>Sampling</li><li>Significance tests</li><li>Randomness</li><li>Probability</li><li>Confidence intervals and two-sample inference</li></ul><p><strong><em>Machine learning</em></strong></p><ul><li>Inference about slope</li><li>Linear and non linear regression</li><li>Classification</li></ul><blockquote><strong>Resources:</strong> I am a big fan of the statistics course on <a href="https://www.khanacademy.org/math/statistics-probability">Khan</a> academy for learning the basics. The <a href="https://www.scipy-lectures.org/packages/statistics/index.html">SciPy lecture notes</a> are another great resource to learn these concepts in Python. I also highly recommend reading the book <a href="http://www.greenteapress.com/thinkstats/">Think Stats</a> — available for free online.</blockquote><h4>Calculus</h4><p><a href="https://en.wikipedia.org/wiki/Calculus">Calculus</a> is defined by Wikipedia as “the mathematical study of continuous change.” In other words calculus can find patterns between functions, for example, in the case of derivatives it can help you to understand how a function changes over time.</p><p>Many machine learning algorithms utilise calculus to optimise the performance of models. If you have studied even a little machine learning you will probably have heard of Gradient descent. This functions by iteratively adjusting the parameter values of a model to find the optimum values to minimise the <a href="https://en.wikipedia.org/wiki/Cost_function">cost function</a>. Gradient descent is a good example of how calculus is used in machine learning.</p><p>What you need to know:</p><p><strong><em>Derivatives</em></strong></p><ul><li>Geometric definition</li><li>Calculating the derivative of a function</li><li>Nonlinear functions</li></ul><p><strong><em>Chain rule</em></strong></p><ul><li>Composite functions</li><li>Composite function derivatives</li><li>Multiple functions</li></ul><p><strong><em>Gradients</em></strong></p><ul><li>Partial derivatives</li><li>Directional derivatives</li><li>Integrals</li></ul><blockquote><strong>Resources:</strong> One of the best resources I have come across to learn these principles is this <a href="https://ml-cheatsheet.readthedocs.io/en/latest/calculus.html#gradients">machine learning cheatsheet</a> which also covers linear algebra, regression, and the maths behind neural networks. I also really love this <a href="https://betterexplained.com/articles/a-gentle-introduction-to-learning-calculus/">blog post</a> that provides a gentle introduction to calculus with practical examples.</blockquote><h4>Linear Algebra</h4><p>Many popular machine learning methods, including XGBOOST, use matrices to store inputs and process data. Matrices alongside vector spaces and linear equations form the mathematical branch known as Linear Algebra. In order to understand how many machine learning methods work it is essential to get a good understanding of this field.</p><p>What you need to learn:</p><p><strong><em>Vectors and spaces</em></strong></p><ul><li>Vectors</li><li>Linear combinations</li><li>Linear dependence and independence</li><li>Vector dot and cross products</li></ul><p><strong><em>Matrix transformations</em></strong></p><ul><li>Functions and linear transformations</li><li>Matrix multiplication</li><li>Inverse functions</li><li>Transpose of a matrix</li></ul><blockquote><strong>Resources: </strong>This <a href="https://www.ritchieng.com/linear-algebra-machine-learning/">blog post</a> by Ritchie Ng covers matrices and vectors really well. If you want a more in depth overview of the field this is a good <a href="http://joshua.smcvt.edu/linearalgebra/book.pdf">free book</a> that gives an extensive coverage of linear algebra.</blockquote><p>In later posts in this series I will be providing a roadmap for other aspects of learning data science including data engineering, and machine learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1ec585195cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/maths-and-statistics-a-complete-roadmap-for-learning-data-science-part-3-e1ec585195cf">Maths and Statistics, A Complete Roadmap for Learning Data Science — Part 3</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Detecting Hate Speech in Tweets: Natural Language Processing in Python for Beginners]]></title>
            <link>https://medium.com/vickdata/detecting-hate-speech-in-tweets-natural-language-processing-in-python-for-beginners-4e591952223?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/4e591952223</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Tue, 15 Jan 2019 12:59:24 GMT</pubDate>
            <atom:updated>2019-02-11T16:02:49.391Z</atom:updated>
            <content:encoded><![CDATA[<h3>Classifying Tweets for Sentiment Analysis: Natural Language Processing in Python for Beginners</h3><h4>Get up and running with some code to perform text classification in Python.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3uApGI6VnEY8Q2-kwvkKA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/gHrEvF7Ugb4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Con Karampelas</a> on <a href="https://unsplash.com/search/photos/twitter?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I recently embarked on a work project to perform sentiment classification on some customer feedback. This was my first time working with text data for machine learning and I found it quite difficult to get started with such a complex area. I wanted to write a guide for anyone else new to natural language processing (NLP) in python to help others get a quick overview of the end to end process without having to delve into all the complexities right at the beginning.</p><p>This post is for anyone that would like to get up and running with some code to perform an NLP task in Python without going into all the details. My preferred learning style is to get something up and running first, and then delve into the nuts and bolts of how it all works. I hope this helps others with a similar learning style.</p><h4>The Data</h4><p>For this post I am going to be using a data set taken from the Analytics Vidhya website, this can be downloaded <a href="https://datahack.analyticsvidhya.com/contest/practice-problem-twitter-sentiment-analysis/">here</a>. This is a nice simple data set to practice on and has the added advantage of being part of an ongoing competition with a leaderboard. So you can quickly get an idea of how well your chosen workflow is performing.</p><p>The dataset consists of a test and train set. The training set comprises a list of 31,962 tweets, a corresponding id and label 0 or 1 for each tweet. The particular sentiment you are asked to identify in this problem is wether or not the tweet is racist or sexist (in which case it will be labelled as 1).</p><p>So to start with I have imported the data sets, and am returning a little information about them.</p><pre>import pandas as pd</pre><pre>train = pd.read_csv(&#39;train.csv&#39;)<br>print(&quot;Training Set:&quot;% train.columns, train.shape, len(train))<br>test = pd.read_csv(&#39;test_tweets.csv&#39;)<br>print(&quot;Test Set:&quot;% test.columns, test.shape, len(test))</pre><h4>Text Cleaning</h4><p>Most text data will likely need some processing in order for the chosen machine learning algorithm to perform well. In this case each text document is a tweet and therefore will contain lots of characters that will not be meaningful to any machine learning algorithm. You can see below from just viewing the first few rows of the data that the tweets contain characters such as #, @ and punctuation marks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*xlQifvIlBb4JdHF3yNip7w.png" /></figure><p>In order to remove these I am using the Python <a href="https://docs.python.org/3/library/re.html">re</a> library, this provides regular expression matching operations. The following function successfully cleans up most of these characters. Additionally this function makes everything lower case.</p><p>This is generally a good idea as many text classification tools rely on counting the occurrences of words. If both upper and lower case versions of the same word are found in the text then the algorithm will count them as different words even though the meaning is the same. Of course this does mean that where the capitalised versions of a word exists, that does have a different meaning. For example the company Apple vs the fruit apple. This could result in poorer performance for some data sets. This is one area of NLP where you may try different methods to see how they affect the overall performance of the model.</p><pre>import re</pre><pre>def  clean_text(df, text_field):<br>    df[text_field] = df[text_field].str.lower()<br>    df[text_field] = df[text_field].apply(lambda elem: re.sub(r&quot;(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)|^rt|http.+?&quot;, &quot;&quot;, elem))  <br>    return df</pre><pre>test_clean = clean_text(test, &quot;tweet&quot;)<br>train_clean = clean_text(train, &quot;tweet&quot;)</pre><p>The output after cleaning looks like this. Not perfect but we can see how it performs later in our model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OLfZbxVLRIK4v_6Oix3LbQ.png" /></figure><h4>Handling imbalanced classes</h4><p>If we count the number of tweets for each label we can see that there are a significantly larger number of tweets labelled as 0. In fact only 7% are classified as sexist/racist. This is problematic as if we provide an algorithm with this data there is a high chance that it will default to predicting all labels as 0.</p><p>There are a number of methods you can use to handle this. One approach is to use either upsampling or downsampling. In the case of upsampling we use a function that repeatedly takes samples, <strong>with</strong> replacement, from the minority class until the class is the same size as the majority. With replacement means that the same sample can be used multiple times.</p><pre>from sklearn.utils import resample</pre><pre>train_majority = train_clean[train_clean.label==0]<br>train_minority = train_clean[train_clean.label==1]</pre><pre>train_minority_upsampled = resample(train_minority, <br>                                 replace=True,    <br>                                 n_samples=len(train_majority),   <br>                                 random_state=123)<br>train_upsampled = pd.concat([train_minority_upsampled, train_majority])<br>train_upsampled[&#39;label&#39;].value_counts()</pre><p>When downsampling we use the same function to take samples from the majority class, <strong>without </strong>replacement, until it is the same size of the minority class. Without replacement means that each sample is only sampled once.</p><pre>train_majority = train_clean[train_clean.label==0]<br>train_minority = train_clean[train_clean.label==1]<br> <br>train_majority_downsampled = resample(train_majority, <br>                                 replace=True,  <br>                                 n_samples=len(train_minority),   <br>                                 random_state=123)<br>train_downsampled = pd.concat([train_majority_downsampled, train_minority])<br>train_downsampled[&#39;label&#39;].value_counts()</pre><p>I have included my code for this above. I tried both upsampling and downsampling and achieved a better result with upsampling so I have used that in my workflow.</p><h4>CountVectoriser</h4><p>Machines are not capable of reading text in the same way as humans do. In order for a machine learning algorithm to determine patterns in text it must first be converted into a numeric structure. One of the most common techniques for this is called Bag of Words, or BoW.</p><p>A BoW model splits the words in a piece of text into tokens disregarding grammar and word order. The model also counts the frequency in which a word occurs in the text, and assigns a weight proportional to this frequency. The output is a matrix of term frequencies where each row represents the text and each column a word in the vocabulary.</p><p>Sci-kit learn has a number of built in functions to perform this type of modelling. But for this walkthrough I am going to be using one of the most simple functions which is CountVectoriser. This function works quite well with the default settings so I will use those for the first iteration of my model.</p><h4>TfidfTransformer</h4><p>As mentioned earlier in the post the BoW model which we are using to process the text has three steps. CountVectoriser accomplishes the first two, splitting the words into tokens and counting the frequency. We can use another scikit-learn function called TfidfTransformer to apply the frequency weighting.</p><p>In any text document there will be a number of words that appear very frequently such as I, we, and get. If we were to build a model without weighting these words they would overshadow less frequent words during training. By weighting these high frequency words we can assign, for example, more importance to less frequent but perhaps more useful words.</p><h4>Training</h4><p>For simplicity and reproducibility I am going to use a sci-kit learn pipeline with a SGDClassifier. The below code creates a pipeline object that when used will apply each step to the data.</p><pre>from sklearn.feature_extraction.text import TfidfVectorizer<br>from sklearn.pipeline import Pipeline<br>from sklearn.feature_extraction.text import CountVectorizer<br>from sklearn.feature_extraction.text import TfidfTransformer<br>from sklearn.linear_model import SGDClassifier</pre><pre>pipeline_sgd = Pipeline([<br>    (&#39;vect&#39;, CountVectorizer()),<br>    (&#39;tfidf&#39;,  TfidfTransformer()),<br>    (&#39;nb&#39;, SGDClassifier()),<br>])</pre><p>Before training the model I am splitting the training data into a training and test set.</p><pre>from sklearn.model_selection import train_test_split</pre><pre>X_train, X_test, y_train, y_test = train_test_split(train_upsampled[&#39;tweet&#39;],                                    train_upsampled[&#39;label&#39;],random_state = 0)</pre><p>The code below fits the model to the training data and computes the F1 score.</p><pre>model = pipeline_sgd.fit(X_train, y_train)</pre><pre>y_predict = model.predict(X_test)</pre><pre>from sklearn.metrics import f1_score</pre><pre>f1_score(y_test, y_predict)</pre><p>You will notice above that I am using the upsampled data set. This is not ideal as the model performs vastly better on the training data than on the new test data. For example on the training data I achieved (after some further optimisation) an F1 score of 0.97 but this translated to a score of 0.75 once I made my submission. Downsampling because of the low volume of data for the minority class performed even more poorly. There are other more complex techniques for handling imbalanced classes such as weight balancing but that subject in itself warrants a whole other post.</p><p>Natural language processing is a vastly complex subject and there is so much more that I could cover in this article. My aim here is to give enough information, and code, to get up and running with your first text classification model. The final score is not too bad for a first attempt but there is much scope for improvement. There are many other models besides BoW for processing the text data for example that may work better. There is more work that could be done with the re library to further clean the data, and perhaps the removal of stop words may help to improve the model further.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e591952223" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/detecting-hate-speech-in-tweets-natural-language-processing-in-python-for-beginners-4e591952223">Detecting Hate Speech in Tweets: Natural Language Processing in Python for Beginners</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[10 Free Data Science Books You Must Read in 2019]]></title>
            <link>https://medium.com/vickdata/10-free-data-science-books-you-must-read-in-2019-2d4f32793a51?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/2d4f32793a51</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Sat, 29 Dec 2018 14:03:58 GMT</pubDate>
            <atom:updated>2018-12-31T17:48:05.027Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cbj_BcK5ePD4Mc4TK4sfvQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/2JIvboGLeho?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Susan Yin</a> on <a href="https://unsplash.com/search/photos/library?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>There are so many great resources out there to learn data science and analysis for free. Over the last year I have read quite a few data science books and I wanted to share some of the best here. If you are studying, or practicing data science, and haven’t read these books I really think they are worth adding to your reading list for 2019. Below is a list of the top 10 I have found most useful to me over the last few years that are currently available online.</p><h4>Automate the boring stuff</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*0AK_Q6UZXa5VJ-IXvcSqkw.png" /></figure><p>I really love this <a href="https://automatetheboringstuff.com/">book</a> it is a simple introduction to getting started with python from a practical point of view. Although not a specifically data science related book it includes most of the basic concepts around using python for data science. Including flow control, functions, web scraping, working with csv and json files, and running programs. It is very much aimed at absolute beginners so a great book for getting started with python. As well as step by step instructions for each technique, at the end of each chapter there are also practice questions and problems.</p><h4>Data science at the command line</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*SkxUT_h7U5sR5t0kgI0ang.png" /></figure><p>I started using python for data analysis purely in Jupyter Notebooks. However, over time I found that using the command line enabled me to be much more efficient in my work. For example I can very quickly obtain data, run programs and search through files all by typing commands and pressing enter in the terminal window. This <a href="https://www.datascienceatthecommandline.com/">book</a> is a highly accessible and comprehensive guide to data science at the command line. In each chapter it covers, alongside working examples, how to obtain, clean, explore, model and interpret data via the command line.</p><h4>Think stats</h4><p><a href="https://greenteapress.com/wp/think-stats-2e/">Think Stats 2e</a></p><p>This is a really practical overview of statistics for data science. The <a href="https://greenteapress.com/wp/think-stats-2e/">book</a> uses a data set from the National Institute of Health throughout to explain the core concepts in probability and statistics necessary for data science and analysis. This is another highly practical book, and includes lots of example python code, and simple programs to explain the concepts. This is much more lightweight than a lot of the more theoretical textbooks you may find on this subject, and I found this really suited my learning style.</p><h4>Python data science handbook</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*TSRwuh5K2O6e4OK8XhoQAA.jpeg" /></figure><p>This is a really comprehensive <a href="https://tanthiamhuat.files.wordpress.com/2018/04/pythondatasciencehandbook.pdf">guide</a> to python for data science. This builds from beginner to advanced concepts. There is a chapter on iPython which really made such a difference to my efficiency as a data science practitioner. This book also covers Numpy, data manipulation with Pandas, visualisation methods, and Machine Learning. The Machine Learning chapter in particular is really good, and covers both the practical implementation of the various libraries, and the nuts and bolts of how they work.</p><h4>R for data science</h4><p><a href="https://r4ds.had.co.nz/">R for Data Science</a></p><p>I mainly work in python but I still find it is really useful to have at least a working knowledge of R. I have often found that if a good library for a particular method is not available in python, R usually has one. This <a href="https://r4ds.had.co.nz/">book</a> is a really comprehensive guide to doing data science with R, and covers everything from data visualisation and transformation, to the R workflow, to data modelling.</p><h4>Probabilistic Programming and Bayesian methods for hackers</h4><p><a href="https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers">GitHub - CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers: aka &quot;Bayesian Methods for Hackers&quot;: An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)</a></p><p>In the authors own words this <a href="https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers">book</a> is an attempt to “bridge the gap between Bayesian mathematics and probabilistic programming”, and I believe it does this very well. As with Think Stats it moves away from heavily theoretical textbooks and offers practical use cases for Bayesian inference, and the approach is a computational understanding first, and a mathematical understanding second. It is another python based book with lots of practical examples, and uses predominately the PyMC libraries.</p><h4>Machine learning yearning</h4><p><a href="http://www.mlyearning.org/">Machine Learning Yearning</a></p><p>This <a href="http://www.mlyearning.org/">book</a> has been released in draft by Andrew Ng this year. It is designed to teach data scientists how to structure Machine Learning projects, and set direction for a data science team. It is a good overview of when and how to use Machine Learning, and how to handle the complexities involved in implementing AI in the real world.</p><h4>Ethics and data science</h4><p><a href="https://www.amazon.com/Ethics-Data-Science-Mike-Loukides-ebook/dp/B07GTC8ZN7">Ethics and Data Science</a></p><p>There has been a lot in the news this year relating to bias in machine learning applications, and data protection and privacy concerns. I read this book as I wanted to ensure that I had the required knowledge to practice good data science. This <a href="https://www.oreilly.com/library/view/ethics-and-data/9781492043898/">book</a> covers how to put ethical principles into data science projects. It includes a really good checklist to go through when designing a project as well as lots of suggestions for building ethics into a general data culture. Another resource released this year along very similar lines was the deon command line tool from <a href="http://deon.drivendata.org/">drivendata.org</a>. This tool allows you to build an ethics checklist into data science projects. This is definitely something I will be incorporating into my work in the new year.</p><h4>Deep learning</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/196/1*FFVGuz1ru3hjA3lpuNg6pg.jpeg" /></figure><p>This is an excellent <a href="http://www.deeplearningbook.org/">book</a> now available to read for free online. It covers applied maths for Machine Learning, and has a large emphasis on deep learning in particular. It covers the mathematics behind key concepts in deep learning such as convolutional networks, regularisation and recurrent and recursive nets. It is very much a theory based book but gives a deep level of understanding into the subject. It does also include chapters on the practical implementation of these techniques.</p><h4>Rules for machine learning</h4><p>This is really an <a href="http://martin.zinkevich.org/rules_of_ml/rules_of_ml.pdf">ebook</a>/paper and only about 24 pages long. However, I have to include it here as it is such a great resource and I found it by chance on twitter this year. This covers some best practices from Google in how to implement a machine learning project. It emphasises the importance of data engineering to create great features and a solid data pipeline over machine learning expertise.</p><p>These books have been really useful to me over the last couple of years, I am always amazed at the quantity and quality of free resources available online. I am sure that I will continue to refer back to these into 2019 and beyond, and hopefully find some more brilliant resources to share. Happy New Year!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d4f32793a51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/10-free-data-science-books-you-must-read-in-2019-2d4f32793a51">10 Free Data Science Books You Must Read in 2019</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Enter Your First Machine Learning Competition]]></title>
            <link>https://medium.com/vickdata/how-to-enter-your-first-machine-learning-competition-90e8c48e07ae?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/90e8c48e07ae</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Mon, 17 Dec 2018 14:01:37 GMT</pubDate>
            <atom:updated>2018-12-17T15:03:36.480Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Enter your First Machine Learning Competition</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ebjc6tZABbzz1qAWURYtXA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/zki5BhKRQa0?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">rawpixel</a> on <a href="https://unsplash.com/search/photos/loan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Machine learning competitions can be a really useful way to learn how to validate and improve models. I found that data science practice problems on <a href="http://www.analyticsvidhya.com/">Analytics Vidhya</a> were a great starting point. These allow you access to simple datasets on which to practice your machine learning skills and benchmark yourself against others. I think they offer a great introduction to approaching these problems before perhaps moving onto something a bit more challenging such as Kaggle competitions.</p><p>I initially found it challenging to know how to build a model and make a submission. I decided to write this post to help anyone else who may be starting out and experiencing the same problem.</p><p>The competition I am going to cover in this post is the <a href="https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/">Loan Prediction Practice Problem</a>. In this competition you are asked to create a model to predict whether or not a person would be approved for a home loan, given a number of fields from an online search form. In the following post, I am going to walk through step by step how to get started with building the first model for this task using Python in JupyterLab. I am not going to go into great detail about each step but wanted to show the end-to-end process of how to approach a classification problem.</p><h4>Getting started</h4><p>For this problem, you are given two CSV files, train.csv and test.csv (they actually have longer file names when downloaded but the first step I took was to rename them to make handling them much easier). The data sets can be downloaded <a href="https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/">here</a>. We are going to use the training file to train and test the model, and we then use the model to predict the unknown values in the text file. The first steps are to read in the files and perform some preliminary analysis to determine what pre-processing we will need before attempting to train a model.</p><p>In the below code I am reading in the renamed CSV files using the pandas read_csv function. I have also dropped the Loan_ID column from the train set as I won’t need this for training the model.</p><pre>import pandas as pd</pre><pre>train = pd.read_csv(&#39;train.csv&#39;)<br>test = pd.read_csv(&#39;test.csv&#39;)<br>train = train.drop(&#39;Loan_ID&#39;, axis=1)</pre><p>Once the files have been imported I run the following function to get some more information about the dataset I am going to be working with.</p><pre>def describe_data(df):<br>    print(&quot;Data Types:&quot;)<br>    print(df.dtypes)<br>    print(&quot;Rows and Columns:&quot;)<br>    print(df.shape)<br>    print(&quot;Column Names:&quot;)<br>    print(df.columns)<br>    print(&quot;Null Values:&quot;)<br>    print(df.apply(lambda x: sum(x.isnull()) / len(df)))</pre><pre>describe_data(train)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMB1VGkSoglRYPaU8FAVxg.png" /></figure><p>This gives me the following information about the data set;</p><ul><li>That we have a number of columns that are non numeric which will need some pre-processing before they can be used to build a model.</li><li>That we have a relatively small data set consisting of 614 rows.</li><li>That there are no null values present in the data.</li></ul><h4>Pre-processing</h4><p>Most machine learning models are unable to handle non-numeric columns, and missing values in data. It is therefore necessary to perform a number of pre-processing steps.</p><p>To make the process simpler I am going to be using a scikit-learn pipeline. These pipelines allow you to apply a series of transformative steps, and fit and predict on an estimator with just one line of code. These are highly reusable, cut down on the need to apply pre-processing steps multiple times on train and test sets, and make workflows reproducible and easy to follow.</p><p>Before creating the pipeline I need to specify which columns are numerical features and which are categorical features. I am doing this using the pandas select_dtypes function.</p><pre>numeric_features = train.select_dtypes(include=[&#39;int64&#39;, &#39;float64&#39;]).columns<br>categorical_features = train.select_dtypes(include=[&#39;object&#39;]).drop([&#39;Loan_Status&#39;], axis=1).columns</pre><p>The next step is to create two transformer pipelines, one for the numerical features and one for the categorical features. In each pipeline is a SimpleImputer to handle null values. We don’t actually need this for our dataset as we don’t have any null values but it is good practice to always include one as in a real world example you do not want your prediction to fail because of a null value. For the numeric transformer I am simply imputing the nulls with the median value for the column, and for the categorical tranformer I am imputing them with the value “missing”.</p><p>In the numeric transformer I have also included a scaler function. This ensures that all the values in each feature are on the same scale. For the categorical transformer I am using OneHotEncoder to transform each unique value in the categorical columns into a new column containing a 0 or 1 dependant on wether or not the value is present.</p><pre>from sklearn.impute import SimpleImputer<br>from sklearn.preprocessing import StandardScaler, OneHotEncoder<br>from sklearn.pipeline import Pipeline</pre><pre>numeric_transformer = Pipeline(steps=[<br>    (&#39;imputer&#39;, SimpleImputer(strategy=&#39;median&#39;)),<br>    (&#39;scaler&#39;, StandardScaler())])</pre><pre>categorical_transformer = Pipeline(steps=[<br>    (&#39;imputer&#39;, SimpleImputer(strategy=&#39;constant&#39;, fill_value=&#39;missing&#39;)),<br>    (&#39;onehot&#39;, OneHotEncoder(handle_unknown=&#39;ignore&#39;))])</pre><p>I then use the ColumnTransfomer to concatenate both the numeric and categorical transformers into an object called preprocessor.</p><pre>from sklearn.compose import ColumnTransformer</pre><pre>preprocessor = ColumnTransformer(<br>    transformers=[<br>        (&#39;num&#39;, numeric_transformer, numeric_features),<br>        (&#39;cat&#39;, categorical_transformer, categorical_features)])</pre><h4>Training the model</h4><p>Once you have your transformation pipeline you can combine this with a classifier to create the full machine learning pipeline. This can be used to both train (fit) and predict on new data.</p><p>Before doing this we need to split the train data into a training and test set so that we can get an idea as to how well our model is predicting on new data. To do this I first specify the features (X) and the target (y). I then use the train_test_split function and have chosen to use 20% of the data for the test set.</p><pre>from sklearn.model_selection import train_test_split<br>X = train.drop(&#39;Loan_Status&#39;, axis=1)<br>y = train[&#39;Loan_Status&#39;]</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)</pre><p>As this is a simple walkthrough and I am not showing the full process of obtaining the best performing model I am going to use a simple LogisticRegression model. The below code creates a logistic regression model that performs the defined transformations before fitting or predicting.</p><pre>from sklearn.linear_model import LogisticRegression<br>lr = Pipeline(steps=[(&#39;preprocessor&#39;, preprocessor),<br>                      (&#39;classifier&#39;, LogisticRegression(solver=&#39;lbfgs&#39;))])</pre><p>I then fit the model on the training set, and obtain an accuracy score using the built in score method. This returns a score of 0.82. A glance at the <a href="https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/lb">leaderboard</a> tells us that this is not a bad initial score but there is still some work to do as the top competitors are currently scoring in excess of 0.90.</p><pre>lr.fit(X_train, y_train)<br>print(&quot;model score: %.3f&quot; % lr.score(X_test, y_test))</pre><p>To finish I will show you how to make your first submission. We now need to use the test.csv file that we read in earlier. Firstly we are going to create a new data frame with the Loan_ID column dropped.</p><pre>test_no_id = test.drop(&#39;Loan_ID&#39;, axis=1)</pre><p>We then use our model to predict on this new data.</p><pre>test_predictions = lr.predict(test_no_id)</pre><p>Finally we need to build the submission file, which we know from looking at the sample submissions available on the website, needs to consist of a csv file containing the Loan_ID’s and corresponding predictions. In the below code I have obtained the Loan_ID’s from the original test data frame. I am then building a new data frame by combining with the test_predictions.</p><pre>Loan_ID = test[&#39;Loan_ID&#39;]<br>submission_df_1 = pd.DataFrame({<br>                  &quot;Loan_ID&quot;: Loan_ID, <br>                  &quot;Loan_Status&quot;: test_predictions})</pre><p>Finally exporting this as a csv file using the pandas to_csv function. You can now upload this as a submission.</p><pre>submission_df_1.to_csv(&#39;submission_1.csv&#39;, index=False)</pre><p>Once you have made a submission you will get a score on the website. You can see here that the accuracy score for the submission is much lower than the score on the test set from the training data. This suggests that I have an overfit model which means that the model has learned patterns in the training data that are specific to that dataset and not necessarily applicable on the wider data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W5_WlP8phy4GePtJpgp6bw.png" /></figure><p>In this post I have described how to prepare data, train a simple model and make your first submission to a machine learning competition. In another blog post I will talk through the steps you can take to improve on this score and move further up the leaderboard.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90e8c48e07ae" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/how-to-enter-your-first-machine-learning-competition-90e8c48e07ae">How to Enter Your First Machine Learning Competition</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Google are Democratising AI]]></title>
            <link>https://medium.com/vickdata/how-google-are-democratising-ai-7d47a07a7307?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/7d47a07a7307</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[cloud]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Fri, 02 Nov 2018 10:09:17 GMT</pubDate>
            <atom:updated>2018-11-05T13:04:39.535Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IjGAupLsBYI0yNlHUCRDPQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/JjGXjESMxOY?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Franck V.</a> on <a href="https://unsplash.com/search/photos/machine-learning?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Over the last few months I have been working with the Google Cloud Machine Learning (ML) Engine to deploy machine learning models into production. Google’s vision is to make machine learning and deep learning accessible to all from engineers to analysts to data scientists. Over the last few years they have released a variety of cloud based tools designed to lower the barriers of entry to using Artificial Intelligence (AI).</p><p>My experience so far has been positive ML Engine does seem to abstract a lot of the complexity of deploying models away for example. In the following post I want to review the various tools that Google have or are releasing in an effort to democratise AI.</p><h4>BigQuery ML</h4><p>The primary purpose of BigQuery ML is to make machine learning accessible to data analysts by enabling the user to develop machine learning models with standard sql. Additionally it allows for fast prototyping, by allowing users to run a model directly where the data already sits, the viability of a machine learning project can be assessed with limited time investment. This is currently in a beta release, and only supports three types of models — linear regression, binary logistic regression and multiclass logistic regression.</p><p>It can be used through a number of tools that data analysts will already have extensive knowledge of. These include spreadsheets with a connection to BigQuery, the BigQuery web UI, statistical software and the Google visualisation tools Cloud Datalab and Data Studio.</p><p>I have recently tried using this both in Datalab and the BigQuery web UI and found it very straight forward to get it working. In essence it is as simple as writing a sql query similar to the one shown below. You will need to create the data set in your project first by following the instructions in the <a href="https://cloud.google.com/bigquery/docs/bigqueryml-analyst-start">documentation</a>. Running this extracts the data and trains the model selected in OPTIONS using this data. You can view training statistics and evaluate the performance of the model directly in the web UI which is impressive. You can access predictions via a different sql query.</p><p>My hope is that in the future they will build in support for other open source libraries such as scikit-learn. Currently the limitation of only having regression based models available limits the ability to develop the most accurate models on real world data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bglxy2MJ4-_JHb4qpYUtZg.png" /><figcaption>Query taken from Google Cloud <a href="https://cloud.google.com/bigquery/docs/bigqueryml-analyst-start">documentation</a></figcaption></figure><h4>ML Engine</h4><p>Aimed at both developers and data scientists this solution offers a managed service to train and deploy machine learning models. A number of machine learning libraries are available including scikit-learn, XGBoost, Keras and Tensorflow (more on this later in the post).</p><p>I have recently been trialling using ML Engine to train a classification model with scikit-learn. I have been able to serve the model in a production environment without having to involve engineers so on the surface this appears to be a way to democratise deploying models for data scientists.</p><p>The process we have followed is relatively straightforward. I created a packaged version of the scikit-learn code for training the model and predicting on new data. This was placed on cloud storage. A cli command needs to be run for each part of the process to create, train and then serve the model. You can then send online prediction requests to the served model.</p><h4>Cloud AutoML</h4><p>This product is aimed primarily at developers, enabling those with limited knowledge of machine learning to deploy a number of pre-built models for specific use cases. Current offerings include AutoML Vision for image classification, AutoML Natural Language for text classification (I am currently trialling this for sentiment analysis) and AutoML Translation.</p><p>With these products you import your prepared data from Google Cloud Storage using the UI for the AutoML product you are using. You train the model using options in the UI. The performance is evaluated via the command line, and predictions are accessed with the UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TnLq7K1tMJmAvbtg47du1Q.png" /><figcaption>The AutoML Text Classification UI, <a href="https://cloud.google.com/natural-language/automl/docs/datasets">Google Cloud Documentation</a></figcaption></figure><h4>Tensorflow</h4><p>Tensorflow, created by the Google Brain team is an open source library designed to simplify large scale deep learning. This tool enables both experts and non experts to leverage the learning power of Artificial Neural Networks. In recent years these networks have powered a lot of the growth in advanced AI including the development of self driving cars and facial recognition.</p><p>The Tensorflow library includes a set of pre-made estimators that act as building blocks to use neural networks for classification, regression, as well text and image identification. Similar to other open source libraries for machine learning the code is written in python and follows a similar train, predict and evaluate set of functions. Tensorflow can be also used with ML Engine to serve these models in a production environment.</p><p>I have been fortunate to have access to use these Google tools with data already in BigQuery as part of my job. However for anybody that wants to try them Google offers free tiers for most of these products. Additionally BigQuery has a variety of public datasets that can easily be made available to practice with. Although my use of these tools is limited so far it does seem that they offer a way to take some of the pain away from developing and deploying both machine and deep learning. I am hopeful that with better support for a wider variety of machine learning libraries, and versions, that these tools will open up the use of AI to more job roles, and enterprises.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d47a07a7307" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/how-google-are-democratising-ai-7d47a07a7307">How Google are Democratising AI</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easier Machine Learning with the New Column Transformer from Scikit-Learn]]></title>
            <link>https://medium.com/vickdata/easier-machine-learning-with-the-new-column-transformer-from-scikit-learn-c2268ea9564c?source=rss----a4d081aa8b34---4</link>
            <guid isPermaLink="false">https://medium.com/p/c2268ea9564c</guid>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[data-preprocess]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Rebecca Vickery]]></dc:creator>
            <pubDate>Mon, 01 Oct 2018 16:06:48 GMT</pubDate>
            <atom:updated>2019-07-26T13:26:08.796Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5zIWId9Q6dzxcR_zqBGZw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/vuMTQj6aQQ0?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Samule Sun</a> on <a href="https://unsplash.com/search/photos/transformer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Last week scikit-learn released version 0.20.0, one of the features in this release I am most excited about is the ColumnTransformer. This function allows you to combine several feature extraction or transformation methods into a single transformer. Say, you are working on a machine learning problem, and you have a dataset containing a mixture of categorical and numerical columns. Rather than having to handle each of these separately, and perhaps writing a function to then apply this to new data. These can now be combined into a transformer which can easily be reapplied, and extended.</p><p>This is a part of my machine learning workflow I have been keen to simplify, so over the weekend I took a simple data set I had lying around on my laptop, and had a go at applying this to a classification problem.</p><p>The data set I am using was taken from the Analytics Vidhya loan prediction competition and can be downloaded <a href="https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/#data_dictionary">here</a>. The purpose of this competition is to predict wether or not a loan application will be successful based on a number of customer features. This contains both categorical and numerical variables, and is a nice simple data set to practice using the new ColumnTransformer.</p><h4>Data Preparation</h4><p>To start with I am using pandas to read in both files. Using the dtypes function I can see that there are both numerical and categorical features present in the dataset.</p><pre>import pandas as pd<br>train = pd.read_csv(&#39;train.csv&#39;)<br>test = pd.read_csv(&#39;test.csv&#39;)<br>print(train.shape, test.shape)<br>print(train.dtypes)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/1*S_vnb7J6eAAs8UdWJOeI9Q.png" /></figure><p>Before going further I am going to drop the Loan_ID column as that will not be used in the model. I am also filling any null values with the most commonly occurring value for each column. There are of course a number of methods I could choose for this but as I am just trying out a new function I am not too worried about the accuracy of the model for now.</p><pre>train = train.drop([&#39;Loan_ID&#39;], axis=1)<br>test =  test.drop([&#39;Loan_ID&#39;], axis=1)</pre><pre>train = train.apply(lambda x:x.fillna(x.value_counts().index[0]))<br>test = test.apply(lambda x:x.fillna(x.value_counts().index[0]))</pre><p>Next I’m going to specify the features (X) and target (y). I then use the sklearn train_test_split function to divide the training data into test and train ready to train and validate the model.</p><pre>feature_set = train.drop([&#39;Loan_Status&#39;], axis=1)<br>X = feature_set.columns[:len(feature_set.columns)]<br>y = &#39;Loan_Status&#39;</pre><pre>from sklearn.model_selection import train_test_split</pre><pre>X_train, X_test, y_train, y_test = train_test_split(<br>    train[X], train[y], random_state=0)</pre><h4>ColumnTransformer</h4><p>The next step is to apply transformation to columns to optimise them for use in the classification model. Usually I would write a function that applies these transformations, so that I can re-use it on both the test and train set, and any holdout data I may have for later validation. However, I am going to try using the ColumnTransformer to simplify these steps.</p><p>For simplicity, again I am not too worried about the accuracy of this model, I am going to transform the categorical columns using the sklearn OneHotEncoder. I will also normalize the numerical columns using the Normalizer function.</p><p>The ColumnTransformer takes a list of tuples specifying the transformers, and the corresponding columns on which the transformation needs to be applied. The columns can either be entered as strings specifying the column names in a pandas data frame, or as in the code I have used below, as integers which are interpreted as the column positions.</p><pre>from sklearn.compose import ColumnTransformer<br>from sklearn.preprocessing import Normalizer, OneHotEncoder</pre><pre>colT = ColumnTransformer(<br>    [(&quot;dummy_col&quot;, OneHotEncoder(categories=[[&#39;Male&#39;, &#39;Female&#39;],<br>                                           [&#39;Yes&#39;, &#39;No&#39;],<br>                                            [&#39;0&#39;,&#39;1&#39;, &#39;2&#39;,&#39;3+&#39;],<br>                                            [&#39;Graduate&#39;, &#39;Not Graduate&#39;],<br>                                            [&#39;No&#39;, &#39;Yes&#39;],<br>                                            [&#39;Semiurban&#39;, &#39;Urban&#39;, &#39;Rural&#39;]]), [0,1,2,3,4,10]),<br>      (&quot;norm&quot;, Normalizer(norm=&#39;l1&#39;), [5,6,7,8,9])])</pre><p>You will notice in the code above I have used the categories argument of the OnHotEncoder function. This takes a list of all possible categories in each column as a list of lists. This produces one hot encoded columns for all categories even if data does not exist for that category in the column. The reason for doing this is that when using the ColumnTransformer function on new data. If it doesn’t contain the same categories in each feature then the array produced will not be the same shape as the data used to train the model, and you will get an error.</p><p>I apply the transformer to the training data as shown below. The first few rows of the output, which is a list of lists containing numerical arrays, is shown beneath the code.</p><pre>X_train = colT.fit_transform(X_train)<br>X_train</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*ILWs_IHVZg6qNkJKLOHdnw.png" /></figure><p>To transform the X_test data you simply apply the column transformer again.</p><pre>X_test = colT.transform(X_test)</pre><h4>Training the Model</h4><p>The data is now ready to be used in any scikit-learn classifier. For simplicity I have just used a RandomForestClassifier model with the default parameters.</p><pre>from sklearn.ensemble import RandomForestClassifier<br>from sklearn.metrics import accuracy_score, classification_report<br>random_forest = RandomForestClassifier()<br>random_forest.fit(X_train, y_train)<br>y_pred = random_forest.predict(X_test)<br>print(classification_report(y_test, y_pred, target_names=[&#39;Y&#39;, &#39;N&#39;]))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*o0gDDLfGCvwXyQJ3PN_KLA.png" /></figure><h4>Predicting New Data</h4><p>To test how the ColumnTransformer would work if we were to use this model to make predictions on previously unseen data. I took a sample of rows from the test csv file I read in earlier. I then simply re-use the column transformer to apply the preprocessing steps.</p><pre>test_samp = test[:15]<br>test_samp = colT.transform(test_samp)</pre><pre>random_forest.predict(test_samp)</pre><p>Applying the Random Forest model to predict the loan status gives the following output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vp0PxPblhlHakH2vmn6kXw.png" /></figure><p>Having used this new tool I have to say I am impressed. It has really simplified my workflow and I am going to be using this a lot in my work from now on. It is really simple to add new transformation steps into the ColumnTransformer, something which I will definitely be looking at over the next few weeks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2268ea9564c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vickdata/easier-machine-learning-with-the-new-column-transformer-from-scikit-learn-c2268ea9564c">Easier Machine Learning with the New Column Transformer from Scikit-Learn</a> was originally published in <a href="https://medium.com/vickdata">vickdata</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>