Let's train some data. I'm Jesper and I have no clue what I'm doing but I'm having fun. Most texts are still from the original notebook from Brandon Hall. As well as the basis for the code.
In [74]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pandas import set_option
set_option("display.max_rows", 10)
pd.set_option('display.width', 1000)
pd.options.mode.chained_assignment = None
def distance(latlon1,latlon2):
lat1 = np.deg2rad(latlon1[0])
lon1 = np.deg2rad(latlon1[1])
lat2 = np.deg2rad(latlon2[0])
lon2 = np.deg2rad(latlon2[1])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat / 2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
return (6371 * c)
facies_labels = ['SS','CSiS','FSiS','SiSh','MS','WS','D','PS','BS']
facies = pd.DataFrame({'ShortName': pd.Series(facies_labels,index=facies_labels),'Facies' : pd.Series(['Nonmarine sandstone', 'Nonmarine coarse siltstone', 'Nonmarine fine siltstone ', 'Marine siltstone and shale', 'Mudstone (limestone)', 'Wackestone (limestone)', 'Dolomite', 'Packstone-grainstone (limestone)','Phylloid-algal bafflestone (limestone)'],index=facies_labels), 'Neighbours' : pd.Series(['CSiS',['SS','FSiS'],'CSiS','MS',['SiSh','WS'],['MS','D'],['WS','PS'],['WS','D','BS'],['D','PS']],index=facies_labels), 'Colors' : pd.Series(['#F4D03F', '#F5B041','#DC7633','#6E2C00',
'#1B4F72','#2E86C1', '#AED6F1', '#A569BD', '#196F3D'],index=facies_labels)})
filename = '../facies_vectors.csv'
training_data = pd.read_csv(filename)
training_data
Out[74]:
Verbatim from Brendon Hall:
This data is from the Council Grove gas reservoir in Southwest Kansas. The Panoma Council Grove Field is predominantly a carbonate gas reservoir encompassing 2700 square miles in Southwestern Kansas. This dataset is from nine wells (with 4149 examples), consisting of a set of seven predictor variables and a rock facies (class) for each example vector and validation (test) data (830 examples from two wells) having the same seven predictor variables in the feature vector. Facies are based on examination of cores from nine wells taken vertically at half-foot intervals. Predictor variables include five from wireline log measurements and two geologic constraining variables that are derived from geologic knowledge. These are essentially continuous variables sampled at a half-foot sample rate.
The seven predictor variables are:
The nine discrete facies (classes of rocks) are:
In [75]:
facies[['Facies']]
Out[75]:
These facies gradually blend into one another. We can see a marine non-marine neighbourhood already.
In [76]:
pd.DataFrame({'Neighbours' : [facies.ShortName[x] for x in facies.Neighbours]},index=facies_labels)
Out[76]:
The 'Well Name' and 'Formation' columns can be turned into a categorical data type.
In [77]:
training_data['Well Name'] = training_data['Well Name'].astype('category')
training_data['Formation'] = training_data['Formation'].astype('category')
training_data['NM_M'] = training_data['NM_M'].astype('category')
training_data['Well Name'].unique()
Out[77]:
Shrimplin:
ALEXANDER D
SHANKLE
LUKE G U
KIMZEY A
CROSS H CATTLE
NOLAN
Recruit F9
NEWBY
CHURCHMAN BIBLE
STUART
CRAWFORD
In [78]:
latlong = pd.DataFrame({"SHRIMPLIN": [37.98126,-100.98329], "ALEXANDER D": [37.6607787,-95.3534525], "SHANKLE": [38.0633727,-101.3891894], "LUKE G U": [37.4537739,-101.6073725], "KIMZEY A": [37.12289,-101.39697], "CROSS H CATTLE": [37.9105826,-101.6464517], "NOLAN": [37.7866294,-101.0451641], "NEWBY": [37.5406739,-101.5847635], "CHURCHMAN BIBLE": [37.3497658,-101.1060761], "STUART": [37.4857262,-101.1391063], "CRAWFORD": [37.1893654,-101.1494994], "Recruit F9": [37.4,-101]})
#distance([37.660779,-95.353453],[37.349766,-101.106076])
dist_mat= pd.DataFrame(np.zeros((latlong.shape[1],latlong.shape[1])))
#dist_mat = np.zeros(len(latlong.index),len(latlong.index))
for i,x in enumerate(latlong):
for j,y in enumerate(latlong):
if i > j:
dist_mat[i][j] = (distance(latlong[x].values,latlong[y].values))
dist_mat[j][i] = dist_mat[i][j]
dist_mat
Out[78]:
In [79]:
training_data.describe()
Out[79]:
The new data set contains about 1000 data points or 33% more than the original one. Rejoice!
In [80]:
#facies_color_map is a dictionary that maps facies labels
#to their respective colors
facies_color_map = {}
for ind, label in enumerate(facies_labels):
facies_color_map[label] = facies.Colors[ind]
def label_facies(row, labels):
return labels[ row['Facies'] ]
training_data['Facies'] = training_data['Facies']-1
training_data= training_data[training_data['PE'].notnull().values]
training_data.loc[:,'FaciesLabels'] = training_data.apply(lambda row: label_facies(row, facies_labels), axis=1)
rows = len(training_data.index)
processed_data = training_data.copy()
Let's take a look at the data from individual wells in a more familiar log plot form. We will create plots for the five well log variables, as well as a log for facies labels. The plots are based on the those described in Alessandro Amato del Monte's excellent tutorial.
In [81]:
def make_facies_log_plot(logs, facies_colors):
#make sure logs are sorted by depth
logs = logs.sort_values(by='Depth')
cmap_facies = colors.ListedColormap(
facies.Colors[0:len(facies.Colors)], 'indexed')
cmap_coarse = colors.ListedColormap(['#c0c0c0','#000000'])
ztop=logs.Depth.min(); zbot=logs.Depth.max()
cluster=np.repeat(np.expand_dims(logs['Facies'].values,1), 100, 1)
clustercoarse=np.repeat(np.expand_dims(logs['NM_M'].values,1), 100, 1)
f, ax = plt.subplots(nrows=1, ncols=7, figsize=(8, 12))
ax[0].plot(logs.GR, logs.Depth, '-g')
ax[1].plot(logs.ILD_log10, logs.Depth, '-')
ax[2].plot(logs.DeltaPHI, logs.Depth, '-', color='0.5')
ax[3].plot(logs.PHIND, logs.Depth, '-', color='r')
ax[4].plot(logs.PE, logs.Depth, '-', color='black')
im1 = ax[5].imshow(cluster, interpolation='none', aspect='auto',
cmap=cmap_facies,vmin=0,vmax=8)
im2 = ax[6].imshow(clustercoarse, interpolation='none', aspect='auto',
cmap=cmap_coarse,vmin=1,vmax=2)
divider = make_axes_locatable(ax[6])
cax = divider.append_axes("right", size="20%", pad=0.05)
cbar=plt.colorbar(im1, cax=cax)
cbar.set_label((18*' ').join(facies.index))
cbar.set_ticks(range(0,1)); cbar.set_ticklabels('')
for i in range(len(ax)-2):
ax[i].set_ylim(ztop,zbot)
ax[i].invert_yaxis()
ax[i].grid()
ax[i].locator_params(axis='x', nbins=3)
ax[0].set_xlabel("GR")
ax[0].set_xlim(logs.GR.min(),logs.GR.max())
ax[1].set_xlabel("ILD_log10")
ax[1].set_xlim(logs.ILD_log10.min(),logs.ILD_log10.max())
ax[2].set_xlabel("DeltaPHI")
ax[2].set_xlim(logs.DeltaPHI.min(),logs.DeltaPHI.max())
ax[3].set_xlabel("PHIND")
ax[3].set_xlim(logs.PHIND.min(),logs.PHIND.max())
ax[4].set_xlabel("PE")
ax[4].set_xlim(logs.PE.min(),logs.PE.max())
ax[5].set_xlabel('Facies')
ax[6].set_xlabel('NM_M')
ax[1].set_yticklabels([]); ax[2].set_yticklabels([]); ax[3].set_yticklabels([])
ax[4].set_yticklabels([]); ax[5].set_yticklabels([]); ax[6].set_yticklabels([])
ax[5].set_xticklabels([]); ax[6].set_xticklabels([])
f.suptitle('Well: %s'%logs.iloc[0]['Well Name'], fontsize=14,y=0.94)
plt.show()
Here's the SHRIMPLIN
well for your vieling pleasure.
In [82]:
make_facies_log_plot(
processed_data[processed_data['Well Name'] == 'SHRIMPLIN'],
facies.Colors)
In addition to individual wells, we can look at how the various facies are represented by the entire training set. Let's plot a histogram of the number of training examples for each facies class.
In [83]:
#count the number of unique entries for each facies, sort them by
#facies number (instead of by number of entries)
facies_counts = processed_data['Facies'].value_counts().sort_index()
tmp = processed_data.query('NM_M==1')
facies_counts0 = tmp['Facies'].value_counts().sort_index()
tmp = processed_data.query('NM_M==2')
facies_counts1 = tmp['Facies'].value_counts().sort_index()
#use facies labels to index each count
facies_counts.index = facies_labels
facies_counts.plot(kind='bar',color=facies.Colors,
title='Distribution of Training Data by Facies')
pd.DataFrame(facies_counts).T
Out[83]:
This shows the distribution of examples by facies for the examples in the training set. Dolomite (facies 7) has the fewest with 81 examples. Depending on the performance of the classifier we are going to train, we may consider getting more examples of these facies.
Crossplots are a familiar tool in the geosciences to visualize how two properties vary with rock type. This dataset contains 5 log variables, and scatter matrix can help to quickly visualize the variation between the all the variables in the dataset. We can employ the very useful Seaborn library to quickly create a nice looking scatter matrix. Each pane in the plot shows the relationship between two of the variables on the x and y axis, with each point is colored according to its facies. The same colormap is used to represent the 9 facies.
In [84]:
#save plot display settings to change back to when done plotting with seaborn
inline_rc = dict(mpl.rcParams)
import seaborn as sns
sns.set()
plotdata=processed_data.drop(['Well Name','Formation','Depth','RELPOS'],axis=1).dropna()
sns.pairplot(plotdata.drop(['Facies','NM_M'],axis=1),
hue='FaciesLabels', palette=facies_color_map,
hue_order=list(reversed(facies_labels)))
Out[84]:
At this point I woult like to take a look at the separated main classes.
In [85]:
sns.pairplot(plotdata.query('NM_M==1').drop(['Facies','NM_M'],axis=1),
hue='FaciesLabels', palette=facies_color_map,
hue_order=list(reversed(facies_labels)))
Out[85]:
In [86]:
facies_counts0.plot(kind='bar',color=facies.Colors,
title='Distribution of Training Data by Facies')
pd.DataFrame(facies_counts0).T
Out[86]:
In this view it becomes obvious that GR, PHIND and PE have some outliers at:
In [87]:
sns.pairplot(plotdata.query('NM_M==2').drop(['Facies','NM_M'],axis=1),
hue='FaciesLabels', palette=facies_color_map,
hue_order=list(reversed(facies_labels)))
Out[87]:
In [88]:
facies_counts1.plot(kind='bar',color=facies.Colors,
title='Distribution of Training Data by Facies')
pd.DataFrame(facies_counts1).T
Out[88]:
Here we can see aparent outliers in GR, PhiND, PE and DeltaPhi
I have decided to keep GR as it is a general trend in the data (and not unheard of in that rock). DeltaPhi also fits a general trend and seems to be a feature. PhiND and PE do not lie in a trend or cluster and will therefore be removed.
In [89]:
processed_data0 = processed_data.query('Facies < 3')
processed_data1 = processed_data.query('and Facies > 2')
processed_data = pd.concat([processed_data0,processed_data1])
print("Dropped {0} rows due to outliers.".format(rows-len(processed_data.index),))
processed_data
In [ ]:
sns.set()
plotdata=processed_data.drop(['Well Name','Facies','Formation','Depth','NM_M','RELPOS'],axis=1).dropna()
sns.pairplot(plotdata,
hue='FaciesLabels', palette=facies_color_map,
hue_order=list(reversed(facies_labels)))
#switch back to default matplotlib plot style
mpl.rcParams.update(inline_rc)
In [ ]:
correct_facies_labels = processed_data['Facies'].values
correct_facies_labels0 = processed_data0['Facies'].values
#facies_labels=['Sand','Carbs']
correct_facies_labels1 = processed_data1['Facies'].values
correct_facies_labels_comp = training_data['Facies'].values
feature_vectors = processed_data.drop(['Formation', 'Well Name','Depth','Facies','FaciesLabels'], axis=1)
feature_vectors0 = processed_data0.drop(['Formation', 'Well Name','Depth','Facies','FaciesLabels'], axis=1)
feature_vectors1 = processed_data1.drop(['Formation', 'Well Name','Depth','Facies','FaciesLabels'], axis=1)
feature_vectors_comp = training_data.drop(['Formation', 'Well Name','Depth','Facies','FaciesLabels'], axis=1)
In [ ]:
feature_vectors.describe()
In [ ]:
feature_vectors0.describe()
In [ ]:
feature_vectors1.describe()
Scikit includes a preprocessing module that can 'standardize' the data (giving each variable zero mean and unit variance, also called whitening). Many machine learning algorithms assume features will be standard normally distributed data (ie: Gaussian with zero mean and unit variance). The factors used to standardize the training set must be applied to any subsequent feature set that will be input to the classifier. The StandardScalar
class can be fit to the training set, and later used to standardize any training data.
In [ ]:
from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(feature_vectors)
scaled_features = scaler.transform(feature_vectors)
scaler0 = preprocessing.StandardScaler().fit(feature_vectors0)
scaled_features0 = scaler0.transform(feature_vectors0)
scaler1 = preprocessing.StandardScaler().fit(feature_vectors1)
scaled_features1 = scaler1.transform(feature_vectors1)
scaler_comp = preprocessing.StandardScaler().fit(feature_vectors_comp)
scaled_features_comp = scaler_comp.transform(feature_vectors_comp)
Scikit also includes a handy function to randomly split the training data into training and test sets. The test set contains a small subset of feature vectors that are not used to train the network. Because we know the true facies labels for these examples, we can compare the results of the classifier to the actual facies and determine the accuracy of the model. Let's use 20% of the data for the test set.
In [ ]:
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier()
clf0 = RandomForestClassifier()
clf1 = RandomForestClassifier()
clf_comp = RandomForestClassifier()
Now we can train the classifier using the training set we created above.
In [ ]:
clf.fit(scaled_features,correct_facies_labels)
clf0.fit(scaled_features0,correct_facies_labels0)
clf1.fit(scaled_features1,correct_facies_labels1)
clf_comp.fit(scaled_features_comp,correct_facies_labels_comp)
Now that the model has been trained on our data, we can use it to predict the facies of the feature vectors in the test set. Because we know the true facies labels of the vectors in the test set, we can use the results to evaluate the accuracy of the classifier.
In [ ]:
predicted_labels = clf.predict(scaled_features)
predicted_labels0 = clf0.predict(scaled_features0)
predicted_labels1 = clf1.predict(scaled_features1)
predicted_labels_comp = clf_comp.predict(scaled_features_comp)
We need some metrics to evaluate how good our classifier is doing. A confusion matrix is a table that can be used to describe the performance of a classification model. Scikit-learn allows us to easily create a confusion matrix by supplying the actual and predicted facies labels.
The confusion matrix is simply a 2D array. The entries of confusion matrix C[i][j]
are equal to the number of observations predicted to have facies j
, but are known to have facies i
.
To simplify reading the confusion matrix, a function has been written to display the matrix along with facies labels and various error metrics. See the file classification_utilities.py
in this repo for the display_cm()
function.
In [ ]:
from sklearn.metrics import confusion_matrix,f1_score, accuracy_score, make_scorer
from classification_utilities import display_cm, display_adj_cm
In [ ]:
conf = confusion_matrix(correct_facies_labels, predicted_labels)
display_cm(conf, facies_labels, hide_zeros=True)
In [ ]:
conf0 = confusion_matrix(correct_facies_labels0, predicted_labels0)
display_cm(conf0, facies_labels[0:3], hide_zeros=True)
In [ ]:
conf1 = confusion_matrix(correct_facies_labels1, predicted_labels1)
display_cm(conf1, facies_labels[3:], hide_zeros=True)
In [ ]:
y_test_comb = np.concatenate([correct_facies_labels0,correct_facies_labels1])
predicted_labels_comb = np.concatenate([predicted_labels0,predicted_labels1])
conf_comb = confusion_matrix(y_test_comb, predicted_labels_comb)
display_cm(conf_comb, facies_labels, hide_zeros=True)
In [ ]:
conf_comp = confusion_matrix(correct_facies_labels_comp, predicted_labels_comp)
display_cm(conf_comp, facies_labels, hide_zeros=True)
The rows of the confusion matrix correspond to the actual facies labels. The columns correspond to the labels assigned by the classifier. For example, consider the first row. For the feature vectors in the test set that actually have label SS
, 23 were correctly indentified as SS
, 21 were classified as CSiS
and 2 were classified as FSiS
.
The entries along the diagonal are the facies that have been correctly classified. Below we define two functions that will give an overall value for how the algorithm is performing. The accuracy is defined as the number of correct classifications divided by the total number of classifications.
In [ ]:
def accuracy(conf):
total_correct = 0.
nb_classes = conf.shape[0]
for i in np.arange(0,nb_classes):
total_correct += conf[i][i]
acc = total_correct/sum(sum(conf))
return acc
As noted above, the boundaries between the facies classes are not all sharp, and some of them blend into one another. The error within these 'adjacent facies' can also be calculated. We define an array to represent the facies adjacent to each other. For facies label i
, adjacent_facies[i]
is an array of the adjacent facies labels.
In [ ]:
adjacent_facies = np.array([[1], [0,2], [1], [4], [3,5], [4,6,7], [5,7], [5,6,8], [6,7]])
def accuracy_adjacent(conf, adjacent_facies,offset):
nb_classes = conf.shape[0]
total_correct = 0.
for i in np.arange(0,nb_classes):
total_correct += conf[i][i]
for j in adjacent_facies[i]:
total_correct += conf[i][j-offset]
return total_correct / sum(sum(conf))
In [ ]:
print('Processed Full')
print('Facies classification accuracy = %f' % accuracy(conf))
print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf, adjacent_facies,0))
In [ ]:
print('Processed NM')
print('Facies classification accuracy = %f' % accuracy(conf0))
print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf0, adjacent_facies[0:3],0))
In [ ]:
print('Processed Marine')
print('Facies classification accuracy = %f' % accuracy(conf1))
print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf1, adjacent_facies[3:],3))
In [ ]:
print('Processed Compined')
print('Facies classification accuracy = %f' % accuracy(conf_comb))
print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf_comb, adjacent_facies,0))
In [ ]:
print('Non-processed')
print('Facies classification accuracy = %f' % accuracy(conf_comp))
print('Adjacent facies classification accuracy = %f' % accuracy_adjacent(conf_comp, adjacent_facies,0))
The classifier so far has been built with the default parameters. However, we may be able to get improved classification results with optimal parameter choices.
We will consider two parameters. The parameter C
is a regularization factor, and tells the classifier how much we want to avoid misclassifying training examples. A large value of C will try to correctly classify more examples from the training set, but if C
is too large it may 'overfit' the data and fail to generalize when classifying new data. If C
is too small then the model will not be good at fitting outliers and will have a large error on the training set.
The SVM learning algorithm uses a kernel function to compute the distance between feature vectors. Many kernel functions exist, but in this case we are using the radial basis function rbf
kernel (the default). The gamma
parameter describes the size of the radial basis functions, which is how far away two vectors in the feature space need to be to be considered close.
We will train a series of classifiers with different values for C
and gamma
. Two nested loops are used to train a classifier for every possible combination of values in the ranges specified. The classification accuracy is recorded for each combination of parameter values. The results are shown in a series of plots, so the parameter values that give the best classification accuracy on the test set can be selected.
This process is also known as 'cross validation'. Often a separate 'cross validation' dataset will be created in addition to the training and test sets to do model selection. For this tutorial we will just use the test set to choose model parameters.
In [ ]:
from sklearn.model_selection import RandomizedSearchCV,LeaveOneGroupOut
from scipy.stats import randint as sp_randint
Fscorer = make_scorer(f1_score, average = 'micro')
Ascorer = make_scorer(accuracy_score)
clf = RandomForestClassifier()
clf0 = RandomForestClassifier()
clf1 = RandomForestClassifier()
clf_comp = RandomForestClassifier()
n_iter_search = 30
param_dist0 = {"max_depth": [np.sqrt(scaled_features0.shape[1]),np.log2(scaled_features0.shape[1]),None],
"n_estimators": sp_randint(30,200),
"max_features": sp_randint(3,scaled_features0.shape[1]),
"min_samples_split": sp_randint(10,100),
"min_samples_leaf": sp_randint(2,30)}
param_dist1 = {"max_depth": [np.sqrt(scaled_features.shape[1]),np.log2(scaled_features1.shape[1]),None],
"n_estimators": sp_randint(30,200),
"max_features": sp_randint(3,scaled_features1.shape[1]),
"min_samples_split": sp_randint(10,100),
"min_samples_leaf": sp_randint(2,30)}
param_dist = {"max_depth": [np.sqrt(scaled_features.shape[1]),np.log2(scaled_features.shape[1]),None],
"n_estimators": sp_randint(30,200),
"max_features": sp_randint(3,scaled_features.shape[1]),
"min_samples_split": sp_randint(10,100),
"min_samples_leaf": sp_randint(2,30)}
param_dist_comp = {"max_depth": [np.sqrt(scaled_features_comp.shape[1]),np.log2(scaled_features_comp.shape[1]),None],
"n_estimators": sp_randint(30,200),
"max_features": sp_randint(3,scaled_features_comp.shape[1]),
"min_samples_split": sp_randint(10,100),
"min_samples_leaf": sp_randint(2,30)}
In [ ]:
random_search0 = RandomizedSearchCV(clf0, param_distributions=param_dist0,scoring = Fscorer,n_iter=n_iter_search)
random_search0.fit(scaled_features0,correct_facies_labels0)
print('Best score for NM: {}'.format(random_search0.best_score_))
print('Best parameters for NM: {}'.format(random_search0.best_params_))
clf0 = random_search0.best_estimator_
random_search0.best_estimator_
In [ ]:
random_search1 = RandomizedSearchCV(clf1, param_distributions=param_dist1,scoring = Fscorer,n_iter=n_iter_search)
random_search1.fit(scaled_features1,correct_facies_labels1)
print('Best score for random: {}'.format(random_search1.best_score_))
print('Best parameters for random: {}'.format(random_search1.best_params_))
clf1 = random_search1.best_estimator_
random_search1.best_estimator_
In [ ]:
random_search = RandomizedSearchCV(clf, param_distributions=param_dist,scoring = Fscorer,n_iter=n_iter_search)
random_search.fit(scaled_features,correct_facies_labels)
print('Best score for combo: {}'.format(random_search.best_score_))
print('Best parameters for combo: {}'.format(random_search.best_params_))
clf = random_search.best_estimator_
random_search.best_estimator_
In [ ]:
random_search_comp = RandomizedSearchCV(clf_comp, param_distributions=param_dist_comp,scoring = Fscorer,n_iter=n_iter_search)
random_search_comp.fit(scaled_features_comp,correct_facies_labels_comp)
print('Best score for unprocessed: {}'.format(random_search_comp.best_score_))
print('Best parameters for unprocessed: {}'.format(random_search_comp.best_params_))
clf_comp = random_search_comp.best_estimator_
random_search_comp.best_estimator_
Okay, take it up a notch.
In [ ]:
clf0.fit(scaled_features0, correct_facies_labels0)
In [ ]:
clf1.fit(scaled_features1, correct_facies_labels1)
In [ ]:
clf.fit(scaled_features, correct_facies_labels)
In [ ]:
clf_comp.fit(scaled_features_comp, correct_facies_labels_comp)
In [ ]:
cv_conf = confusion_matrix(np.concatenate([correct_facies_labels0,correct_facies_labels1]), np.concatenate([clf0.predict(scaled_features0),clf1.predict(scaled_features1)]))
cv_conf_comb = confusion_matrix(correct_facies_labels, clf.predict(scaled_features))
cv_conf_comp = confusion_matrix(correct_facies_labels_comp, clf_comp.predict(scaled_features_comp))
print('Combined split')
print('Optimized facies classification accuracy = %.2f' % accuracy(cv_conf))
print('Optimized adjacent facies classification accuracy = %.2f' % accuracy_adjacent(cv_conf, adjacent_facies,0))
In [ ]:
print('Preprocessed')
print('Optimized facies classification accuracy = %.2f' % accuracy(cv_conf_comb))
print('Optimized adjacent facies classification accuracy = %.2f' % accuracy_adjacent(cv_conf_comb, adjacent_facies,0))
In [ ]:
print('Unprocessed')
print('Optimized facies classification accuracy = %.2f' % accuracy(cv_conf_comp))
print('Optimized adjacent facies classification accuracy = %.2f' % accuracy_adjacent(cv_conf_comp, adjacent_facies,0))
Precision and recall are metrics that give more insight into how the classifier performs for individual facies. Precision is the probability that given a classification result for a sample, the sample actually belongs to that class. Recall is the probability that a sample will be correctly classified for a given class.
Precision and recall can be computed easily using the confusion matrix. The code to do so has been added to the display_confusion_matrix()
function:
In [ ]:
print('Combined split')
display_cm(cv_conf, facies_labels,
display_metrics=True, hide_zeros=True)
In [ ]:
print('Pre-processed')
display_cm(cv_conf_comb, facies_labels,
display_metrics=True, hide_zeros=True)
In [ ]:
print('Un-processed')
display_cm(cv_conf_comp, facies_labels,
display_metrics=True, hide_zeros=True)
To interpret these results, consider facies SS
. In our test set, if a sample was labeled SS
the probability the sample was correct is 0.8 (precision). If we know a sample has facies SS
, then the probability it will be correctly labeled by the classifier is 0.78 (recall). It is desirable to have high values for both precision and recall, but often when an algorithm is tuned to increase one, the other decreases. The F1 score combines both to give a single measure of relevancy of the classifier results.
These results can help guide intuition for how to improve the classifier results. For example, for a sample with facies MS
or mudstone, it is only classified correctly 57% of the time (recall). Perhaps this could be improved by introducing more training samples. Sample quality could also play a role. Facies BS
or bafflestone has the best F1
score and relatively few training examples. But this data was handpicked from other wells to provide training examples to identify this facies.
We can also consider the classification metrics when we consider misclassifying an adjacent facies as correct:
In [ ]:
display_adj_cm(cv_conf, facies_labels, adjacent_facies,
display_metrics=True, hide_zeros=True)
In [ ]:
display_adj_cm(cv_conf_comb, facies_labels, adjacent_facies,
display_metrics=True, hide_zeros=True)
In [ ]:
display_adj_cm(cv_conf_comp, facies_labels, adjacent_facies,
display_metrics=True, hide_zeros=True)
Considering adjacent facies, the F1
scores for all facies types are above 0.9, except when classifying SiSh
or marine siltstone and shale. The classifier often misclassifies this facies (recall of 0.66), most often as wackestone.
These results are comparable to those reported in Dubois et al. (2007).
In [ ]:
wells = processed_data["Well Name"].values
wells0 = processed_data0["Well Name"].values
wells1 = processed_data1["Well Name"].values
wells_comp = training_data["Well Name"].values
logo = LeaveOneGroupOut()
logo0 = LeaveOneGroupOut()
logo1 = LeaveOneGroupOut()
logo_comp = LeaveOneGroupOut()
f1_SVC0 = []
pred0 = {}
for train, test in logo0.split(scaled_features0, correct_facies_labels0, groups=wells0):
well_name = wells0[test[0]]
clf0.fit(scaled_features0[train], correct_facies_labels0[train])
pred0[well_name] = clf0.predict(scaled_features0[test])
sc = f1_score(correct_facies_labels0[test], pred0[well_name], labels = np.arange(10), average = 'micro')
print("{:>20s} {:.3f}".format(well_name, sc))
f1_SVC0.append(sc)
print("-Average leave-one-well-out F1 Score: {0}".format(sum(f1_SVC0)/(1.0*(len(f1_SVC0)))))
In [ ]:
f1_SVC1 = []
pred1 = {}
for train, test in logo1.split(scaled_features1, correct_facies_labels1, groups=wells1):
well_name = wells1[test[0]]
clf1.fit(scaled_features1[train], correct_facies_labels1[train])
pred1[well_name] = clf1.predict(scaled_features1[test])
sc = f1_score(correct_facies_labels1[test], pred1[well_name], labels = np.arange(10), average = 'micro')
print("{:>20s} {:.3f}".format(well_name, sc))
f1_SVC1.append(sc)
print("-Average leave-one-well-out F1 Score: {0}".format(sum(f1_SVC1)/(1.0*(len(f1_SVC1)))))
In [ ]:
f1_SVC_comb = []
for train, test in logo.split(np.concatenate([scaled_features0,scaled_features1]), np.concatenate([correct_facies_labels0,correct_facies_labels1]), groups=np.concatenate([wells0,wells1])):
well_name = np.concatenate([wells0,wells1])[test[0]]
pred=np.concatenate([pred0.get(well_name, []),pred1.get(well_name, [])])
sc = f1_score(np.concatenate([correct_facies_labels0,correct_facies_labels1])[test], pred, labels = np.arange(10), average = 'micro')
print("{:>20s} {:.3f}".format(well_name, sc))
f1_SVC_comb.append(sc)
print("-Average leave-one-well-out F1 Score: {0}".format(sum(f1_SVC_comb)/(1.0*(len(f1_SVC_comb)))))
In [ ]:
f1_SVC_comp = []
pred_comp = {}
for train, test in logo_comp.split(scaled_features_comp, correct_facies_labels_comp, groups=wells_comp):
well_name = wells_comp[test[0]]
clf_comp.fit(scaled_features_comp[train], correct_facies_labels_comp[train])
pred_comp[well_name] = clf_comp.predict(scaled_features_comp[test])
sc = f1_score(correct_facies_labels_comp[test], pred_comp[well_name], labels = np.arange(10), average = 'micro')
print("{:>20s} {:.3f}".format(well_name, sc))
f1_SVC_comp.append(sc)
print("-Average leave-one-well-out F1 Score: {0}".format(sum(f1_SVC_comp)/(1.0*(len(f1_SVC_comp)))))
Now that we have a trained facies classification model we can use it to identify facies in wells that do not have core data. In this case, we will apply the classifier to two wells, but we could use it on any number of wells for which we have the same set of well logs for input.
This dataset is similar to the training data except it does not have facies labels. It is loaded into a dataframe called test_data
.
In [ ]:
well_data = pd.read_csv('../validation_data_nofacies.csv')
well_data['Well Name'] = well_data['Well Name'].astype('category')
well_data0 = well_data.query('NM_M==1')
well_data1 = well_data.query('NM_M==2')
well_features0 = well_data0.drop(['Formation', 'Well Name','Depth','NM_M'], axis=1)
well_features1 = well_data1.drop(['Formation', 'Well Name','Depth','NM_M'], axis=1)
The data needs to be scaled using the same constants we used for the training data.
In [ ]:
X_unknown0 = scaler0.transform(well_features0)
X_unknown1 = scaler1.transform(well_features1)
Finally we predict facies labels for the unknown data, and store the results in a Facies
column of the test_data
dataframe.
In [ ]:
#predict facies of unclassified data
y_unknown0 = clf0.predict(X_unknown0)
y_unknown1 = clf1.predict(X_unknown1)
well_data0['Facies'] = y_unknown0
well_data1['Facies'] = y_unknown1
well_data=pd.concat([well_data0,well_data1], axis=0)
well_data
In [ ]:
well_data['Well Name'].unique()
We can use the well log plot to view the classification results along with the well logs.
In [ ]:
make_facies_log_plot(
well_data[well_data['Well Name'] == 'STUART'],
facies_colors=facies.Colors)
In [ ]:
make_facies_log_plot(
well_data[well_data['Well Name'] == 'CRAWFORD'],
facies_colors=facies.Colors)
Finally we can write out a csv file with the well data along with the facies classification results.
In [ ]:
well_data0['Facies'] = y_unknown0+1
well_data1['Facies'] = y_unknown1+1
well_data=pd.concat([well_data0,well_data1], axis=0)
well_data.to_csv('well_data_with_facies.csv')
Amato del Monte, A., 2015. Seismic Petrophysics: Part 1, The Leading Edge, 34 (4). doi:10.1190/tle34040440.1
Bohling, G. C., and M. K. Dubois, 2003. An Integrated Application of Neural Network and Markov Chain Techniques to Prediction of Lithofacies from Well Logs, KGS Open-File Report 2003-50, 6 pp. pdf
Dubois, M. K., G. C. Bohling, and S. Chakrabarti, 2007, Comparison of four approaches to a rock facies classification problem, Computers & Geosciences, 33 (5), 599-617 pp. doi:10.1016/j.cageo.2006.08.011