The one machine learning concept you need to know
2016-04-27 10:07
369 查看
Some people spend weeks, months, even years trying to learn machine learning without any success. They play around with datasets, buy books, compete on Kaggle, but ultimately make little progress.
One of the big problems, is that many people just want to “dive in and build something.”
I admire the ambition of these students, but I absolutely think that the “just build something” method of learning a new subject is vastly overrated. In order to learn a technical subject, it pays off to have a solid understanding of the conceptual framework
that underlies that subject.
To quote Cal Newport: “to master a technical subject, you need to develop
intuition for every major concept.”
Machine learning is no exception.
And ultimately, in order to build your understanding, you need to start at the beginning with the most fundamental concepts.
But don’t take my word for it. I think that Elon Musk said it best : “It is important to view knowledge as sort of a semantic tree. Make sure you understand
the fundamental principles … before you get into the details.” Read that again (and consider who said it): start with the fundamental principals before you move on to the details. Start at the “trunk” of a subject before you climb out towards the “branches
and leaves.”
The most fundamental thing you need to understand in almost any applied subject, is what you’re actually trying to accomplish. What I’m suggesting, is that before you really dive into the details, you need to have a solid, intuitive understanding of
what you’re actually trying to do when you apply a machine learning algorithm.
So before you learn the syntax, techniques, “rules of thumb,” math, or advanced concepts, you need to have a firm understanding of the core task of machine learning.
So, let’s start at the beginning.
If I asked you to distill the core problem of supervised machine learning down to a single thing, do you know what it would be?
Not sure? Keep reading.
I’m going to give you a dataset …
You can get the data right here:df.unknown_fxn_data <- read.csv(url("http://www.sharpsightlabs.com/wp-content/uploads/2016/04/unknown_fxn_data.txt"))
We’re going to perform some basic machine learning with this data, and in doing this, I want to help you understand an important concept.
Preliminary data exploration
Ok, just like any data scientist that’s worth his $150,000 salary, let’s take a look the data first. We’ll perform some basic data exploration.First, we’ll use str() to examine the “structure” of the data:
str(df.unknown_fxn_data)
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/1_2016-04-12_ML-as-function-estimation_str-output-600x47.png?zoom=2&resize=456%2C36)
You’ll notice that there are two variables, input_var, andtarget_var.
A quick side-note about terminology
In machine learning, the variables that we use as inputs to our machine learning algorithms are commonly called inputs, but they are also frequently referred to as predictors or features (the terms are more or less interchangeable).Inputs/predictors areindependent variables. In a simple 2-d space, you can think of an input as x-axis variable.
In ML, the variable that we’re trying to predict is commonly called a target variable, but they are also called output variables orresponse variables (again, the terms are largely interchangeable). Ultimately, you can think of the
target as the dependent variable; visually, in a 2-dimensional graph, the target variable is the variable on the y-axis.
For the purposes of this exercise, input_var is the input/feature, andtarget_var is the variable we’re going to predict.
Ok, let’s get back to our data exploration; let’s print out the first few rows of the dataset using the head() function.
head(df.unknown_fxn_data)
![](http://i1.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/2_2016-04-12_ML-as-function-estimation_head-output.png?zoom=2&resize=172%2C122)
As I just mentioned, input_var is the independent input variable andtarget_var is the dependent target variable that we’re going to predict. Because we have only two variables, you can think of each row of data as a simple
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-f2cc7fdbb9ba90fd8eac175adbd6ac10_l3.png?zoom=2&resize=39%2C18)
pair.
Visually exploring the dataset
Now, let’s examine the data visually.Because input_var is an independent variable, we can plot it on the x-axis, and target_var should be on the y-axis.
Because these are discrete
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-f2cc7fdbb9ba90fd8eac175adbd6ac10_l3.png?zoom=2&resize=39%2C18)
pairs,
we’ll use a scatterplot to visualize the data. To create a scatterplot with R’s ggplot2, we’ll use geom_point().
(Note: if you’re not familiar with how ggplot works, and how to create scatterplots in ggplot, you can
find out more information in a separate blog post. To be honest, knowledge of data visualization and exploratory data analysis is a prerequisite for doing machine learning. If you don’t know data visualization and EDA, you might want to start there …)
Ok, here’s the code to create a scatterplot of our data using ggplot:
require(ggplot2) ggplot(data = df.unknown_fxn_data, aes(x = input_var, y = target_var)) + geom_point()
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/3_2016-04-08_machine-learning-as-function-estimation__random-data-without-function.png?zoom=2&resize=423%2C401)
By plotting the data visually, we can visually detect a pattern. Ultimately, we’re going to try to build a model that can replicate and predict that pattern.
A hint about the one thing you need to know about ML
I want to pause here for a moment and bring us back to our original question: “what is the core task of machine learning?”When I just stated that we’re trying to “replicate and predict” the pattern that we plotted, this is a big clue. Again, if you’re still not sure what the answer is, keep reading …
Creating a linear model using caret
Now that we’ve explored our data, we’ll create a very simple linear model using caret.require(caret) mod.lm <- train( target_var ~ input_var ,data = df.unknown_fxn_data ,method = "lm" )
In a recent blog post, I explained the basics of caret, but let’s quickly review.
The train() function is the “core” function of the caret package; it is the R function that we use to build a machine learning model.
Inside of the train() function, we’re using several parameters.
data = indicates that we’re going to be building a model with training observations in the df.unknown_fxn_data dataset.
target_var ~ input_var specifies the “formula” of our model. It indicates the target variable that we’re trying to predict as well as the predictor we’ll use as an input. (Note that caret also allows you to use multiple predictors. If you had a dataset
with several predictor variables, you could use several of them by separating each predictor with a + sign.)
Finally, method = “lm” indicates that we want to build a linear regression model. That is, we’re going to fit a straight line to these data of the form
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-ab5fc3a534109aa9a1f913e958218fc3_l3.png?zoom=2&resize=106%2C16)
,
where
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-002f618dd2d52e6ce129e66c06d48c36_l3.png?zoom=2&resize=16%2C16)
is
the slope and
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-e8e6326368351396d0034aff61919281_l3.png?zoom=2&resize=17%2C16)
is
the intercept.
Plotting the model, and the data
Ok. Now that we’ve built a simple linear model using the train()function, let’s plot the data and plot the linear model on top of it.To do this, we’ll extract the slope and the intercept of our linear model using the coef() function. (To speak a little more technically, what we’re doing here is extracting
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-e8e6326368351396d0034aff61919281_l3.png?zoom=2&resize=17%2C16)
and
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-002f618dd2d52e6ce129e66c06d48c36_l3.png?zoom=2&resize=16%2C16)
.)
#~~~~~~~~~~~~~~~~~ # get coefficients #~~~~~~~~~~~~~~~~~ coef.icept <- coef(mod.lm$finalModel)[1] coef.slope <- coef(mod.lm$finalModel)[2] #~~~~~~~~~~~~~~ # plot the data #~~~~~~~~~~~~~~ ggplot(data = df.unknown_fxn_data, aes(x = input_var, y = target_var)) + geom_point() + geom_abline(intercept = coef.icept, slope = coef.slope, color = "red")
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/4_2016-04-08_machine-learning-as-function-estimation__random-sine-data_with-regression.png?zoom=2&resize=421%2C400)
Using train(), we’ve essentially fit a mathematical function, which we’ll call
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-684b9dd79ec39c6e17e1678ff458ad61_l3.png?zoom=2&resize=34%2C22)
,
to the data.
To be exact, the algorithm fit the following linear function to the data:
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-df427b0942afda187df5ab2e084f4953_l3.png?zoom=2&resize=135%2C22)
.
We can use this to predict target_var astarget_var
![](http://i1.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-0f9686912d4ae7a12d32c761ea870ff3_l3.png?zoom=2&resize=29%2C22)
(input_var).
What we’ve actually done here: a quick recap
Ok, let’s quickly recap. What did we just do here?1. I gave you a set of data points.
2. We used an algorithm to fit a line to these data points. (Note: I haven’t explicitly discussed the exact algorithm. However, “behind the scenes” the train() function is executing an algorithm to find the “best fit” line for the data points.)
3. The output of this process is our function
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-684b9dd79ec39c6e17e1678ff458ad61_l3.png?zoom=2&resize=34%2C22)
,
which we can use as a model to make rough predictions about the relationship between input_var and target_var.
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/5_2016-04-12_ML-as-function-estimation_flow-chart.png?zoom=2&resize=456%2C134)
Here’s the part that I didn’t tell you at the beginning …
What I didn’t tell you at the beginning, is that there is a function that underlies the data points in our dataset.![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/6_2016-04-12_ML-as-function-estimation_flow-chart-with-UNKNOWN.png?zoom=2&resize=456%2C217)
The data points were created with the sine function, and a little bit of random noise.
set.seed(9877) input_var <- seq(-2.75,2.75, by = .2) target_var <- sin(input_var) + rnorm(length(input_var), mean = 0, sd = .2) df.unknown_fxn_data <- data.frame(input_var, target_var) ggplot(data = df.unknown_fxn_data, aes(x = input_var, y = target_var)) + geom_point() + stat_function(fun = sin, color = "navy", size = 1)
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/7_2016-04-08_machine-learning-as-function-estimation__sine-wave_and_random-data.png?zoom=2&resize=423%2C400)
So ultimately, when we began this exercise, there was a hidden function that generated the data, and we used machine learning to estimate that function. The machine learning method used only the 28 data points in our dataset to select an estimated
function
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-684b9dd79ec39c6e17e1678ff458ad61_l3.png?zoom=2&resize=34%2C22)
that
approximates the underlying function,
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-a7ee323bc5a3f73ad5e066b13bed5504_l3.png?zoom=2&resize=34%2C18)
.
![](http://i1.wp.com/www.sharpsightlabs.com/wp-content/uploads/2016/04/8_2016-04-08_machine-learning-as-function-estimation__estimated-fxn-vs-unknown-fxn.png?zoom=2&resize=456%2C188)
Said differently, machine learning allowed us to estimate the underlying function, while being effectively blind to that underlying function.
This is the one concept you need to understand before moving on…
This is the core concept that you need to understand: the essential problem of machine learning is function estimation.When you’re doing machine learning (specifically, supervisedlearning), you’re essentially using computational techniques toreverse engineer the underlying function from the data points alone. You’re estimating an underlying function
based only on training observations in a dataset.
In the exercise in this blog post, I intentionally kept the underlying function hidden from you, because we never know the underlying function (I only revealed it at the end to help drive home the concept). In practice, when you apply machine learning, you’ll
never know the underlying structure; your task will be to reverse engineer the underlying pattern in the data. Machine learning allows you to do this.
I’ll say it again: machine learning provides us with a set of computational and statistical tools for estimating
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-a7ee323bc5a3f73ad5e066b13bed5504_l3.png?zoom=2&resize=34%2C18)
;
for figuring out the underlying function with only a set of data points.
This might sound easy, but it’s fraught with pitfalls, challenges, and tradeoffs.
Next steps: more concepts to learn
As I mentioned in the beginning of the blog post, the real secret to mastering a technical subject is developing intuition for each major concept.Here are a few of topics that you’ll need to learn more about:
– Why do we need to estimate
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-a7ee323bc5a3f73ad5e066b13bed5504_l3.png?zoom=2&resize=34%2C18)
?
– How exactly do we assess how well
![](http://i0.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-684b9dd79ec39c6e17e1678ff458ad61_l3.png?zoom=2&resize=34%2C22)
approximates
![](http://i2.wp.com/www.sharpsightlabs.com/wp-content/ql-cache/quicklatex.com-a7ee323bc5a3f73ad5e066b13bed5504_l3.png?zoom=2&resize=34%2C18)
?
– The Bias/Variance tradeoff
– Training a model vs testing a model
Having said that, understanding “machine learning as function estimation” will help you develop your understanding of these additional concepts.
To return to Musk’s analogy at the beginning of the blog post, if you think of the topics of ML as sort of a semantic tree, these additional topics are some of the “branches” of the tree. However, you can think of the topic of this blog post – machine learning
as function estimation – as the “trunk” of the tree. Once you understand this, you can begin to learn the other foundational concepts of machine learning.
The post The one machine learning concept you need to knowappeared first
on SHARP SIGHT LABS.
相关文章推荐
- 原创作品
- mysql 删除日志文件详解
- H3C 无线控制器关闭信号认证
- 让免费版MarkdownPad2使用Pro版本的功能
- Visual Studio对 C++11标准支持情况
- mysql求最大第二,最大第三个数
- Android学习路线总结,绝对干货(转)
- Java多线程编程实战指南(设计模式篇,黄文海)-之管道线模式
- 使用intellij的svn时提示出错: Can't use Subversion command line client: svn.Errors found while svn working co
- java中静态代码块的用法 static用法详解
- studio中java文件右下角显示红色j而不是C标识
- JAVA中字符串函数subString的用法小结
- Scanner中next()方法和nextLine()方法的区别
- 随记
- hashmap和treemap比较
- android从案例学习RxAndroid
- eclipse下如何配置tomcat
- 1、CSS3背景颜色渐变
- js 判断只能输入正整数或两位以内的小数(包括一位小数)
- C和指针之学习笔记(1)