Message Boards Message Boards

Import trained classifier to Python

Posted 24 days ago

Hello Wolfram Community

I have trained a text classifier that understands a product description and puts the product in a category for example.

"Low Fat milk, 1000ml" -> Dairy
" Strawberry high protein yogurt"-> Dairy
" 1kg of red apples" -> Fruits

and so.
My classifier was built with the Classify function and I saved the model in a . wmlf Whenever I need to classify a bunch of products I just import as

MarkovClassifier = 
 Import["C:\\Users\\Amor_Rodrigo\\Desktop\\Latest\\Markov Classifier \
834.wmlf"]

and then just classify my list

MarkovClassifier[mylist]

That's it. As easy as that. My problem is that now that I need to implement this function in Python

I am trying something like this

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wlexpr

session=WolframLanguageSession()

classifierpath = r"C:\\Users\\Amor_Rodrigo\\Desktop\\Latest\\Markov Classifier 834.wmlf"
session.evaluate(wlexpr('MarkovClassifier=Import[classifierpath]'))

test= session.evaluate(wlexpr('Map[#^2 &, Range[5]]'))
print(test)
session. Terminate()

My output is this

First argument classifierpath is not a valid file, directory, or URL specification.
First argument classifierpath is not a valid file, directory, or URL specification.
(1, 4, 9, 16, 25)

Does anyone know how to import that classifier function and put it to work. ?

Any help will be really appreciated

POSTED BY: Rodrigo Amor
Posted 18 days ago

Finally Got it Right in case someone will have a similar question. I saved my model as a wxf and later in python I did this

from wolframclient.evaluation import WolframLanguageSession

from wolframclient.deserializers import binary_deserialize

markovpath='/Users/rodrigoamor/Desktop/Markov Model.wxf'

session= WolframLanguageSession()
with open(markovpath, 'rb') as fp:
    markov = binary_deserialize(fp)

print(session.evaluate(markov(({"BdAAAAB", "BBBB", "CCC","AAC"}))))

session.terminate()
POSTED BY: Rodrigo Amor
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract