4. Fitting a Model to Data - 6bolin9/Data-Science-For-Business-Foster-Provost-and-Tom-Fawcett- GitHub Wiki
Parameter Learning or Parametric Modeling Approach
An alternative method for learning a predictive model from a dataset is to:
- start by specifying the structure of the model with certain numeric parameters left unspecified.
- Then the data mining calculates the best parameter values given a particular set of training data.
- A very common case is where the structure of the model is a parameterized mathematical function or equation of a set of numeric attributes.
- The attributes used in the model could be chosen based on domain knowledge.
- Or they could be chosen based on other data mining techniques, such as the attribute selection procedures introduced in Chapter 3.
- Specifies the form of the model and the attributes; the goal of the data mining is to tune the parameters so that the model fits the data as well as possible.
In certain fields of statistics and econometrics, the bare model with unspecified parameters is called “the model.” Here it's called the structure of the model, which still needs to have its parameters specified to be useful.
- 本章节的假设: 1)只针对binary classes问题,方便总结 2)假设所有变量是numeric的 3)暂时不考虑对变量的normalization
LINEAR DISCRIMINANT FUNCTIONS or linear classifier
Tree model 是多条垂直于坐标轴的分类线, linear classifier 是一条可以倾斜的线: The difference is that the method for taking multiple attributes into account is to create a mathematical function of them。

