Orange Forum • View topic - Getting predictions from classifier with python file

Getting predictions from classifier with python file

A place to ask questions about methods in Orange and how they are used and other general support.

Getting predictions from classifier with python file

Postby mjrother » Mon Jun 10, 2013 2:47

I have trained several learners and want to use the learner to get the prediction another set of data.

Here is an example using neural network

learner = orngClass.neural.NeuralNetworkLearner()
learner.name = "Neural Network"
learner.n_mid = 10
learner.reg_fact = 0.1
learner.normalize = False

data = Orange.data.Table("train.csv")
test_data = Orange.data.Table("test.csv")

print "Testing Neural Network"
for t in test_data:
print learner(t)


I get this error:
Traceback (most recent call last):
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\NeuralNetworkLearner.py", line 65, in <module>
predict_NeuralNetwork(data, test_data)
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\NeuralNetworkLearner.py", line 56, in predict_NeuralNetwork
print learner(t)
File "y:\python\Python27\lib\site-packages\Orange\classification\neural.py", line 195, in __call__
X = data.to_numpy()[0]
AttributeError: 'Orange.data.Instance' object has no attribute 'to_numpy'


With other learners/classifiers I get different errors. For example with the Bayes classifier I get:
Traceback (most recent call last):
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\BayesLearner.py", line 35, in <module>
predict_Bayes(data, test_data)
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\BayesLearner.py", line 27, in predict_Bayes
print learner(t)
File "y:\python\Python27\lib\site-packages\Orange\utils\__init__.py", line 201, in wrap_call
return func(*args, **kwargs)
File "y:\python\Python27\lib\site-packages\Orange\classification\bayes.py", line 93, in __call__
return NaiveClassifier(bayes(data, weight))
AttributeError: Learner.__call__: examples and, optionally, weight attribute expected


Am I using the classifers incorrectly?

Re: Getting predictions from classifier with python file

Postby Ales » Mon Jun 10, 2013 11:37

mjrother wrote:Am I using the classifers incorrectly?
No. You are not even training the the classifier.
Try this:
Code: Select all
classifier = learner(data)
for t in test_data:
    print classifier(t)

Re: Getting predictions from classifier with python file

Postby mjrother » Tue Jun 11, 2013 0:04

My bad, I omitted the cross training step in the learning. Repeating some code for context.


learner = orngClass.bayes.NaiveLearner()
learners = [learner]

results = orngEval.testing.cross_validation(learners, data, folds=10)

for t in test_data:
print learner(t)

Traceback (most recent call last):
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\BayesLearner.py", line 35, in <module>
predict_Bayes(data, test_data)
File "Z:\Courses\Kaggle\BiologicalResponse\src\bioresponse\BayesLearner.py", line 27, in predict_Bayes
print learner(t)
File "y:\python\Python27\lib\site-packages\Orange\utils\__init__.py", line 201, in wrap_call
return func(*args, **kwargs)
File "y:\python\Python27\lib\site-packages\Orange\classification\bayes.py", line 93, in __call__
return NaiveClassifier(bayes(data, weight))
AttributeError: Learner.__call__: examples and, optionally, weight attribute expected

Re: Getting predictions from classifier with python file

Postby Ales » Tue Jun 11, 2013 9:49

mjrother wrote:My bad, I omitted the cross training step in the learning. Repeating some code for context.

The same comment still applies. See the tutorial on learners and classifiers.

Re: Getting predictions from classifier with python file

Postby LetterRip » Thu May 08, 2014 22:57

The confusion here is that the cross_validation doesn't keep the models that are created, it creates the 10 models, tests them, and destroys each model after it has been tested. So at the end of the cross validation it is a blank learner again.

So you have to submit the data directly to the learner, and then you can use the learner on new data.

Re: Getting predictions from classifier with python file

Postby Ales » Fri May 16, 2014 12:28

LetterRip wrote:... So at the end of the cross validation it is a blank learner again.

So you have to submit the data directly to the learner, and then you can use the learner on new data.

Learners are always 'blank', they never retain the model and cannot be used to predict new instances. The learned models (classifiers) they produce are new objects distinct from the learner that produced them (they are not even of the same type).
Code: Select all
learner = Orange.classification.bayes.NaiveLearner()  # Create a learner instance
classifier = learner(data)  # Train a classifier using the learner
# Classifier and learner are distinct objects
assert classifier is not learner
assert classifier != learner
assert type(classifier) != type(learner)

classifier2 = learner(data2)  # Train another classifier using the same learner instance on different data
assert classifier != classfier2

# now we have two different classifiers both usable and still one learner (exactly the same as when it was created).

print classifier(data[0])  # Predict a class of a data instance using classifer
print classifier2(data2[0])  # Predict a class of a data instance using classifer2



Return to Questions & Support



cron