How to evaluate the PMML file using python - python

I have pmml file generated by python having random forest classifier, I need to test the model again in python. Kindly let me know how to import the pmml file back to python so that I can test the model using new dataset.
I have tried using titanium package but it went to error because of the version issue of PMML.
The expected output to be the predicted value of the model so that I can verify the accuracy of the model.

You could use PyPMML to load PMML in Python, then make predictions on new dataset, e.g.
from pypmml import Model
model = Model.fromFile('the/pmml/file/path')
result = model.predict(data)
The data could be dict, string in JSON, Series or DataFrame of Pandas.

Related

Pycharm: Is there a way to run a snippet of code without running the entire file?

I am teaching myself to code Convolutional Neural Networks. In particular I am looking at the "Dogs vs. Cats" challenge (https://medium.com/#mrgarg.rajat/kaggle-dogs-vs-cats-challenge-complete-step-by-step-guide-part-2-e9ee4967b9). I am using PyCharm.
In PyCharm, is there a way of using the trained model to make a prediction on the test data without having to run the entire file each time (and thus retrain the model each time)? Additionally, is there a way to skip the part of the script that prepares the data for input into the CNN? In a similar manner, does PyCharm store variables- can I print individual variables after the script has been run.
Would it be better if I used a different IDLE?
You can use sklearn joblib to save the trained model as a pickle and use it later for predictions.
from sklearn.externals import joblib
# Save the model as a pickle in a file
joblib.dump(knn, 'filename.pkl')
# Load the model from the file
knn_from_joblib = joblib.load('filename.pkl')
# Use the loaded model to make predictions
knn_from_joblib.predict(X_test)

Load and use XGBoost PMML or XGBoost .rds model in python sklearn with out loosing its dependencies/nature

I have a XGB model which is built in R, I wanted to use the same model in python for predictions without loosing its nature/embedded dependencies.
I've tried rpy2 and sklearn-pmml-model, seems there is no help for XGBoost models.
Appreciate any help.
You could use PyPMML to evaluate XGBoost PMML in Python, for example:
from pypmml import Model
model = Model.fromFile('the/pmml/model/file/path')
result = predict(data)
The data could be in dict, Series or DataFrame of Pandas, or JSON string.

Text Categorization Test NLTK python

I have using nltk packages and train a model using Naive Bayes. I have save the model to a file using pickle package. Now i wonder how can i use this model to test like a random text not in the dataset and the model will tell if the sentence belong to which categorize?
Like my idea is i have a sentence : " Ronaldo have scored 2 goals against Egypt" And pass it to the model file and return categorize "sport".
Just saving the model will not help. You should also save your VectorModel (like tfidfvectorizer or countvectorizer what ever you have used for fitting the train data). You can save those the same way using pickle. Also save all those models you used for pre-processing the train data like normalization/scaling models, etc. For the test data repeat the same steps by loading the pickle models that you saved and transform the test data in train data format that you used for model building and then you will be able to classify.

Save classifier for future prediction

I am new to machine learning with python and I am trying to build a Sentiment Analyzer in which I am using this dataset and this tutorial. Everything is working fine on the test data. But I'm trying to save my classifier for future use. I'm doing this using pickle by saving it as
sentiment_analyzer = open("Sentiment_Analyzer.pkl", "wb")
pkl.dump(classifier_linear, sentiment_analyzer)
sentiment_analyzer.close()
Later, I'm extracting my saved analyzer by doing this
model_pkl = open("Sentiment_Analyzer.pkl", "rb")
model = pkl.load(model_pkl)
But I'm unable to understand how to call the predict method on my extracted model classifier.
You need to save the vectorizer too, just the same way you are pickling the model. Then during future use, load both the vectorizer and classifier, transform the new X using the loaded vectorizer and then call predict() on classifier.

Apply PMML predictor model in python

Knime has generated for me a PMML model. At this time I want to apply this model to a python process. What is the right way to do this?
More in depth: I develop a django student attendance system. The application is already so mature that I have time to implement the 'I'm feeling lucky' button to automatically fill an attendance form. Here is where PMML comes in. Knime has generated a PMML model that predicts student attendance. Also, thanks to django for being so productive that I time for this great work ;)
Finally I have wrote my own code. Be free to contribute or fork it:
https://github.com/ctrl-alt-d/lightpmmlpredictor
The code for Augustus, to score PMML models in Python, is at https://code.google.com/p/augustus/
You could use PyPMML to apply PMML in Python, for example:
from pypmml import Model
model = Model.fromFile('the/pmml/file/path')
result = model.predict(data)
The data could be dict, json, Series or DataFrame of Pandas.
If you use PMML in PySpark, you could use PyPMML-Spark, for example:
from pypmml_spark import ScoreModel
model = ScoreModel.fromFile('the/pmml/file/path')
score_df = model.transform(df)
The df is a DataFrame of PySpark.
For more info about other PMML libraries, be free to see:
https://github.com/autodeployai

Categories