English 中文(简体)
Laplacian smoothing to Biopython
原标题:

I am trying to add Laplacian smoothing support to Biopython s Naive Bayes code 1 for my Bioinformatics project.

I have read many documents about Naive Bayes algorithm and Laplacian smoothing and I think I got the basic idea but I just can t integrate this with that code (actually I cannot see which part I will add 1 -laplacian number).

I am not familiar with Python and I am a newbie coder. I appreciate if anyone familiar with Biopython can give me some suggestions.

最佳回答

Try using this definition of the _contents() method instead:

def _contents(items, laplace=False):
    # count occurrences of values
    counts = {}
    for item in items:
        counts[item] = counts.get(item,0) + 1.0
    # normalize
    for k in counts:
        if laplace:
            counts[k] += 1.0
            counts[k] /= (len(items)+len(counts))
        else:
            counts[k] /= len(items)
    return counts

Then change the call on Line 194 into:

# Estimate P(value|class,dim)
nb.p_conditional[i][j] = _contents(values, True)

use True to enable the smoothing, and False to disable it.

Here s a comparison of the output with/without the smoothing:

# without
>>> carmodel.p_conditional
[[{ Red : 0.40000000000000002,  Yellow : 0.59999999999999998},
  { SUV : 0.59999999999999998,  Sports : 0.40000000000000002},
  { Domestic : 0.59999999999999998,  Imported : 0.40000000000000002}],
 [{ Red : 0.59999999999999998,  Yellow : 0.40000000000000002},
  { SUV : 0.20000000000000001,  Sports : 0.80000000000000004},
  { Domestic : 0.40000000000000002,  Imported : 0.59999999999999998}]]

# with
>>> carmodel.p_conditional
[[{ Red : 0.42857142857142855,  Yellow : 0.5714285714285714},
  { SUV : 0.5714285714285714,  Sports : 0.42857142857142855},
  { Domestic : 0.5714285714285714,  Imported : 0.42857142857142855}],
 [{ Red : 0.5714285714285714,  Yellow : 0.42857142857142855},
  { SUV : 0.2857142857142857,  Sports : 0.7142857142857143},
  { Domestic : 0.42857142857142855,  Imported : 0.5714285714285714}]]

Aside from the above, I think there might be a bug with the code:

The code splits the instances according to their class, and then for each class, and giving each dimension, it counts how many times each of this dimension values appear.

The problem is if for a subset of the instances belonging to one class, it happens that not all values of a dimension appear in that subset, then when the _contents() function is called, it will not see all possible values, and thus will return the wrong probabilities...

I think you need to keep track of the all unique values for each dimension (from the entire dataset), and take that into consideration during the counting process.

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签