<jupyter_start><jupyter_text>Import all the necessary libraries<jupyter_code>import numpy as np
import pandas as pd
import pickle
import math<jupyter_output><empty_output><jupyter_text>Processing the data to be predictedBelow codes demonstrate how to process the data in an xlsx file. You can download the file above.<jupyter_code>## Load the file
file_df = pd.read_excel("example_adsorption_HMsoil.xlsx", sheet_name='Sheet1')
file_df.head()
## Obtain the X_input for the model
X_input = np.array(file_df.iloc[:, 1:])
X_input
len(X_input[1])<jupyter_output><empty_output><jupyter_text>Load the model and perform the prediction<jupyter_code>model_Cd = pickle.load(open('models/Cd.pkl', 'rb'))
model_Cr = pickle.load(open('models/Cr.pkl', 'rb'))
model_Cu = pickle.load(open('models/Cu.pkl', 'rb'))
model_Ni = pickle.load(open('models/Ni.pkl', 'rb'))
model_Pb = pickle.load(open('models/Pb.pkl', 'rb'))
model_Zn = pickle.load(open('models/Zn.pkl', 'rb'))
## Perform the prediction. Use different models for corresponding heavy metals
predict_data = []
for i in range(len(X_input)):
    m = i + 1
    if len(X_input[i]) == 7:
        try:
            if file_df['Heavy metal'][i] == "Cd":
                prediction_individual = model_Cd.predict(np.array([X_input[i]]))
            elif file_df['Heavy metal'][i] == "Cr":
                prediction_individual = model_Cr.predict(np.array([X_input[i]]))
            elif file_df['Heavy metal'][i] == "Cu":
                prediction_individual = model_Cu.predict(np.array([X_input[i]]))
            elif file_df['Heavy metal'][i] == "Ni":
                prediction_individual = model_Ni.predict(np.array([X_input[i]]))
            elif file_df['Heavy metal'][i] == "Pb":
                prediction_individual = model_Pb.predict(np.array([X_input[i]]))
            elif file_df['Heavy metal'][i] == "Zn":
                prediction_individual = model_Zn.predict(np.array([X_input[i]]))
            else:
                prediction_individual = "Invalid entry"

            predict_row = [m] + np.array(file_df.iloc[i, :]).tolist() + [round(prediction_individual[0], 3), round(math.exp(prediction_individual[0]), 3)]
        except:
            predict_row = [m] + np.array(file_df.iloc[i, :]).tolist() + ['Invalid entry', 'Invalid entry']
    else:
        predict_row = [m] + np.array(file_df.iloc[i, :]).tolist() + ['Invalid entry', 'Invalid entry']

    predict_data.append(predict_row)

pred_df = pd.DataFrame(predict_data)
headings = ["Number"] + list(file_df.columns.values) + ["Ln-Adsorption (mg/L)", "Adsorption (mg/L)"]
pred_df.columns = headings
pred_df<jupyter_output><empty_output><jupyter_text>Save the results to a csv file<jupyter_code>pred_df.to_csv("prediction_result.csv", index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15298776097912167, 'fraction_numerical': 0.0032397408207343412, 'mean_word_length': 4.052727272727273, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '240824', 'license': 'MIT', 'n_tokens_mistral': 1003, 'n_tokens_neox': 958, 'n_words': 187, 'path': 'example/Adsorption_HMsoil_prediction_example.ipynb', 'repo_name': 'KuanHuang/predicting-heavy-metal-adsorption-in-soil', 'size': '2778'}