华为云AI开发平台ModelArtsScikit Learn_云淘科技
训练并保存模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import json import pandas as pd from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.externals import joblib iris = pd.read_csv('/home/ma-user/work/iris.csv') X = iris.drop(['variety'],axis=1) y = iris[['variety']] # Create a LogisticRegression instance and train model logisticRegression = LogisticRegression(C=1000.0, random_state=0) logisticRegression.fit(X,y) # Save model to local path joblib.dump(logisticRegression, '/tmp/sklearn.m') |
训练前请先下载iris.csv数据集,解压后上传至Notebook本地路径/home/ma-user/work/。iris.csv数据集下载地址:https://gist.github.com/netj/8836201。Notebook上传文件操作请参见上传本地文件至Notebook中。
保存完模型后,需要上传到OBS目录才能发布。发布时需要带上“config.json”配置以及“customize_service.py”,定义方式参考模型包规范介绍。
推理代码
在模型代码推理文件customize_service.py中,需要添加一个子类,该子类继承对应模型类型的父类,各模型类型的父类名称和导入语句如请参考表1。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# coding:utf-8 import collections import json from sklearn.externals import joblib from model_service.python_model_service import XgSklServingBaseService class UserService(XgSklServingBaseService): # request data preprocess def _preprocess(self, data): list_data = [] json_data = json.loads(data, object_pairs_hook=collections.OrderedDict) for element in json_data["data"]["req_data"]: array = [] for each in element: array.append(element[each]) list_data.append(array) return list_data # predict def _inference(self, data): sk_model = joblib.load(self.model_path) pre_result = sk_model.predict(data) pre_result = pre_result.tolist() return pre_result # predict result process def _postprocess(self,data): resp_data = [] for element in data: resp_data.append({"predictresult": element}) return resp_data |
父主题: 自定义脚本代码示例
同意关联代理商云淘科技,购买华为云产品更优惠(QQ 78315851)
内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家