# What is the proper way to save a cmdstanpy model after a fit has been complete?

**URL:** <https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020>\
**Category:** General\
**Created:** [October 28, 2021, 10:41am UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020 "2021-10-28T10:41:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [October 28, 2021, 10:41am UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020/1 "2021-10-28T10:41:31Z")

</div>

How do I properly save a cmdstanpy model? I have successfully compiled and sampled from the model and have verified the csv’s are saved to the output directory?

After being saved, how do I load the model to sample with new data?

---

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [October 29, 2021, 1:59pm UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020/2 "2021-10-29T13:59:37Z")

</div>

Or are the csv’s the model itself somehow?

---

<div class="post-metadata">

**Author:** ![yizhang](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yizhang/32/15673_2.png) [@yizhang](https://discourse.mc-stan.org/u/yizhang)\
**Post date:** [November 10, 2021, 5:08pm UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020/3 "2021-11-10T17:08:43Z")

</div>

@ahartikainen @mitzimorris

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [November 10, 2021, 7:47pm UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020/4 "2021-11-10T19:47:24Z")

</div>

The compiled model will be saved as a file on disk. Re-running the `CmdStanModel(...)` block of code should re-create the model without recompiling, unless you’ve edited the model code itself (the .stan file)

The CSV files from a fit of the model can be saved with [`save_csvfiles`](https://cmdstanpy.readthedocs.io/en/v1.0.0rc1/api.html#cmdstanpy.CmdStanMCMC.save_csvfiles) and turned back into a CmdStanMCMC(or MLE/VI/GQ) using [`from_csv`](https://cmdstanpy.readthedocs.io/en/v1.0.0rc1/api.html#cmdstanpy.from_csv)

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [November 10, 2021, 9:48pm UTC](https://discourse.mc-stan.org/t/what-is-the-proper-way-to-save-a-cmdstanpy-model-after-a-fit-has-been-complete/25020/5 "2021-11-10T21:48:30Z")

</div>

> [@Jordan\_Howell](#):
>
> After being saved, how do I load the model to sample with new data?

A CmdStanModel object just holds onto the location of the Stan source file and/or compiled executable file on disk. If both exist in the same directory, you then instantiate a new CmdStanModel object (or use the existing one, if you’re in the same session). Then you call the `sample` method with the new data.

> Or are the csv’s the model itself somehow

no, the CSV files are just [Stan CSV files](https://mc-stan.org/docs/2_28/cmdstan-guide/stan_csv.html).
