model_path = './model22.pkl'
with open(model_path, 'rb') as modelf:
model = pickle.load(modelf)
模型训练代码
main.py
# -*- coding: utf-8 -*-
from __future__ import print_function # do not delete this line if you want to save your log file.
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA, QuadraticDiscriminantAnalysis as QDA
from naie.datasets import get_data_reference
from naie.metrics import report
import pickle
from naie.context import Context
import os
import joblib
'''
代码框架见QDA.py,大致是 加载数据,模型训练,测试集预测,日志上传四个部分。
补上了日志上传的代码,即score_model()
补上了模型训练部分中模型保存的代码,即save_model()
'''
def get_list_letter(char, features, path):
#数据集处理部分
with open(path, 'r') as letter_train:
content_letter_train = letter_train.readlines()
for line in content_letter_train:
temp = line.split(',')
temp[-1] = list(temp[-1])[0]
char.append(temp[0])
features.append((temp[1::]))
def convert_int(str_list):
#数据集处理部分
for row in range(0, len(str_list)):
for col in range(0, len(str_list[row])):
str_list[row][col] = int(str_list[row][col])
def train_and_test(train_result, fetures_list, test_list):
#调用sklearn进行训练及预测
letter_x = np.array(fetures_list)
letter_y = np.array(train_result)
clf = QDA()
clf.fit(letter_x, letter_y)
#!!增加了保存模型,以便验证时读取。
save_model(clf)
return list(clf.predict(test_list))
def analysis_accuracy(judge_result, test_char):
#分析预测精确度
sum = 0
right_num = 0
for pos in range(0, len(judge_result)):
sum += 1
if judge_result[pos] == test_char[pos]:
right_num += 1
return right_num / sum
def score_model(accuracy):
# !!日志生成函数
#NAIE官方模板,用于给出报告,下面的注释就是示例。
with report(True) as log_report:
log_report.log_property('Accuracy', accuracy)
"""
====================== YOUR CODE HERE ======================
there are several api you can use here.
Example:
from naie.metrics import report
with report(True) as log_report:
log_report.log_property("score", accuracy_score(y_validation, model.predict(x_validation)))
============================================================
"""
return accuracy
def save_model(model):
# !!保存模型函数,用pickle/joblib保存模型,方便模型验证时使用。
"""
====================== YOUR CODE HERE ======================
write model to the specific model path of train job
e.g.
from naie.context import Context
with open(os.path.join(Context.get_output_path(), 'model.pkl'), 'w') as ff:
pickle.dump(clf, ff)
or
tf.estimator.Estimator(model_dir=Context.get_output_path()) # using tensorflow Estimator
============================================================
"""
with open(os.path.join(Context.get_output_path(), 'model22.pkl'), 'wb') as ff:
pickle.dump(model, ff)
def main():
# letter数据集初始化
# !!这里的路径获取改了
letter_train_path = get_data_reference(dataset="Default", dataset_entity="letter_train").get_files_paths()[0]
letter_train_class = []
letter_train_features = []
letter_test_path = get_data_reference(dataset="Default", dataset_entity="letter_test").get_files_paths()[0]
letter_test_class = []
letter_test_features = []
get_list_letter(letter_train_class, letter_train_features, letter_train_path)
get_list_letter(letter_test_class, letter_test_features, letter_test_path)
convert_int(letter_train_features)
convert_int(letter_test_features)
# Letter数据集学习
letter_QDA_judge_result = train_and_test(letter_train_class, letter_train_features, letter_test_features)
letter_QDA_judge_accuracy = analysis_accuracy(letter_QDA_judge_result, letter_test_class)
print('使用QDA对letter的', len(letter_train_features), '份数据学习后,对',
len(letter_test_features), '份测试数据分类的准确率为:', letter_QDA_judge_accuracy)
# !!增加了日志生成
score = score_model(letter_QDA_judge_accuracy)
return score
if __name__ == "__main__":
main()
共有 0 条评论