简单介绍使用前端streamlit框架快速部署本地模型:
1、模型训练:
import pandas as pd
# 流程整合
from sklearn.pipeline import make_pipeline, Pipeline
# 数据处理
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler, StandardScaler, MaxAbsScaler, RobustScaler
# 随机森林
from sklearn.ensemble import RandomForestClassifier
# 模型保存
import joblib
# Pipeline步骤显示
from sklearn import set_config
set_config(display='diagram')# 加载数据集
data = pd.read_csv('train.csv')
data = data[["x1","x2","x3","x4","y"]]# 管道建模
pip = make_pipeline(SimpleImputer(strategy='constant', fill_value=-1),RobustScaler(), # 针对异常值的归一化RandomForestClassifier())
# 模型拟合
pip.fit(data.drop("y", axis=1), data["y"])# 保存模型
joblib.dump(pip, './rfc_model.pkl')
2、streamlit模型部署,仅需几行代码即可,保存为test.py文件:
import streamlit as st
import joblibst.header("本地模型部署")
st.subheader("模型预测")
# 模型加载
model = joblib.load('./rfc_model.pkl')
# 创建一个文本输入框
text = st.text_input("请输入数据:")
# 切分转数字列表
pre_data = [float(i) for i in text.split()]if st.button('提交'):# 调用模型进行预测pre_prob = model.predict_proba([pre_data])[0][1]pre_label = 1 if pre_prob > 0.5 else 0# 显示用户输入的内容 st.write(f'预测概率为:{pre_prob},预测标签为:{pre_label}')
3、启动服务:
终端执行命令:
streamlit run .\test.py
4、浏览器打开服务地址:
http://localhost:8501