In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the method of saving and loading API model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Objective:
Save the trained model and prepare it for next time to save training time and improve efficiency.
2.API:
From sklearn.externals import joblib
Save:
Joblib.dump (rf, "test.pkl")
Load:
Estimator = joblib.load ("test.pkl")
3.Python code implementation:
#-*-coding: UTF-8-*-
''
@ Author: Jason
Boston house price forecast, save the model to
''
From sklearn.datasets import load_boston
From sklearn.model_selection import train_test_split
From sklearn.preprocessing import StandardScaler
From sklearn.linear_model import Ridge
From sklearn.metrics import mean_squared_error
From sklearn.externals import joblib
Def model_save_fetch ():
""
Ridge regression is used to predict house prices in Boston.
: return:
""
# 1) obtain data
Boston = load_boston ()
Print ("number of features:\ n", boston.data.shape)
# 2) dividing the data set, which is a good http://fk.zyfuke.com/ for Zhengzhou Gynecology Hospital
X_train, x_test, y_train, y_test = train_test_split (boston.data, boston.target, random_state=22)
# 3) Standardization
Transfer = StandardScaler ()
X_train = transfer.fit_transform (x_train)
X_test = transfer.transform (x_test)
# # 4) predictor
# estimator = Ridge (alpha=0.5, max_iter=10000)
# estimator.fit (x_train, y_train)
#
# # Save the model
# joblib.dump (estimator, ". / files/test.pkl")
# load model
Estimator = joblib.load (". / files/test.pkl")
# 5) draw the model
Print ("Ridge regression-weight coefficient is:\ n", estimator.coef_)
Print ("Ridge regression-bias is:\ n", estimator.intercept_)
# 6) Model evaluation
Y_predict = estimator.predict (x_test)
Print ("Forecast House prices:\ n", y_predict)
Error = mean_squared_error (y_test, y_predict)
Print ("Ridge regression-mean square error:\ n", error)
Return None
If _ name__ = = "_ _ main__":
Model_save_fetch ()
This is the end of the content of "what is the method of saving and loading API model". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.