### Matrix Factorization for Well Formation Tops (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Implements matrix factorization for identifying well formation tops, functioning as a recommender system for automatic picking of subsurface formation tops. ```Python import pandas as pd import numpy as np from sklearn.decomposition import NMF # Load well formation data # Assume 'formation_data.csv' contains data where rows are wells and columns are potential formation tops # The values could represent the depth or a binary indicator of presence. formation_data = pd.read_csv('formation_data.csv', index_col=0) # Handle missing values if necessary (e.g., fill with 0 if absence means no formation top detected) formation_data.fillna(0, inplace=True) # Initialize and train the Non-negative Matrix Factorization (NMF) model # n_components determines the number of latent factors (e.g., geological concepts) n_components = 10 # Example: Adjust based on domain knowledge model = NMF(n_components=n_components, init='random', random_state=42) W = model.fit_transform(formation_data) # User-feature matrix H = model.components_ # Feature-item matrix # W matrix: Represents each well in terms of the latent factors. # H matrix: Represents each latent factor in terms of the formation tops. # Example: Recommending formation tops for a specific well (e.g., well_id = 'Well_A') well_id = 'Well_A' if well_id in formation_data.index: well_vector = W[formation_data.index.get_loc(well_id)] # Calculate scores for each formation top based on the well's latent factors formation_scores = np.dot(well_vector, H) # Get the indices of the top N formation tops for this well top_n = 5 top_formation_indices = np.argsort(formation_scores)[::-1][:top_n] # Map indices back to formation top names formation_names = formation_data.columns recommended_formations = [formation_names[i] for i in top_formation_indices] print(f'Recommended formation tops for {well_id}: {recommended_formations}') else: print(f'Well ID {well_id} not found in the data.') # This is a simplified example. Real-world application would involve more sophisticated data prep and evaluation. ``` -------------------------------- ### Petrophysics Python Series for Well Log Analysis Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md A series of Python tools and scripts focused on petrophysics and well-log analysis. The repository provides code for various petrophysical calculations and interpretations. ```Python https://github.com/andymcdgeo/Petrophysics-Python-Series ``` -------------------------------- ### Imputation Benchmark for ML Methods (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md A benchmark study published in Computers and Geosciences comparing machine learning methods for well log data imputation, with associated code and dataset. ```Python # This code corresponds to the benchmark study on well log imputation. # It likely involves implementing and evaluating various ML algorithms for imputation. import pandas as pd import numpy as np from sklearn.experimental import enable_iterative_imputer from sklearn.impute import IterativeImputer from sklearn.ensemble import RandomForestRegressor from sklearn.linear_model import BayesianRidge from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split # Load the dataset (e.g., from Zenodo or a local file) # Assuming 'well_log_data_with_missing.csv' contains well log data with NaNs data = pd.read_csv('well_log_data_with_missing.csv') # Define features and the target column to be imputed features = ['GR', 'Resistivity', 'Porosity', 'Caliper'] # Example features target_column = 'NMR' # Example target column to impute X = data[features] y = data[target_column] # Split data into training and testing sets for evaluation X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # --- Imputation Method 1: IterativeImputer with RandomForestRegressor --- print("--- Imputing with IterativeImputer (RandomForestRegressor) ---") imputer_rf = IterativeImputer(estimator=RandomForestRegressor(n_estimators=100, random_state=42), max_iter=10, random_state=42) # Fit on training data and transform both train and test sets X_train_imputed_rf = imputer_rf.fit_transform(X_train) X_test_imputed_rf = imputer_rf.transform(X_test) # Evaluate imputation quality (if y_test has NaNs, this needs careful handling) # For simplicity, let's assume we are evaluating imputation of a feature column # If imputing the target, we'd compare imputed y_test with original y_test # Example: Imputing a feature column 'Resistivity' within the dataset feature_to_impute = 'Resistivity' # Create a copy to avoid modifying original data during imputation tests data_copy = data.copy() # Identify rows with missing values in the target column missing_indices = data_copy[data_copy[target_column].isna()].index # Prepare data for imputation of the target column X_impute = data_copy.drop(columns=[target_column]) y_impute = data_copy[target_column] # Impute missing values in X using a simple strategy first (e.g., mean) X_impute_filled = X_impute.fillna(X_impute.mean()) # Split data for training the imputer model X_train_imputer, X_test_imputer, y_train_imputer, y_test_imputer = train_test_split(X_impute_filled, y_impute, test_size=0.2, random_state=42) # Train the imputer model imputer_model = IterativeImputer(estimator=RandomForestRegressor(n_estimators=100, random_state=42), max_iter=10, random_state=42) imputer_model.fit(X_train_imputer) # Predict the missing values y_imputed_values = imputer_model.predict(X_test_imputer) # Evaluate the imputation performance using RMSE on the test set # Note: This assumes y_test_imputer contains the actual values for comparison rmse = mean_squared_error(y_test_imputer, y_imputed_values, squared=False) print(f'RMSE for {target_column} imputation (RandomForest): {rmse}') # --- Imputation Method 2: IterativeImputer with BayesianRidge --- print("--- Imputing with IterativeImputer (BayesianRidge) ---") imputer_br = IterativeImputer(estimator=BayesianRidge(), max_iter=10, random_state=42) imputer_br.fit(X_train_imputer) # Fit on the same training data y_imputed_values_br = imputer_br.predict(X_test_imputer) rmse_br = mean_squared_error(y_test_imputer, y_imputed_values_br, squared=False) print(f'RMSE for {target_column} imputation (BayesianRidge): {rmse_br}') # The benchmark would compare RMSE values across different methods and parameters. ``` -------------------------------- ### Machine Learning for Oil and Gas Projects (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md A collection of projects focused on applying machine learning techniques to problems in the oil and gas industry, including well-log analysis. ```Python # This repository contains various ML projects for Oil and Gas. # Example: A placeholder for a project using ML for well log analysis. import pandas as pd import numpy as np from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split from sklearn.metrics import r2_score # Load well log data (example) # Assume 'well_log_data.csv' has features like GR, Resistivity, Porosity and a target like NMR well_log_data = pd.read_csv('well_log_data.csv') # Feature Engineering and Preprocessing (example) # Handle missing values, scale features, etc. well_log_data.fillna(method='ffill', inplace=True) # Define features (X) and target (y) features = ['GR', 'Resistivity', 'Porosity'] target = 'NMR' X = well_log_data[features] y = well_log_data[target] # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Initialize and train a model (e.g., RandomForestRegressor) model = RandomForestRegressor(n_estimators=100, random_state=42) model.fit(X_train, y_train) # Make predictions y_pred = model.predict(X_test) # Evaluate the model r2 = r2_score(y_test, y_pred) print(f'R-squared score: {r2}') # Further analysis and deployment would follow. ``` -------------------------------- ### Inclination Prediction using Recurrent Neural Networks (RNN) (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Applies a training-while-drilling approach using Recurrent Neural Networks (RNNs) for inclination prediction in directional drilling, as detailed in the Journal of Petroleum Science and Engineering. ```Python # This code demonstrates a conceptual implementation of using RNNs for inclination prediction. # It requires a deep learning framework like TensorFlow/Keras or PyTorch. import pandas as pd import numpy as np # Import necessary deep learning libraries # from tensorflow.keras.models import Sequential # from tensorflow.keras.layers import LSTM, Dense, Dropout # from sklearn.preprocessing import MinMaxScaler # from sklearn.model_selection import train_test_split # Load drilling data # Assuming 'drilling_data.csv' contains time-series data including depth, inclination, and other relevant parameters data = pd.read_csv('drilling_data.csv') # --- Data Preprocessing --- # Select relevant features and the target variable (inclination) # Features might include previous inclination values, depth, rate of penetration (ROP), etc. features = ['Depth', 'Prev_Inclination', 'ROP', 'Torque'] # Example features target = 'Inclination' X = data[features] y = data[target] # Scale features (important for neural networks) # scaler_X = MinMaxScaler() # X_scaled = scaler_X.fit_transform(X) # scaler_y = MinMaxScaler() # y_scaled = scaler_y.fit_transform(y.values.reshape(-1, 1)) # --- Prepare data for RNN (Sequence Creation) --- # RNNs require data in sequences (e.g., using past N time steps to predict the next) # def create_sequences(X, y, time_steps): # Xs, ys = [], [] # for i in range(len(X) - time_steps): # Xs.append(X[i:(i + time_steps)]) # ys.append(y[i + time_steps]) # return np.array(Xs), np.array(ys) # time_steps = 10 # Example: Use the last 10 data points to predict the next # X_sequences, y_sequences = create_sequences(X_scaled, y_scaled, time_steps) # Split data into training and testing sets # X_train, X_test, y_train, y_test = train_test_split(X_sequences, y_sequences, test_size=0.2, random_state=42) # --- Build the RNN Model (LSTM example) --- # model = Sequential() # model.add(LSTM(units=50, return_sequences=True, input_shape=(time_steps, X_train.shape[2]))) # model.add(Dropout(0.2)) # model.add(LSTM(units=50, return_sequences=False)) # model.add(Dropout(0.2)) # model.add(Dense(units=1)) # Compile the model # model.compile(optimizer='adam', loss='mean_squared_error') # --- Train the Model --- # history = model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.1, verbose=1) # --- Prediction and Evaluation --- # y_pred_scaled = model.predict(X_test) # y_pred = scaler_y.inverse_transform(y_pred_scaled) # y_test_original = scaler_y.inverse_transform(y_test) # Calculate evaluation metrics (e.g., RMSE, MAE) # rmse = np.sqrt(mean_squared_error(y_test_original, y_pred)) # print(f'RMSE for inclination prediction: {rmse}') # This is a conceptual outline. Actual implementation requires careful data preparation, # hyperparameter tuning, and potentially more complex RNN architectures. ``` -------------------------------- ### Machine Learning Competition (e.g., FORCE 2020) (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Code and resources related to machine learning competitions in the geosciences, such as the FORCE 2020 Machine Learning Competition, focusing on well-log and seismic data. ```Python # This repository likely contains code submissions or baseline models for ML competitions. # Example structure for a competition submission script: import pandas as pd import numpy as np # Import necessary libraries for feature engineering, modeling, etc. # from sklearn.model_selection import KFold # from sklearn.ensemble import RandomForestClassifier # Load training and test data train_data = pd.read_csv('train.csv') test_data = pd.read_csv('test.csv') # --- Feature Engineering --- # Example: Create new features from existing ones # train_data['new_feature'] = train_data['feature1'] / train_data['feature2'] # test_data['new_feature'] = test_data['feature1'] / test_data['feature2'] # --- Preprocessing --- # Handle missing values, scale features, encode categorical variables # train_data.fillna(method='ffill', inplace=True) # test_data.fillna(method='ffill', inplace=True) # Define features (X) and target (y) # features = ['feature1', 'feature2', 'new_feature'] # target = 'target_variable' # X_train = train_data[features] # y_train = train_data[target] # X_test = test_data[features] # --- Model Training --- # Initialize and train a model (e.g., RandomForestClassifier) # model = RandomForestClassifier(n_estimators=200, random_state=42) # model.fit(X_train, y_train) # --- Prediction --- # Make predictions on the test set # predictions = model.predict(X_test) # --- Submission File Generation --- # Create a submission file in the required format (e.g., CSV with IDs and predictions) # submission_df = pd.DataFrame({'ID': test_data['ID'], 'Prediction': predictions}) # submission_df.to_csv('submission.csv', index=False) # print('Submission file created successfully!') # Note: Actual code would depend heavily on the specific competition dataset and task. ``` -------------------------------- ### Well Log Imputation Benchmark (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md A comprehensive benchmark for machine learning methods in well log data imputation. It includes multiple ML algorithms and standardized evaluation protocols. ```Python import pandas as pd from sklearn.impute import SimpleImputer from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error # Load the well log data # Assuming 'data.csv' contains your well log data with a target column for imputation data = pd.read_csv('data.csv') # Separate features and target X = data.drop('target_log', axis=1) y = data['target_log'] # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Handle missing values in features (if any) X_train_imputed = SimpleImputer(strategy='mean').fit_transform(X_train) X_test_imputed = SimpleImputer(strategy='mean').fit_transform(X_test) # --- Example: Using a simple ML model (e.g., Linear Regression) for imputation --- # In a real benchmark, you would test multiple ML algorithms here. from sklearn.linear_model import LinearRegression imputer_model = LinearRegression() imputer_model.fit(X_train_imputed, y_train) # Predict missing values y_pred = imputer_model.predict(X_test_imputed) # Evaluate the imputation performance rmse = mean_squared_error(y_test, y_pred, squared=False) print(f'RMSE for imputation: {rmse}') # To impute missing values in the original dataset: # imputed_data = pd.DataFrame(SimpleImputer(strategy='mean').fit_transform(data), columns=data.columns) # Or using the trained model for specific columns if needed. ``` -------------------------------- ### Recurrent Neural Networks (RNN) for Synthetic Well Log Generation Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Applies Recurrent Neural Networks (RNN) to generate synthetic well logs. This method is documented in Petroleum Exploration and Development and the associated code can be found on GitHub. ```Python https://github.com/YuntianChen/cascaded_EnLSTM ``` -------------------------------- ### Multivariate Imputation via Chained Equations (MICE) for Well Log Imputation (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Implements Multivariate Imputation by Chained Equations (MICE) using LGBM for elastic well log imputation and prediction, as published in Applied Computing and Geosciences. ```Python # This code implements MICE using LightGBM (LGBM) for well log imputation. # It requires the 'fancyimpute' library or a similar implementation of MICE. # Ensure you have the necessary libraries installed: # pip install pandas numpy scikit-learn lightgbm fancyimpute import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error from lightgbm import LGBMRegressor # Assuming 'fancyimpute' is installed and available # If not, you might need to find an alternative implementation or install it. try: from fancyimpute import IterativeImputer except ImportError: print("fancyimpute library not found. Please install it: pip install fancyimpute") # As a fallback, we can use scikit-learn's IterativeImputer with LGBM from sklearn.experimental import enable_iterative_imputer from sklearn.impute import IterativeImputer print("Using scikit-learn's IterativeImputer as a fallback.") # Load well log data # Assuming 'well_log_data.csv' contains well log data with missing values data = pd.read_csv('well_log_data.csv') # Define features and the target column to impute features = ['GR', 'Resistivity', 'Porosity', 'Caliper'] # Example features target_column = 'NMR' # Example target column to impute X = data[features] y = data[target_column] # Combine features and target for imputation process combined_data = pd.concat([X, y], axis=1) # Handle missing values in features if any, before imputation combined_data.fillna(method='ffill', inplace=True) # Split data into training and testing sets for evaluation X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Prepare data for the imputer (ensure no NaNs in features used by the imputer) X_train_imputer = X_train.fillna(X_train.mean()) X_test_imputer = X_test.fillna(X_test.mean()) # Initialize the MICE imputer with LGBMRegressor as the estimator # The 'estimator' parameter specifies the model used for imputation in each step. # 'max_iter' controls the number of imputation rounds. # Using LGBMRegressor as the estimator for MICE imputer = IterativeImputer(estimator=LGBMRegressor(random_state=42), max_iter=10, # Number of imputation rounds random_state=42) # Fit the imputer on the training data and transform it X_train_imputed = imputer.fit_transform(X_train_imputer) # Transform the test data using the fitted imputer X_test_imputed = imputer.transform(X_test_imputer) # Convert back to DataFrame for clarity X_train_imputed_df = pd.DataFrame(X_train_imputed, columns=X_train.columns, index=X_train.index) X_test_imputed_df = pd.DataFrame(X_test_imputed, columns=X_test.columns, index=X_test.index) # Now, X_train_imputed_df and X_test_imputed_df have no missing values in the specified features. # If the goal was to impute the target column 'NMR' itself: # We would need to structure the data differently for IterativeImputer # For example, create a matrix where the target column also has NaNs. # Example of imputing the target column 'NMR' using MICE: # Create a matrix with the target column having NaNs for imputation # We need to ensure all columns used by the imputer are present and have no NaNs initially # Let's assume 'data' has NaNs in 'NMR' and potentially other columns # Prepare the full dataset for imputation # Ensure all columns used by the imputer are clean or imputed first full_data_for_imputation = data.copy() full_data_for_imputation.fillna(method='ffill', inplace=True) # Simple forward fill for demonstration # Identify the column to impute column_to_impute = 'NMR' # Create the matrix for imputation (features + target column with NaNs) # For IterativeImputer, it's best to provide the full matrix with NaNs # Let's simulate missing values in NMR for demonstration np.random.seed(42) missing_fraction = 0.1 num_missing = int(missing_fraction * len(full_data_for_imputation)) missing_indices = np.random.choice(full_data_for_imputation.index, num_missing, replace=False) full_data_for_imputation.loc[missing_indices, column_to_impute] = np.nan # Now, impute the 'NMR' column using MICE with LGBM imputer_full = IterativeImputer(estimator=LGBMRegressor(random_state=42), max_iter=10, random_state=42) imputed_data_matrix = imputer_full.fit_transform(full_data_for_imputation) imputed_df = pd.DataFrame(imputed_data_matrix, columns=full_data_for_imputation.columns, index=full_data_for_imputation.index) # Now 'imputed_df' contains the data with 'NMR' column imputed. # You can evaluate the imputation quality by comparing imputed values with original values (if available for a subset) # Or by downstream task performance. print(f"Imputed data shape: {imputed_df.shape}") print(f"Number of NaNs in original '{column_to_impute}' column: {data[column_to_impute].isna().sum()}") print(f"Number of NaNs in imputed '{column_to_impute}' column: {imputed_df[column_to_impute].isna().sum()}") ``` -------------------------------- ### Synthetic Well Log Generation via Polynomial Regression (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Constructs a missing well log (e.g., NMR) from other available well logs (e.g., Gamma Ray, Caliper, Resistivity, Porosity) using polynomial regression. ```Python import pandas as pd import numpy as np from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # Load well log data # Assuming 'well_logs.csv' contains GR, Caliper, Resistivity, Porosity, and NMR logs data = pd.read_csv('well_logs.csv') # Define features (X) and target (y) # We want to predict NMR (y) using GR, Caliper, Resistivity, and Porosity (X) features = ['GR', 'Caliper', 'Resistivity', 'Porosity'] target = 'NMR' X = data[features] y = data[target] # Handle potential missing values (e.g., by imputation or removal) X.fillna(method='ffill', inplace=True) y.fillna(method='ffill', inplace=True) # Generate polynomial features # Degree can be adjusted based on the complexity of the relationship poly = PolynomialFeatures(degree=3) X_poly = poly.fit_transform(X) # Initialize and train the Linear Regression model model = LinearRegression() model.fit(X_poly, y) # Predict the synthetic NMR log y_pred_synthetic = model.predict(X_poly) # Evaluate the model (optional, if you have a validation set) # mse = mean_squared_error(y_true, y_pred_synthetic) # print(f'Mean Squared Error: {mse}') # Add the synthetic log to the dataframe data['NMR_synthetic'] = y_pred_synthetic # Display the first few rows with the synthetic log print(data[['GR', 'Caliper', 'Resistivity', 'Porosity', 'NMR', 'NMR_synthetic']].head()) ``` -------------------------------- ### Ensemble Long Short-Term Memory (EnLSTM) for Well Log Generation Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Implements an Ensemble Long Short-Term Memory (EnLSTM) network for generating well logs. This approach is detailed in Geophysical Research Letters and IEEE Transactions on Geoscience and Remote Sensing. The code is available on GitHub. ```Python https://github.com/YuntianChen/EnLSTM?tab=readme-ov-file ``` -------------------------------- ### Well Log Prediction (Python) Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Predicts Equinor Volve well logs data using machine learning models. This involves training a model on historical data to forecast future well log values. ```Python import pandas as pd import numpy as np from sklearn.ensemble import GradientBoostingRegressor from sklearn.model_selection import train_test_split from sklearn.metrics import mean_absolute_error # Load the Equinor Volve well logs dataset # Assuming 'volve_well_logs.csv' contains the relevant data volve_data = pd.read_csv('volve_well_logs.csv') # Data Preprocessing and Feature Selection # Select relevant features and target variable # Example: Predicting 'GR' using 'Depth', 'ILD_log10', 'NPHI', 'RHOB' features = ['Depth', 'ILD_log10', 'NPHI', 'RHOB'] target = 'GR' X = volve_data[features] y = volve_data[target] # Handle missing values (e.g., using imputation) X.fillna(method='ffill', inplace=True) y.fillna(method='ffill', inplace=True) # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Initialize and train a regression model (e.g., Gradient Boosting Regressor) model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42) model.fit(X_train, y_train) # Make predictions on the test set y_pred = model.predict(X_test) # Evaluate the model performance mae = mean_absolute_error(y_test, y_pred) print(f'Mean Absolute Error for GR prediction: {mae}') # You can then use the trained model to predict GR values for new data points. ``` -------------------------------- ### Ensemble Neural Networks (ENN) for Well Log Analysis Source: https://github.com/lumisong/awesome-well-log-ml-dl/blob/main/README.md Utilizes Ensemble Neural Networks (ENN), a gradient-free stochastic method, for well-log analysis. This technique is described in Neural Networks and has its official code available on GitHub. ```Python https://github.com/YuntianChen/ENN ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.