A parameterized model: the weights of the linear function (wi) are the parameters. The data mining is going to “fit” this parameterized model to a particular dataset—meaning specifically, to find a good set of weights on the features. After learning, these weights are often loosely interpreted as important indicators of the features.
Roughly, the larger the magnitude of a feature’s weight, the more important that feature is for classifying the target.By the same token, if a feature’s weight is near zero the corresponding feature can usually be ignored or discarded.
OPTIMIZING AN OBJECTIVE FUNCTION
we need to ask, what should be our goal or objective in choosing the parameters? For linear regression,what weights should we choose?
- define an objective function that represents our goal, and can be calculated for a particular set of weights and a particular set of data.
- find the optimal value for the weights by maximizing or minimizing the objective function.
- What can easily be overlooked is that these weights are “best” only if we believe that the objective function truly represents what we want to achieve, or practically speaking, is the best proxy we can come up with.
Linear regression, logistic regression, and support vector machines are all very similar instances of our basic fundamental technique: fitting a (linear) model to data. The key difference is that each uses a different objective function.
Example: Classify two parts of a flower.
Which separator do you think is better?
One generated by logistic regression and the second by another linear method, a support vector machine (which will be described shortly).Logistic regression separates the two classes completely: all the Iris Versicolor examples are to the left of its line and all the Iris Setosa to the right. The Support vector machine line is almost midway between the clumps, though it misclassifies the starred point at (3, 1).
SUPPORT VECTOR MACHINES, BRIEFLY.
- Oversimplifying slightly, a nonlinear SVM uses different features (that are functions of the original features), so that the linear discriminant with the new features is a nonlinear discriminant with the original features.
- What is the objective function that is used to fit an SVM to data?
- SVMs choose based on a simple, elegant idea: instead of thinking about separating with a line, first fit the fattest bar between the classes.
- Then once the widest bar is found, the linear discriminant will be the center line through the bar.
- The distance between the dashed parallel lines is called the margin around the linear discriminant, and thus the objective is to maximize the margin.
New observations from the true population are likely to fall into the margin area.The margin-maximizing boundary gives the maximal leeway for classifying such points.
- Some data points will inevitably be misclassified by the model.
- In the objective function that measures how well a particular model fits the training points,
- Will simply penalize a training point for being on the wrong side of the decision boundary.
- In the case where the data indeed are linearly separable, incur no penalty and simply maximize the margin.
- If the data are not linearly separable, the best fit is some balance between a fat margin and a low total error penalty.
- The penalty for a misclassified point is proportional to the distance from the decision boundary, so if possible the SVM will make only “small” errors.
This error function is known as hinge loss (see Sidebar: Loss functions and Figure 4-9).
A loss function determines how much penalty should be assigned to an instance based on the error in the model’s predicted value.
Support vector machines use hinge loss.
.
-
Hinge loss incurs no penalty for an example that is not on the wrong side of the margin. The hinge loss only becomes positive when an example is on the wrong side of the boundary and beyond the margin. Loss then increases linearly with the example’s distance from the margin, thereby penalizing points more the farther they are from the separating boundary.
-
Zero-one loss assigns a loss of zero for a correct decision and one for an incorrect decision.
-
Squared error specifies a loss proportional to the square of the distance from the boundary. Squared error loss usually is used for numeric value prediction (regression), rather than classification. The squaring of the error has the effect of greatly penalizing predictions that are grossly wrong
For classification, this would apply large penalties to points far over on the “wrong side” of the separating boundary. Using squared error for classification also penalizes points far on the correct side of the decision boundary.
For most business problems, choosing squared-error loss for classification or class-probability estimation thus would violate our principle of thinking carefully about whether the loss function is aligned with the business goal. Hinge-like versions of squared error have been created because of this misalignment [Rosset & Zhu, 2007].
Linear Regression
For linear regression, there are many ways to compute the error between an estimated value and an actual value. Standard linear regression procedures minimize the sum or mean of the squares of these errors. Squared error is particularly convenient mathematically. Analysts often claim to prefer squared error because it strongly penalizes very large errors. Importantly, any choice for the objective function has both advantages and drawbacks.
- least squares regression: very sensitive to the outlying data points that can severely skew the resultant linear function.
- For systems that build and apply models totally automatically, the modeling needs to be much more robust than when doing a detailed regression analysis “by hand.” We may want to use a more robust modeling procedure (e.g., use as the objective function absolute error instead of the squared error). 在商业应用中,对于线性回归的loss function选择切记于心。
Class Probability Estimation and Logistic “Regression”
Within the same framework for fitting linear models to data, by choosing a different objective function we can produce a model designed to give accurate estimates of class probability. The most common procedure by which we do this is called logistic regression. They are used widely to estimate quantities like the probability of default on credit, the probability of response to an offer, the probability of fraud on an account, the probability that a document is relevant to a topic, and so on
What exactly is an accurate estimate of class membership probability?
- we would like (i) the probability estimates to be well calibrated.
- (ii) the probability estimates to be discriminative, in that if possible they give meaningfully different probability estimates to different examples.
The latter condition keeps us from simply giving the “base rate” (the overall prevalence in the population) as the prediction for every example.
- For probability estimation, logistic regression uses the same linear model as do linear discriminants for classification and linear regression for estimating numeric target values.
- The output of the logistic regression model is interpreted as the log-odds of class membership.Log-odds, more loosely, is the probability of class membership (a numeric quantity) over a categorical class. The odds of an event is the ratio of the probability of the event occurring to the probability of the event, not occurring.
- These log-odds can be translated directly into the probability of class membership.
LOGISTIC REGRESSION: SOME TECHNICAL DETAILS.
Log-odds linear function
Since often we actually want the estimated probability of class membership, not the log-odds, we can solve:
We would like there to be relative certainty in the estimations of class membership far from the decision boundary, and uncertainty near the decision boundary. This curve is called a “sigmoid” curve because of its “S” shape, which squeezes the probabilities into their correct range (between zero and one)

