Daily demand for a constructed product rises with temperature, curves upward, and climbs faster in summer than in winter. A straight line in temperature cannot draw that, yet ordinary least squares can still fit it once the design matrix contains the right columns. This lesson builds those columns from temperature and season, explains why the model stays linear in its coefficients while its predictions curve, and shows which construction steps must be learned from training data alone.
Key terms
Feature engineering
Definition Feature engineering is the construction of the columns of the design matrix from the raw inputs. A feature map sends each raw input to a feature vector in , and the model is then fitted on the rows . Every quantity the map takes from data, such as means and scales, a category vocabulary or knot locations, is part of the fitted procedure. It is learned from the training rows only, inside each training fold when cross-validation is used, saved, and applied unchanged to validation, test and future rows. Construction choices such as the polynomial degree or which interactions to include are made with validation data, never with the test set. Fixed arithmetic with constants chosen in advance needs no data, and every raw input that the map uses has to be available at prediction time.
In plain words A linear model can only combine the columns it is given, so choosing the columns decides which shapes of relationship it can express. Building them is part of training, and anything learned while building them must come from the same rows as the coefficients.
Example For demand with temperature in degrees Celsius and a season of winter, summer or spring, let , where and are reference values fixed in advance rather than statistics of the data. The feature vector is , where and indicate summer and spring. A constructed model on these features is
Because has no unit, each coefficient is in demand units. If were instead standardized with the mean and standard deviation of temperature, those two numbers would have to come from the training rows.
Basis function
Definition Basis functions are fixed functions of the raw input, and a basis expansion model predicts
Such a model is linear in parameters, meaning that for every fixed input the prediction is a linear function of the coefficient vector . Its dependence on the raw input can be strongly nonlinear. Let be the design matrix with entries . Fitting by squared error is then ordinary least squares on , its objective is a convex quadratic in , and the coefficients are unique exactly when has full column rank. A model such as is not linear in parameters.
In plain words Each basis function supplies one column, and the model is a weighted sum of columns. Linearity refers to the weights, not to the shape of the curve, so curved predictions can still be fitted with the linear algebra of least squares.
Example The demand model has the six basis functions , , , , and . Its summer predictions at , and degrees are , and , which rise by and then by , so they do not lie on a straight line in temperature. Nine constructed rows, one for each season at each of the three temperatures, give a design matrix of rank , and least squares on targets generated without noise from the model returns exactly the coefficients , , , , and . That recovery checks the construction. It says nothing about whether real demand follows this formula.
Polynomial feature
Definition A polynomial feature is a power of an input, such as or , or a product of powers of several inputs. A polynomial model of degree in one input uses the basis . With raw inputs, all products of powers with total degree at most give columns including the constant, so the count grows quickly, to columns for and . The coefficient of a single power is not a slope. The rate of change is a derivative that combines several coefficients, for example for the model .
In plain words Squares and higher powers let a fitted curve bend. When an input’s values lie far from zero compared with their spread, its powers are nearly collinear, so it helps to center and scale the input before taking powers. Outside the observed range a polynomial is driven by its highest power and can quickly become implausible.
Example Winter demand is , which is , and at , and degrees. Its rate of change is per unit of , which is , and at those temperatures, or , and demand units per degree, since one unit of is ten degrees. Over the nine rows the raw temperature and its square have correlation about , while and have correlation . At degrees, far outside the observed range of to degrees, and the summer prediction is , a number with no data behind it.
Interaction term
Definition An interaction term is the product of two features, such as . In a model containing , and , the change in prediction that goes with one of them depends on the value of the other. For the demand model
and the summer minus winter difference at the same temperature is . Without the product, the difference between seasons would be the same at every temperature. A model with an interaction usually also keeps both features of the product, because dropping one imposes a restriction whose meaning depends on where the zero of the other is placed. An interaction describes how predictions vary, and it has a causal reading only when the design of the study or further assumptions support one.
In plain words The product switches on an extra temperature effect only in summer, so the summer curve can be steeper than the winter curve. The season coefficient then gives the gap at one temperature only, the one where .
Example The gap equals , and at , and degrees, so the coefficient is the gap at degrees only. At degrees the temperature slope is per unit of in winter and in summer, which is and demand units per degree. The model has no spring interaction, so the spring and winter curves have the same shape and differ by at every temperature. That is a modeling restriction, and adding would remove it at the cost of one more coefficient.
Indicator variable
Definition An indicator variable equals when a condition holds and otherwise. A categorical input with categories enters a model with an intercept through indicators, and the omitted category is the reference. Adding an indicator for the reference category as well makes the indicators sum to the column of ones, so the design matrix loses full column rank. The fitted values stay unique, but the coefficients do not. Dropping the intercept and keeping all indicators is the other valid coding. The vocabulary of categories is fixed during training and saved in order. A category that never appeared in training needs an explicit policy, such as raising an error or routing it to a designated category for rare or unknown values that already exists in training, because silently setting all indicators to treats it as the reference category.
In plain words Indicators let each category have its own level without pretending the labels are numbers. With an intercept one category is left out, and every indicator coefficient is a difference from that baseline.
Example Winter is the reference, for summer and for spring. A spring day has and , and its prediction is below winter at the same temperature. If a winter indicator is added next to the intercept, then on every row, and the nine-row design matrix with seven columns has rank . Adding to the intercept and subtracting from each of the three season coefficients then leaves every prediction for a known season unchanged. A test row labeled autumn has no indicator of its own, and encoding it as all zeros under the two-indicator coding would quietly give it the winter prediction.
Spline
Definition A spline in one input is a function built from polynomial pieces on the intervals between chosen points , called knots, with the pieces joined so that the function, and possibly some of its derivatives, is continuous at each knot. With the hinge functions , every continuous piecewise linear function with these knots can be written as
whose slope changes by at the knot . Cubic splines use pieces of degree three whose values and first two derivatives agree at each knot. Once the knots are fixed, a spline is linear in parameters and is fitted by least squares like any other basis. Knots placed using data, for example at quantiles of the input, are learned from the training rows only.
In plain words A spline lets the curve change its shape at chosen places without jumping. With the other coefficients fixed, changing the coefficient of a hinge changes predictions only to the right of its knot, while changing the coefficient of a power in a global polynomial changes predictions at every input where that power is not zero. Local bases such as B-splines go further, since each of their basis functions is nonzero only over a few neighboring intervals between knots.
Example With one knot at , the spline has slope per unit of below degrees and slope above, and both pieces give at the knot. Its values at , and are , and . Beyond the range of the data its last piece simply continues as a straight line, which is no more trustworthy than the polynomial’s extrapolation.
Check yourself
- Using the demand model, build the feature row and the prediction for a -degree summer day and for a -degree spring day. Why is their difference not the same as at degrees?
Show the answer
At degrees and . The summer row is , and the prediction is . The spring row is , and the prediction is . The difference is . Only summer has an interaction with , so the summer minus spring difference is , which is at degrees and at degrees.
- An analyst encodes the season with an intercept and indicators for all three seasons, and the solver reports that the design matrix has rank . Explain why, and say what goes wrong when a test row labeled autumn is encoded with all three season indicators equal to .
Show the answer
Every row belongs to exactly one season, so the three indicators add up to the column of ones and one column is a combination of the others. The other six columns are independent, so the rank is . The fitted values are still unique, but the coefficients are not. Adding any number to the intercept and subtracting from all three season coefficients changes no prediction for a winter, summer or spring row. One least-squares solution has intercept and season coefficients , and for winter, summer and spring, and with another has , , and . Both reproduce the training data exactly. An autumn row with all indicators receives the intercept plus the temperature terms, so at degrees it gets from the first solution and from the second. Its prediction depends on an arbitrary choice of the solver, not on the data. Keep an intercept with two indicators, or drop the intercept, and handle a category unseen in training with an explicit policy such as raising an error.
- A workflow does four things. It computes with the fixed constants and . It standardizes with the mean and standard deviation of all rows before the test set is split off. It chooses the polynomial degree among , and by the smallest test error. It places spline knots at the quartiles of the training temperatures. Which steps are safe, and how would you repair the others?
Show the answer
The first step is safe, because fixed arithmetic on an input available at prediction time uses no information from held-out rows. The second step leaks, since the test rows influence the scaling that the training rows see. Compute the mean and standard deviation from the training rows, inside each training fold under cross-validation, and apply the saved values to every other row. The third step makes the test set part of development, so the test error of the chosen degree is optimistically biased. Choose the degree on validation data or by cross-validation within the training data, then evaluate the frozen choice once on the test set. The fourth step is safe as long as the quartiles are computed from the training rows only, and recomputed inside each training fold when cross-validation is used.