Machine learning has become a common part of petrophysical workflows, from predicting missing curves to classifying facies. Most of the data we feed these models is continuous, eg. gamma ray, density, neutron porosity, sonic compressional. But, alongside the curves, almost every project carries categorical data too. This includes formation names, facies schemes, zonations, and lithology descriptions.
The problem is that machine learning models can’t use them directly. A linear regression, a neural network, or a support vector machine works entirely in numbers. It can multiply a gamma ray value by a coefficient, but it cannot multiply text strings by anything. So before a formation column can go into a machine learning model, it has to be converted into numeric values. There are different ways to do this, and choosing which method can impact how the model(s) treat those values and information.
In this article, we will look at what encoding is, the common options, and why one hot encoding is usually a safe choice. We will then test it on a simple, but real dataset from the Volve field to see how much difference the choice of encoding method makes.
What is encoding?
First, we need to understand what encoding is.
Encoding is simply the process of converting categorical values into numerical values that a model can work with. That sounds like a mechanical step, and it is often treated as one, a line of preprocessing code to get through before the interesting work starts. But, every encoding scheme carries information into the model that in turn tells the model something about those categories.
Numbers have an order, and as a result they have distances between them. Whatever numbers we choose, the model will take them seriously, whether we intended them or not.
Here are a few common encoding techniques.
Label encoding (also called integer encoding) assigns each category a whole number: 0, 1, 2, 3 and so on. It’s simple and compact, one column in, one column out, and often the first thing we think of doing when converting lithologies or formations to numeric values.
In some cases, the label encoding of lithology values is already done as part of data deliverables or converted as part of a company standard. For example a coarse grained arkosic sandstone is always encoded as 12345, and a fine grained laminated shale is always encoded as 54321. Even though the numbers are very different, they still carry information and distances.
When using tools like scikit-learn’s LabelEncoder each category is assigned an integer based on alphabetical order. This is illustrated below:
The encoder has placed the formations on a number line, at equal spacing, in alphabetical order. To us that looks like a lookup table, however when a model looks it this it considers it a measurement scale. A linear model given this column has to fit a single coefficient across it, which means treating the step from Draupne Fm to Heather Fm as the same size as the step from Hugin Fm to Tor Fm, and treating the Draupne Fm as two times as far from Heimdal Fm as the Tor Fm is.
Ordinal encoding is the same idea, but with the integers assigned in an order you choose deliberately, because the categories have a real ranking. Coarse to fine grain size across facies would be a candidate, as would geological age.
One hot encoding creates a new binary column for each category, which we will come to shortly.
There are more exotic options too, such as target encoding and learned embeddings, which are useful when a categorical has hundreds of levels, but for the handful of formations or facies in a typical petrophysical dataset, the three above cover almost every situation.
What is one hot encoding?
One hot encoding takes a different approach. Instead of one column of integers, it creates one binary column per category. Each sample gets a 1 in the column belonging to its category and a 0 everywhere else.
Which prediction algorithms this matters for comes down to a single question: does the algorithm treat the numbers you feed it as positions on a scale? The ones that do are exactly the ones that get misled by label encoding, and exactly the ones one hot encoding can work well with.
Distance-based methods are the clearest case. For example, K-nearest neighbours decides what a sample is by measuring how far it sits from others, and self-organising maps and clustering methods like K-means work the same way. Under label encoding, the Skagerrak at code 6 is literally six units from a formation at code 0 and one unit from its alphabetical neighbour, so the algorithm treats alphabetical adjacency as geological similarity. One hot encoding fixes this because every category sits the same distance from every other, so no false neighbours are created.
Linear and additive models are the next case. Multiple linear regression fits one coefficient per feature, so a label encoded column forces a single straight-line relationship across categories that have no order. With one hot encoding, each formation gets its own coefficient and can take whatever value the data supports, independently of the others. The same reasoning covers logistic regression and, to a degree, support vector machines.
Neural networks sit here too. A network multiplies each input by a weight, so a label encoded formation enters as a single input whose magnitude the network reads as meaningful, and larger codes push harder on the first layer for no good reason. One hot encoding gives the network one input per category, each with its own weight, which is why it is the usual starting point for categorical inputs that are not being handled by a dedicated embedding layer.
The common theme between these models is that all of them treat input magnitude as information. The main family that does not are tree-based models. A decision tree, random forest, or gradient boosting model splits a feature into groups rather than scaling it, so it can carve a label encoded column into sensible partitions on its own. Encoding still matters there for other reasons, but the false-ordering problem is much weaker.
So one hot encoding is generally the safe default whenever the model reads numbers as positions on a scale, and the categories have no natural order. It makes no ordering claim, so it cannot make a wrong one.
One issue that can arise from using One Hot Encoding is that one column can suddenly become many. This can start to become a handful when we have 100s of categories being converted into 100s of input curves. However, for most petrophysical work, this is unlikely to become a huge issue due to the smaller amount of categories and formation names we work with.
One column too many: dropping the first category
There is one wrinkle worth understanding before we use One Hot Encoding, because it appears as an argument in most encoders. When you one hot encode a formation with, say, four categories, you get four binary columns. But, those four columns carry a hidden redundancy: because every sample belongs to exactly one formation, the four columns always sum to one across any row. Knowing three of them tells you the fourth for free. If it is not Heather, Hod, or Hugin, it must be Draupne.
For many models that redundancy is harmless, but for linear regression it is a real problem. A regression usually includes an intercept, which is effectively a column of all ones. Since the full set of dummy columns also sums to one on every row, the dummies and the intercept become perfectly collinear, and the regression can no longer find a unique solution. This is sometimes called the dummy variable trap.
The fix is to drop one category, which is what drop="first" does in scikit-learn (pd.get_dummies offers the same through drop_first=True). The dropped category does not disappear from the model. It becomes the baseline, and every remaining coefficient is then read as the effect relative to that baseline.
For tree-based models and regularised models the collinearity is not fatal, so you will often see the full set of columns kept there, but for a plain linear regression, dropping one is the safe habit.
A real example from Volve
To see how much the choice actually matters, we can test both encodings on the same task with real data: well 15/9–19 A from Equinor’s openly released Volve dataset, with formation tops from the Norwegian Offshore Directorate factpages. The task is predicting the sonic curve (DT) from gamma ray, bulk density and the formation column, using a multiple linear regression.
First, we load the well and assign a formation to each depth sample from the tops:
import lasio
import numpy as np
tops = pd.read_csv(”wellbore_formation_top.csv”, encoding=”utf-8-sig”)
def add_formations(df, wellbore_name):
t = tops[(tops.wlbName == wellbore_name) & (tops.lsuLevel == “FORMATION”)]
out = pd.Series(None, index=df.index, dtype=object)
for _, row in t.iterrows():
mask = (df.MD >= row.lsuTopDepth) & (df.MD < row.lsuBottomDepth)
out[mask] = row.lsuName.replace(” FM”, “”).title()
return out
df = lasio.read(”159-19A_LFP.las”).df().reset_index()[[”DEPTH”, “LFP_GR”, “LFP_DT”, “LFP_RHOB”]]
df.columns = [”MD”, “GR”, “DT”, “RHOB”]
df[”formation”] = add_formations(df, “15/9-19 A”)
df = df.dropna(subset=[”GR”, “DT”, “RHOB”, “formation”])That gives us 3,850 samples across ten formations, from the Blodøks down to the Skagerrak. Before modelling anything, it is worth looking at what label encoding would do to this well. Plotting the mean DT of each formation against its alphabetical integer code:
The means zigzag along the alphabet. The Draupne hot shale sits up at around 120 us/ft in second position, between the much faster Blodøks and Heather, and the chalks and sands land wherever their names happen to fall. The dashed line shows the single slope a linear model would be forced to fit through that. It is wrong for nearly every formation at once.
Now the test. We fit the same regression twice, once with the label encoded formation column and once with the one hot version, and score both with shuffled 5-fold cross validation:
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import KFold, cross_val_score
le = LabelEncoder()
df[”form_code”] = le.fit_transform(df[”formation”])
y = df[”DT”].values
X_int = df[[”GR”, “form_code”, “RHOB”]].values
ohe = OneHotEncoder(sparse_output=False, drop=”first”)
X_ohe = np.column_stack([df[[”GR”, “RHOB”]].values, ohe.fit_transform(df[[”formation”]])])
kf = KFold(n_splits=5, shuffle=True, random_state=42)
kf = KFold(n_splits=5, shuffle=True, random_state=0)
r2_int = cross_val_score(LinearRegression(), X_int, y, cv=kf, scoring=”r2”).mean()
r2_ohe = cross_val_score(LinearRegression(), X_ohe, y, cv=kf, scoring=”r2”).mean()
print(f”label encoded: R2 = {r2_int:.3f} | one hot: R2 = {r2_ohe:.3f}”)The label encoded model scores an R squared of 0.613. The one hot model scores 0.888.
Even though this is a simple model, we have the same data, same model, and the same validation process, with the only difference between them is how the formation names were turned into numbers.
Summary
Encoding is the step where categorical data becomes something a model can use, and it is a choice rather than a formality. Label encoding is compact but invents an ordering, and unless you assign the codes deliberately, that ordering is alphabetical and geologically meaningless. One hot encoding trades a few extra columns for making no assumptions at all, and for the unordered categories that dominate petrophysical work, formations, facies, lithologies, that is usually the right trade. On a real Volve well, it was the difference between an R squared of 0.613 and 0.888.
Data: Equinor Volve open dataset (well logs) and the Norwegian Offshore Directorate factpages (formation tops), used with thanks under their respective licences.
The example in this article used a linear regression, to keep the focus on the encoding rather than the model. If you would rather see curve prediction done with a neural network, I have a walkthrough on Substack that builds one in Keras to predict bulk density from other logs, using the same Volve data. It also shows how much of a difference normalising the inputs makes to the result.
→ Well Log Measurement Prediction Using Neural Networks with Keras


