Orange Forum • View topic - BayesLearner on sql data

BayesLearner on sql data

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

BayesLearner on sql data

Postby bsloane » Thu Jul 26, 2012 15:30

I am trying to tun orange.BayesLearner on data from a postgres database:

Code: Select all
from Orange.data.sql import *
import orange

def main():
    r=SQLReader()
    r.setClassName("status")
   status = Orange.feature.Discrete("status",values = "404","500","410","502","501","400","405","403","404_not_found","300","200"])
    r.setDiscreteNames(["status"])

    r.connect('postgres://sslprint:xxx@localhost/sslprint')
    r.execute('SELECT * FROM http_headers')

    data=r.data()
    classifier = orange.BayesLearner(data)
   

if __name__ == '__main__':
    main()


but when I try running the above, I get:
Code: Select all
Traceback (most recent call last):
  File "orangeAnalyze.py", line 19, in <module>
    main()
  File "orangeAnalyze.py", line 15, in main
    classifier = orange.BayesLearner(data)
orange.KernelException: Distribution: unknown value type


EDIT: After doing some debbuging, I think the issue is that all of the data is Orange.feature.String, and it needs to be numeric or discrete. Is there any way to change String to discrete, that does not involve manualy specifying all possible values like I did for status?

Re: BayesLearner on sql data

Postby bsloane » Fri Jul 27, 2012 20:48

I figured it out. The problem is that all of my data was a string type. For anyone else with this problem, below is code to translate it to all discrete types:
Code: Select all
def preProsses(OrigData):
    className="status"
    features=[]

    #Construct a discrete replacement for all features
    #TODO are there features where continuas makes sense?
    for f in OrigData.domain:
        key=f.name
        newFeature=feature.Discrete(key)
        for d in OrigData:
            value=d[key].value
            newFeature.add_value(value)

        features.append(newFeature)
    #Build a domain from the above features, second arg is the class_var
    domain=Domain(features)

    newDatas=[]
    for d in OrigData:
        values = map(lambda x:x.value,d)
        newData=data.Instance(domain,values)
        newDatas.append(newData)

    domain=Domain(domain,className); #replace the classVar in domain
    ans = Table(domain,newDatas)
    return (ans)


Re: BayesLearner on sql data

Postby zycbobby » Mon Dec 03, 2012 9:49

Any others have better solution?


Return to Questions & Support



cron