<jupyter_start><jupyter_text>Classification of pH range using machine learning  Setup  Colab<jupyter_code>from google.colab import drive
HOME_DIR = '/content/drive/My Drive/Data_Translators/acidoCEST-ML/'
drive.mount('/content/drive')
%pip install scikit-plot<jupyter_output>Collecting scikit-plot
  Downloading scikit_plot-0.3.7-py3-none-any.whl (33 kB)
Requirement already satisfied: scipy>=0.9 in /usr/local/lib/python3.7/dist-packages (from scikit-plot) (1.4.1)
Requirement already satisfied: joblib>=0.10 in /usr/local/lib/python3.7/dist-packages (from scikit-plot) (1.1.0)
Requirement already satisfied: scikit-learn>=0.18 in /usr/local/lib/python3.7/dist-packages (from scikit-plot) (1.0.1)
Requirement already satisfied: matplotlib>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from scikit-plot) (3.2.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=1.4.0->scikit-plot) (1.3.2)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=1.4.0->scikit-plot) (3.0.6)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=1.4.0->scikit-plot) (0.11.0)
Requirement already satisfied[...]<jupyter_text>Libraries<jupyter_code>#libs
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn import linear_model
import pandas as pd
import numpy as np
import scikitplot as skplt
import matplotlib.pyplot as plt
from sklearn import metrics

from sklearn.pipeline import Pipeline
from sklearn import preprocessing as pp
import seaborn as sns

from sklearn.feature_selection import SelectFromModel

from joblib import dump, load<jupyter_output><empty_output><jupyter_text>Data<jupyter_code># load data
drive_path   = HOME_DIR + 'clean_data/acido_CEST_MRI_Iopamidol.parquet.gzip'
acidoCEST_ML = pd.read_parquet(drive_path)

# drop columns we cannot measure or that are redundant
cols_2_drop = ['ApproT1(sec)','Temp','ExpB0(ppm)','FILE','Conc(mM)']
acidoCEST_ML = acidoCEST_ML.drop(cols_2_drop, axis = 1)

# define experimental columns -- > things we measure other than CEST
exper_cols = [ 'ExpB1(percent)', 'ExpB0(Hz)', 'SatPower(uT)',  'SatTime(ms)','ExpT1(ms)', 'ExpT2(ms)']

# used during development
#acidoCEST_ML  = acidoCEST_ML.sample( n = 1_000)

acidoCEST_ML.shape<jupyter_output><empty_output><jupyter_text>Functions  Random Forest<jupyter_code># Regression
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.ensemble import RandomForestRegressor as RFR

def train_RFR(Xdata, pH_observed):
    print('------------------ Random Forest ------------------ ')
    print()
    # Split data
    X_train, X_test, y_train, y_test = train_test_split(Xdata, pH_observed, test_size=0.30, random_state=42)
        
        
    
    max_f = int( 0.5 * Xdata.shape[1] )
        
    # Regression
    #param_grid = dict( n_estimators = [100,200,500], max_depth =[None], max_features=['sqrt','log2','auto',max_f], max_samples=[.10,.50], min_samples_leaf=[.01,.50] )

    
    param_grid = dict( n_estimators = [10,20,50,100], max_depth =[10,20,40] )

    
    # grid parameters
    scorer = metrics.make_scorer( metrics.r2_score, greater_is_better = True) 
    
    #
    estimator = GridSearchCV( RFR(random_state = 42, criterion = 'absolute_error', n_jobs = -1), param_grid, verbose = 3, cv = 3, n_jobs= 1, scoring=scorer )

    # fit
    estimator.fit(X_train, y_train)

    score_train = np.round( 100 * metrics.mean_absolute_percentage_error(y_train , estimator.predict(X_train)), 2)
    score_test  = np.round( 100 * metrics.mean_absolute_percentage_error(y_test , estimator.predict(X_test)), 2)



    m = f' mean absolute percentage error \n train = {score_train} % \n test  = {score_test} %'
    
    #print(', '.join(Xdata.columns.to_list()))
    print()
    print(m)
   
    plt.figure()
    sns.regplot(x = estimator.best_estimator_.predict(X_train), y =  y_train)
    plt.xlabel('\n Predicted pH')
    plt.ylabel('Measured pH \n')
    plt.title('Train Random Forest')
    
    plt.figure()
    sns.regplot(x = estimator.best_estimator_.predict(X_test), y =  y_test)
    plt.xlabel('\n Predicted pH')
    plt.ylabel('Measured pH \n')
    plt.title('Test Random Forest')
    
    print(estimator.best_estimator_)
    
    return estimator.best_estimator_<jupyter_output><empty_output><jupyter_text>Results  Random Forest  4.2 and 5.6 only<jupyter_code>%%time
