自动将ml模型用作rest api
scikit_rest的Python项目详细描述
SCIKIT支架
自动将ml模型部署为rest api
通常,将您最喜欢的scikit learn/xgboost/pytorch/tensorflow模型部署为rest api可能需要很多时间。有很多样板代码需要编写。scikit-rest
是一个旨在减轻此过程中大多数痛点的包。
先决条件
这个包正式支持python 3
安装
pip install scikit_rest
用法
这个包中提供的主要功能是serve
,语法如下:
serve(
col_list: List[str],
col_types: Dict[str, Union[List, type]],
transform_fn: Callable,
predict_fn: Union[Callable, sklearn.base.BaseEstimator],
port: int,
is_nullable: bool ,
name: str,
)
列列表
列名列表,其中值的顺序将指示pandas数据框中的顺序
col_list = ['class', 'sex', 'age', 'embarked', 'date', 'is_englishman']
列类型
列名和变量类型的字典,用于输入验证。如果 而字典的是一个列表,我们假设变量的任何输入只能是 列表中列出的那些
col_types = {
'class' : int,
'sex' : str,
'age' : float,
'embarked': ['C', 'S', 'Q'],
'date': datetime.datetime,
'is_englishman': bool
}
转换fn
将输入数据帧转换为测试数据帧的函数,我们可以调用model.predict on以获得最终结果
def transform_fn(input_df):
df = input_df.copy()
df['sex'] = df['sex'].apply(lambda x : transform_sex(x))
df['embarked'] = df['embarked'].apply(lambda x : transform_embarked(x))
df['date'] = df['date'].dt.year
df = df.fillna(0.)
return df
预测fn
将测试数据帧转换为结果的函数。如果传入了ml model实例,我们将尝试调用model.predict_proba/model.predict以获得结果
def predict_fn(input_df):
df = input_df.copy()
return model.predict(df).item()
端口
应为其提供rest api的端口号
是否可以为空
输入api是否可以为空
姓名
程序名称
示例
用法示例可在example文件夹中找到
贡献
请阅读CONTRIBUTING.md了解我们行为准则的详细信息, 以及向我们提交请求的过程。
作者
[Aditya Kelvianto Sidharta][https://aditya sidharta.com]
许可证
这个项目是在麻省理工学院的许可下授权的-详细信息请参见LICENSE文件