Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of saving and loading API model

Shulou Source: shulou.com Published: 2022-06-02 06:32:46 10月04日 Update

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!

Tags: Model house price method content data more knowledge Bols Boston training practicality achievement next code hospital dilemma gynecology reality situation efficiency Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Linux Shulou Information MySQL