X = acidoCEST_ML[['4.2','5.6']]
Y = acidoCEST_ML.pH
reg_rf = train_RFR(X,Y)
name = 'RF4256'
dump(reg_rf, HOME_DIR + f'{name}.joblib') 
#regressor = load(HOME_DIR + f'{name}.joblib')<jupyter_output><empty_output><jupyter_text>Only CEST data<jupyter_code>%%time
Xdata = acidoCEST_ML.drop(exper_cols + ['pH'],axis=1)
Ydata = acidoCEST_ML.pH 

reg_rf = train_RFR(Xdata,Ydata)
name = 'all_CEST'
dump(reg_rf, HOME_DIR + f'{name}.joblib')<jupyter_output>------------------ Random Forest ------------------ 

Fitting 3 folds for each of 12 candidates, totalling 36 fits
[CV 1/3] END .....max_depth=10, n_estimators=10;, score=0.531 total time= 3.3min
[CV 2/3] END .....max_depth=10, n_estimators=10;, score=0.529 total time= 3.6min
[CV 3/3] END .....max_depth=10, n_estimators=10;, score=0.532 total time= 3.4min
[CV 1/3] END .....max_depth=10, n_estimators=20;, score=0.554 total time= 6.2min
[CV 2/3] END .....max_depth=10, n_estimators=20;, score=0.536 total time= 7.2min
[CV 3/3] END .....max_depth=10, n_estimators=20;, score=0.544 total time= 7.0min
[CV 1/3] END .....max_depth=10, n_estimators=50;, score=0.568 total time=15.5min
[CV 2/3] END .....max_depth=10, n_estimators=50;, score=0.542 total time=18.0min
[CV 3/3] END .....max_depth=10, n_estimators=50;, score=0.555 total time=17.3min
[CV 1/3] END ....max_depth=10, n_estimators=100;, score=0.569 total time=31.2min
[CV 2/3] END ....max_depth=10, n_estimators=100;, score=0.547 total time=35[...]<jupyter_text>All data<jupyter_code>%%time
Xdata = acidoCEST_ML.drop( ['pH'],axis=1) 
Ydata = acidoCEST_ML.pH

reg_rf = train_RFR(Xdata,Ydata)
name = 'all_DATA'
dump(reg_rf, HOME_DIR + f'{name}.joblib')<jupyter_output><empty_output><jupyter_text>Feature Importance<jupyter_code>I = pd.DataFrame( 100 * reg_rf_all.feature_importances_, columns=['Importance'], index = Xdata.columns)
_, ax = plt.subplots(dpi = 100, figsize=(16,6))

I.sort_values('Importance').plot(kind='bar', ax = ax, rot=90)<jupyter_output><empty_output><jupyter_text>Recursive Feature elimination<jupyter_code>%%time
selected_RF = SelectFromModel( reg_rf_all  , max_features=20) 
selected_RF.fit(Xdata, Ydata)
cols = Xdata.columns[selected_RF.get_support()]
print(cols)

Xdata = acidoCEST_ML[cols]
Ydata = acidoCEST_ML.pH

reg_rf = train_RFR(Xdata,Ydata)

name = 'reg_rf_selected'
dump(reg_rf, HOME_DIR + f'{name}.joblib')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15301100173711638, 'fraction_numerical': 0.04617834394904458, 'mean_word_length': 4.7004950495049505, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '237166', 'license': 'MIT', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2669, 'n_words': 518, 'path': 'notebooks/2-Regression_colab_RF_only.ipynb', 'repo_name': 'JCardenasRdz/acidoCEST-ML', 'size': '6908'}