{"id":1215,"date":"2015-05-12T09:33:52","date_gmt":"2015-05-12T16:33:52","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?p=1215"},"modified":"2015-05-21T09:46:35","modified_gmt":"2015-05-21T16:46:35","slug":"saving-results-of-python-classifiers-to-disk","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/2015\/05\/12\/saving-results-of-python-classifiers-to-disk\/","title":{"rendered":"Saving Results of Python Classifiers to Disk"},"content":{"rendered":"<div dir=\"ltr\">\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignright\" src=\"http:\/\/s3.postimg.org\/ojntf29xv\/logo.png\" alt=\"\" width=\"97\" height=\"119\" \/>If you are building Naive Bayes classifiers using packages such as NLTK, you may notice that if you have a large training set that it can take hours to run. In order to not lose these results between work sessions, you can save the results of your classifier training to a disk file using the <strong>pickle<\/strong> commands list further below.<\/p>\n<\/div>\n<p><!--more--><\/p>\n<p>This may be useful if you &#8216;train&#8217; your classifier on one day and then want to use it to &#8216;predict&#8217; classification results on a new test data set on another day.\u00a0 You can just re-load your classifier from disk into memory without having to re-build it.\u00a0 Note: these pickle file sizes may get extremely large.<\/p>\n<p>The examples below store a Python classifier object called &#8216;classifier&#8217; into a pickle file called &#8216;my_classifier.pickle&#8217;.<\/p>\n<div dir=\"ltr\">\n<p><span style=\"text-decoration: underline;\"><strong>To Save<\/strong><\/span><\/p>\n<pre><code>import pickle\r\nf = open('my_classifier.pickle', 'wb')\r\npickle.dump(classifier, f)\r\nf.close()<\/code><\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>To Load Later<\/strong><\/span><\/p>\n<pre><code>import pickle\r\nf = open('my_classifier.pickle')\r\nclassifier = pickle.load(f)\r\nf.close()<\/code><\/pre>\n<\/div>\n<p>Brought to you by Tank Brigade.<br \/>\n<img decoding=\"async\" src=\"http:\/\/s3.postimg.org\/ojntf29xv\/logo.png\" alt=\"\" width=\"40%\" height=\"40%\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are building Naive Bayes classifiers using packages such as NLTK, you may notice that if you have a large training set that it can take hours to run. In order to not lose these results between work sessions, you can save the results of your classifier training to a disk file using the [&hellip;]<\/p>\n","protected":false},"author":27014,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[585,1054147],"class_list":["post-1215","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python","tag-text-analytics"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/27014"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=1215"}],"version-history":[{"count":3,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1215\/revisions"}],"predecessor-version":[{"id":1219,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/posts\/1215\/revisions\/1219"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=1215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/categories?post=1215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/tags?post=1215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}