如何将XGBoost模型保存在ipynb中并加载到javascript中以调用模型并提示用户输入并获得预测值?

问题描述 投票:0回答:1

如何将训练好的模型(本地的 ipynb 或 python 文件)链接到 javascript(前端?

  • 我有一个经过训练的 XGB 模型,使用一些特征(浮点)来预测一个 值(碳强度)。
  • 我想将模型保存为文件并加载模型以在中使用 JavaScript。它将要求用户在 JavaScript 中输入一条记录(22 个特征)并传递给 ML 模型,该模型最终返回并打印预测值(碳强度)。
  • 我知道模型可以保存为 json/pickle/joblib 文件。

以下是Python中的模型训练代码:

# 1. Load the Data
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Load the dataset from an Excel file
df = pd.read_excel(r"C:\Users\RY\Desktop\Jupiter\test.xlsx", sheet_name="Sheet1")

# Keep only the relevant columns (excluding the first two columns)
df = df.iloc[:, 2:]

# Categorize features based on their module (boiler, turbine, power, coal, carbon emissions)
# Features: 1(date) + 7(boiler) + 6(turbine) + 3(power) + 5(coal quality) + 2(carbon emissions) + 1(runtime) + 1(predicted carbon intensity)

# Define feature names for each module (adjust based on your actual column names)
boiler_features = ['Boiler Feedwater Temperature', 'Air Supply Temperature', 'Oxygen Level',
                   'Air Preheater Leakage Rate', 'Calculated Flue Gas Temperature',
                   'Oxygen Content (%)', 'Flue Gas Flow Rate']

turbine_features = ['Main Steam Temperature', 'Main Steam Pressure', 'Reheat Steam Temperature',
                    'Exhaust Steam Temperature', 'Vacuum', 'Average Load']

power_features = ['Load', 'Plant Power Consumption Rate', 'Operating Hours']

coal_features = ['Carbon Content (Air Dry, %)', 'Volatile Matter (Received, %)', 
                 'Ash Content (Received, %)', 'Net Calorific Value (kJ/kg, Received)', 
                 'Moisture Content (%)']

carbon_emission_features = ['CO2 Concentration', 'Carbon Emission', 'Carbon Intensity']

# Combine features in the specified order
sorted_columns = boiler_features + turbine_features + power_features + coal_features + carbon_emission_features
df = df[sorted_columns]

# Split data into features (X1) and target (y1)
X1 = df.drop('Carbon Intensity', axis=1).drop('Carbon Emission', axis=1)
y1 = df['Carbon Intensity']

# 2. Split Data into Training and Testing Sets
from sklearn.model_selection import train_test_split
X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.2, random_state=42)

# Display basic statistics of the feature dataset
X1.describe()

# 3. Train the Model
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, mean_absolute_percentage_error

# Initialize the XGBoost regressor
xgb_model = XGBRegressor(
    n_estimators=300,        # Number of trees
    max_depth=3,             # Maximum tree depth
    learning_rate=0.1,       # Learning rate
    subsample=0.8,           # Fraction of samples per tree
    colsample_bytree=0.8,    # Fraction of features per tree
    random_state=42
)

# Train the model on the training data
xgb_model.fit(X1_train, y1_train)

# Make predictions on the test set
xgb_y1_pred = xgb_model.predict(X1_test)


# 4. Make Predictions on one record 
# Example: Predict carbon intensity for a single data point
xgb_model.predict([X1_train.iloc[0].values])

# 5. Save the Model
# Save the trained model as a JSON file
xgb_model.save_model("xgb_model.json")

如何在 JavaScript 中保存并加载模型?

单条记录输入输出示例:

[array([ 2.42860000e+02,  1.97800000e+01,  4.59000000e+00,  2.43000000e+00,
         1.27770000e+02,  9.60153257e-02,  6.51605003e+05,  5.42320000e+02,
         1.26000000e+01,  5.12910000e+02,  3.18000000e+01, -9.78800000e+01,
         1.97080000e+02,  1.93096475e+02,  5.45000000e+00,  2.40000000e+01,
         5.36500000e+01,  2.41800000e+01,  2.18900000e+01,  1.86225000e+04,
         1.53000000e+01,  3.33225000e+03])]

array([865.6147], dtype=float32)
javascript python machine-learning scikit-learn frontend
1个回答
-1
投票

有一个 NPM 包,XGBoost-Node

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.