Skip to main content

how to open .model model objects in python?

Introduction:

A few days ago, I got a model called xyz.model. We got it from a client and I had no idea how to open it. And after searching a lot, I couldn't find out an useful answer though. That's why this article.

What is the object?

a .model model object is created on saving xgboost model from R, using xgboost::save() method. That is how it is created. Now as you may guess, using a R model in python we will have a problem. And you are right. In general the standard xgboost library will not be able to load according to this github thread.

What is the issue?

Actually it is impossible to load a model from a buffer (e.g. bytestring) in Python, due to a bug where ctypes is used incorrectly to get a const char* pointer:

>>> bst.load_model(b'123')
TypeError: underlying buffer is not writable

Here's a short example of what it is basically trying to do:

>>> b = b'123'
>>> (ctypes.c_char * 3).from_buffer(buf)
TypeError: underlying buffer is not writable

Now, to actually load a xgboost model properly in this scenario, we can use the work around model mentioned here. 

import ctypes
import xgboost
import xgboost.core

def xgb_load_model(buf):
    if isinstance(buf, str):
        buf = buf.encode()
    bst = xgboost.core.Booster()
    n = len(buf)
    length = xgboost.core.c_bst_ulong(n)
    ptr = (ctypes.c_char * n).from_buffer_copy(buf)
    xgboost.core._check_call(
        xgboost.core._LIB.XGBoosterLoadModelFromBuffer(bst.handle, ptr, length)
    )  # segfault
    return bst

So using this function, we can load the model. So as a whole, you can do the following:

with open('xgb_model.model','rb') as f:
    raw = f.read() 
model = xgb_load_model(raw) 

Now your model problem is solved. Lay back and predict using your xgboost model.

Comments

Popular posts from this blog

Mastering SQL for Data Science: Top SQL Interview Questions by Experience Level

Introduction: SQL (Structured Query Language) is a cornerstone of data manipulation and querying in data science. SQL technical rounds are designed to assess a candidate’s ability to work with databases, retrieve, and manipulate data efficiently. This guide provides a comprehensive list of SQL interview questions segmented by experience level—beginner, intermediate, and experienced. For each level, you'll find key questions designed to evaluate the candidate’s proficiency in SQL and their ability to solve data-related problems. The difficulty increases as the experience level rises, and the final section will guide you on how to prepare effectively for these rounds. Beginner (0-2 Years of Experience) At this stage, candidates are expected to know the basics of SQL, common commands, and elementary data manipulation. What is SQL? Explain its importance in data science. Hint: Think about querying, relational databases, and data manipulation. What is the difference between WHERE

What is Bort?

 Introduction: Bort, is the new and more optimized version of BERT; which came out this october from amazon science. I came to know about it today while parsing amazon science's news on facebook about bort. So Bort is the newest addition to the long list of great LM models with extra-ordinary achievements.  Why is Bort important? Bort, is a model of 5.5% effective and 16% total size of the original BERT model; and is 20x faster than BERT, while being able to surpass the BERT model in 20 out of 23 tasks; to quote the abstract of the paper,  ' it obtains performance improvements of between 0 . 3% and 31%, absolute, with respect to BERT-large, on multiple public natural language understanding (NLU) benchmarks. ' So what made this achievement possible? The main idea behind creation of Bort is to go beyond the shallow depth of weight pruning, connection deletion or merely factoring the NN into different matrix factorizations and thus distilling it. While methods like knowle

Spacy errors and their solutions

 Introduction: There are a bunch of errors in spacy, which never makes sense until you get to the depth of it. In this post, we will analyze the attribute error E046 and why it occurs. (1) AttributeError: [E046] Can't retrieve unregistered extension attribute 'tag_name'. Did you forget to call the set_extension method? Let's first understand what the error means on superficial level. There is a tag_name extension in your code. i.e. from a doc object, probably you are calling doc._.tag_name. But spacy suggests to you that probably you forgot to call the set_extension method. So what to do from here? The problem in hand is that your extension is not created where it should have been created. Now in general this means that your pipeline is incorrect at some level.  So how should you solve it? Look into the pipeline of your spacy language object. Chances are that the pipeline component which creates the extension is not included in the pipeline. To check the pipe eleme