Objective function to fit the logistic regression
Computing the “likelihood” that a particular labeled example belongs to the correct class, given a set of parameters w that produces class probability estimates P(x=1) :
The g function gives the model’s estimated probability of seeing x’s actual class given x’s features. Now consider summing the g values across all the examples in a labeled dataset. And do that for different parameterized models—in our case, different sets of weights (w) for the logistic regression. The model (set of weights) that gives the highest sum is the model that gives the highest “likelihood” to the data—the “maximum likelihood” model. The maximum likelihood model “on average” gives the highest probabilities to the positive examples and the lowest probabilities to the negative examples.
Understand the Estimated Probability
Take an application to targeted marketing for example. For a consumer c, our model may estimate the probability of responding to the offer to be P(c,response)=0.02. In the data, we see that the person indeed does respond.
That does not mean that this consumer’s probability of responding actually was 1.0, nor that the model incurred a large error on this example. The consumer’s probability may indeed have been around p(c responds) = 0.02, which actually is a high probability of response for many campaigns, and the consumer just happened to respond this time.
A more satisfying way to think about it is that the training data comprise a set of statistical “draws” from the underlying probabilities, rather than representing the underlying probabilities themselves. The logistic regression procedure then tries to estimate the probabilities (the probability distribution over the instance space) with a linear-log-odds model, based on the observed data on the result of the draws from the distribution.
Example: Logistic Regression versus Tree Induction
**Differences between Logistic Regression and Tree Induction:
- A classification tree uses decision boundaries that are perpendicular to the instance-space axes, whereas the linear classifier can use decision boundaries of any direction or orientation. Because classification trees select a single attribute at a time whereas linear classifiers use a weighted combination of all attributes.
- In principle, a classification tree can cut up the instance space arbitrarily finely into very small regions (though we will see reasons to avoid that in Chapter 5). A linear classifier places a single decision surface through the entire space. It has great freedom in the orientation of the surface, but it is limited to a single division into two segments. This is a direct consequence of there being a single (linear) equation that uses all of the variables, and must fit the entire data space.
The comprehensibility of the models: decision trees may be more understandable for non-statistician than logistic models.
the data science team does not have the ultimate say in which models are used or implemented. Often there is at least one manager who must “sign off” on the use of a model in practice, and in many cases a set of stakeholders need to be satisfied with the model.
Nonlinear Functions, Support Vector Machines, and Neural Networks
linear functions can actually represent nonlinear models, if we include more complex features in the functions.
The Iris dataset with a nonlinear feature.
In this figure, logistic regression and support vector machine—both linear models—are provided an additional feature, Sepal width2, which allows both the freedom to create more complex, nonlinear models (boundaries).
We could specify arbitrarily complex numeric functions and fit their parameters to the data. The two most common families of techniques that are based on fitting the parameters of complex, nonlinear functions are nonlinear support-vector machines and neural networks.
Nonlinear SVM
One can think of nonlinear support vector machines as adding more complex terms and fitting a linear function to them. One could implement a nonlinear support vector machine with a “polynomial kernel,” which essentially means it would consider “higher-order” combinations of the original features (e.g., squared features, products of features).
1. Support vector machines have a so-called “kernel function” that maps the original features to some other feature space.
2. Then a linear model is fit to this new feature space.
A data scientist would become familiar with the different alternatives for kernel functions (linear, polynomial, and others).
Neural Networks.
Neural networks offer an intriguing twist. One can think of a neural network as a “stack” of models. On the bottom of the stack are the original features. From these features are learned a variety of relatively simple models.
- For example these are logistic regressions. Then, each subsequent layer in the stack applies a simple model (another logistic regression) to the outputs of the next layer down.
- So in a two-layer stack, we would learn a set of logistic regressions from the original features.
- And then learn a logistic regression using as features the outputs of the first set of logistic regressions.
- Neural networks target labels for training are provided only for the final layer
We could think of this very roughly as first creating a set of “experts” in different facets of the problem (the first-layer models), and then learning how to weight the opinions of these different experts (the second-layer model).
The stack of models can be represented by one big parameterized numeric function. The paramenters now are the coefficients of all the models, taken together. So once we have decided on an objective function representing what we want to optimize (e.g., the fit to the training data, based on some fitting function), we can then apply an optimization procedure to find the best parameters to this very complex numeric function.