<jupyter_start><jupyter_text>Compare regression models for lithium-ion conducitng molecules<jupyter_code>import joblib
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

df=joblib.load("data/df.bin")        # data without salts
df=joblib.load("data/df_salt.bin")  # data with salts
df

target_param = "Conductivity"
from sklearn.metrics import mean_squared_error, r2_score

def rmse(true,pred):
    return mean_squared_error(true, pred,squared=False)

def eval(model,train_df,test_df,plot=True):

    column_list = list(df.columns)
    column_list.remove(target_param)

    train_x = list(train_df[column_list].values)
    train_y = list(train_df[target_param])
    test_x = list(test_df[column_list].values)
    test_y = list(test_df[target_param])

    model.fit(train_x, train_y)
    #Q = np.diag(model.coef_)

    p_tr_y = model.predict(train_x)
    p_te_y = model.predict(test_x)

    res_dict={}
    res_dict["R2 Train"]=r2_score(train_y, p_tr_y)
    res_dict["R2 Test"]=r2_score(test_y, p_te_y)
    res_dict["RMSE Train"]=rmse(train_y, p_tr_y)
    res_dict["RMSE Test"]=rmse(test_y, p_te_y)

    if plot:
        plt.rcParams['xtick.direction'] = 'in'
        plt.rcParams['ytick.direction'] = 'in'
        plt.rcParams['xtick.major.width'] = 0.5
        plt.rcParams['ytick.major.width'] = 0.5
        plt.rcParams["font.size"] = 8

        plt.figure(figsize=(2, 2), dpi=150)
        plt.scatter(train_y, p_tr_y, s=2, alpha=0.5)
        plt.scatter(test_y, p_te_y, s=2, alpha=0.5)


        vmax = int(max(df[target_param]))
        vmin = int(min(df[target_param]))

        plt.xlim(vmin, vmax)
        plt.ylim(vmin, vmax)
        tick_step = 1

        plt.plot([vmin, vmax], [vmin, vmax],
                c="gray",
                linewidth=0.3)

        plt.xlabel("Experimental")
        plt.ylabel("Predicted")
        plt.xticks(np.arange(vmin, vmax+1, step=tick_step))
        plt.yticks(np.arange(vmin, vmax+1, step=tick_step))


        print("Train R2 ", r2_score(train_y, p_tr_y))
        print("Test R2 ", r2_score(test_y, p_te_y))

    return res_dict
from sklearn.cross_decomposition import PLSRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.svm import SVR

model_dict={}

model_dict["PLS"]=PLSRegression(n_components=30)
model_dict["RFR"]=RandomForestRegressor()
model_dict["SVR"]=SVR()

import copy
y_rand_df=copy.copy(df)
y_df=y_rand_df[target_param]
y_df=y_df.sample(frac=1)
y_rand_df[target_param]=y_df.values


mode_dict={}
mode_dict["Normal"]=df
#mode_dict["y-randomization"]=y_rand_df
from tqdm import tqdm

res_dict={}

for i in  tqdm(range(10)):
    for mode in mode_dict:
        target_df=mode_dict[mode]
        train_df, test_df = train_test_split(target_df, test_size=0.2)

        for model_name,model in model_dict.items():
            if i==0:
                plot=True
            else:
                plot=False


            temp_dict=eval(model,train_df,test_df,plot=plot)
            temp_dict["Model"]=model_name
            temp_dict["Trial"]=i
            temp_dict["Mode"]=mode
            res_dict[f"{i}-{model_name}-{mode}"]=temp_dict

res_df=pd.DataFrame.from_dict(res_dict).T
res_df
import seaborn as sns
for y_label in ["RMSE Test"]:

    plt.figure(figsize=(4, 2),dpi=300)
    sns.swarmplot(x="Model", y=y_label, data=res_df,hue="Mode")
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=18)
    plt.xlabel("")
    plt.ylabel(y_label)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1329025007024445, 'fraction_numerical': 0.014048890137679123, 'mean_word_length': 3.5935483870967744, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '235538', 'license': 'MIT', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1364, 'n_words': 217, 'path': '5_composite_extraction/2_Compare_regression.ipynb', 'repo_name': 'KanHatakeyama/anneal_project2', 'size': '3559'}