image_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

df_test = pd.read_csv('/kaggle/input/ml-for-exploration-geophysics-2022-regression/test.csv')
df_train = pd.read_csv('/kaggle/input/ml-for-exploration-geophysics-2022-regression/train.csv')

feature_names = df_train.columns[:-1].tolist()
label_names = df_train.columns[-1]

def make_log_plot(df):
    
    color_list = ['#F4D03F', '#F5B041','#DC7633','#6E2C00', '#1B4F72','#2E86C1', '#AED6F1', '#A569BD', '#196F3D']
    feature_names = df.columns.tolist()  
    feature_num = len(feature_names)
    Depth = np.linspace(0,len(df[feature_names[0]]),len(df[feature_names[0]]))
   
    f, ax = plt.subplots(nrows=1, ncols=feature_num, figsize=(12, 12))

    for i in range(len(ax)):
        log = df[feature_names[i]]
        ax[i].plot(log, Depth, '-', color=color_list[i])
        ax[i].set_ylim(Depth.min(),Depth.max())
        ax[i].invert_yaxis()
        ax[i].grid()
        ax[i].locator_params(axis='x', nbins=3)
        ax[i].set_xlabel(feature_names[i])
        ax[i].set_xlim(log.min(),log.max())
        if i > 0:
            ax[i].set_yticklabels([]); 
    f.suptitle('Well logs', fontsize=14,y=0.94)

X_train_orig = df_train[feature_names].values
X_test_orig = df_test[feature_names].values
Y_train_orig = df_train['DTC'].values
scaler = StandardScaler()
scaler.fit(X_train_orig)
X_train_norm = scaler.transform(X_train_orig)
X_test_norm = scaler.transform(X_test_orig)

X_train, X_val, y_train, y_val = train_test_split(X_train_norm, Y_train_orig, test_size=0.2, random_state=1, shuffle=True)

def evaluate_regression(reg, X_test, y_test):
    
    R2 = reg.score(X_test, y_test)
    y_pred = reg.predict(X_test)
    RMSE = mean_squared_error(y_test, y_pred, squared=False)

    plt.figure(figsize=(15, 8))
    f, (ax1, ax2) = plt.subplots(1, 2)

    ax1.scatter(
        y_test,
        y_pred)
    ax1.set_xlabel('True')
    ax1.set_ylabel('Predicted')
    
    ax2.plot(y_test, linewidth=2, label="True")
    ax2.plot(y_pred, linewidth=2, label="Predicted")
    ax2.legend(loc='lower right')
    ax2.set_xlabel('Sample')
    ax2.set_ylabel('DTC')
    plt.show()
    
    print(f'R2 = {R2}')
    print(f'RMSE = {RMSE}')

reg = LinearRegression()
reg.fit(X_train, y_train)
evaluate_regression(reg, X_val, y_val)