[PYTHON] Save the pystan model and results in a pickle file

What to do in this article

--Save the model and result created using pystan as a pickle file --Introducing how to read the model and result without recalculating

Conclusion

python


#writing
with open('model.pkl', 'wb') as f:
     pickle.dump(stm, f)
with open('fit.pkl', 'wb') as g:
     pickle.dump(fit, g)
  
#Read
stm = pickle.load(open(DATA_DIR + 'model_ForS.pkl', 'rb'))
fit = pickle.load(open(DATA_DIR + 'fit_ForS.pkl', 'rb'))

The problem you want to solve

Bayesian statistical modeling by pystan is convenient, but it takes a lot of calculation method. It depends on the item, but most of them take about an hour. Moreover, it is often the case that the calculation did not converge.

So many people want to save the calculated result. It may be written in the tutorial book, but I think that there are many people who say ** because there are many Rstan books, without knowing how to save python **.

So in this article -** Save the model and results of Bayesian statistical modeling by pystan in a pickle file ** I will show you how.

Solution

Suppose you compile and sample with pystan as follows. Here, the model is stored in stm and the sampling result is stored in fit.

python


model = """
data{
...
}

parameters{
...
}

model{
...
}
"""
stm = pystan.StanModel(model_code=model) #compile
stan_data = {...}
fit = stm.sampling(data=stan_data) #sampling

At this time, you can save and write by writing as follows. It was easy. Even if only the result is saved, it cannot be read. You have to save the model with it. I don't know the details, but it seems that the sampling result is linked to the model.

python


#writing
with open('model.pkl', 'wb') as f:
     pickle.dump(stm, f)
with open('fit.pkl', 'wb') as g:
     pickle.dump(fit, g)
  
#Read
stm = pickle.load(open(DATA_DIR + 'model_ForS.pkl', 'rb'))
fit = pickle.load(open(DATA_DIR + 'fit_ForS.pkl', 'rb'))

Recommended Posts

Save the pystan model and results in a pickle file
Save the object to a file with pickle
Save the binary file in Python
Save a YAML-formatted file in PyYAML
Process Splunk execution results using Python and save to a file
Save the Pydrive authentication file in a different directory from the script
Replace the directory name and the file name in the directory together with a Linux command.
12. Save the first column in col1.txt and the second column in col2.txt
Create a Python image in Django without a dummy image file and test the image upload
Save the setting conditions as a CSV file using UDF Manager in OCTA
Create a REST API using the model learned in Lobe and TensorFlow Serving.
Attempt to launch another .exe and save the console output to a text file
How to save the feature point information of an image in a file and use it for matching
Get the file name in a folder using glob
Read the csv file and display it in the browser
Select the required variables in TensorFlow and save / restore
Output a binary dump in binary and revert to a binary file
Extract the lyrics information in the MP3 / MP4 file and save it in the lyrics file (* .lrc) for Sony walkman.
[Python] You can save an object to a file by using the pickle module.
[Mac] A super-easy way to execute system commands in Python and output the results
[Sublime Text 2] Always execute a specific file in the project
Download the file in Python
Temporarily save a Python object and reuse it in another Python
Change the standard output destination to a file in Python
Get the MIME type in Python and determine the file format
Read and write a file
[Note] Import of a file in the parent directory in Python
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Write and read a file
[TF] How to load / save Model and Parameter in Keras
Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
Learn the flow of Bayesian estimation and how to use Pystan through a simple regression model
[PyTorch] Make sure the model and dataset are in cuda mode
Search the file name including the specified word and extension in the directory
Anyway, the fastest serial communication log is left in a file
Specify or create a python folder and then save the screenshot.
Read a file in Python with a relative path from the program
[Python3] Save the mean and covariance matrix in json with pandas
Read the config file in Go language! Introducing a simple sample
Dig the directory and create a list of directory paths + file names
Get the formula in an excel file as a string in Python
Get OCTA simulation conditions from a file and save with pandas
Format the Git log and get the committed file name in csv format
Count specific strings in a file
Run Pylint and read the results
Save a specific variable in tensorflow.session
Create a binary file in Python
The story of the "hole" in the file
Check if there is a specific symbol in the executable file and its dependent libraries (simplified version)
[Python] How to save the installed package and install it in a new environment at once Mac environment
A memo organized by renaming the file names in the folder with python
How to specify a .ui file in the dialog / widget GUI in PySide
Enclose the cat result in double quotes and put it in a variable
The one that divides the csv file, reads it, and processes it in parallel
Test & Debug Tips: Create a file of the specified size in Python
View the full path (absolute path) of a file in a directory in Linux Bash
The file name was bad in Python and I was addicted to import
Deploy and use the prediction model created in Python on SQL Server
Receive a list of the results of parallel processing in Python with starmap
Open an Excel file in Python and color the map of Japan
Start the webcam to take a still image and save it locally