Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (non-positive) PMI #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/composes/matrix/dense_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ def plog(self):
self.mat[self.mat < 1.0] = 1
self.mat = np.log(self.mat)

def log(self):
"""
Applies log to the matrix elements.

Elements smaller than or equal to 0 (leading to not-defined log)
are set to 0. Log is applied on all other elements.

Modifies the current matrix.
"""

#this line uses 3 x size(mat) to run in the worst case
#(if we select the entire matrix - depends on the size of the selection)
self.mat[self.mat <= 0] = 1
self.mat = np.log(self.mat)


def assert_positive(self):
"""
Expand Down
14 changes: 14 additions & 0 deletions src/composes/matrix/sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ def plog(self):
self.mat.data = np.log(self.mat.data)
self.mat.eliminate_zeros()

def log(self):
"""
Applies log to the matrix elements.

Elements smaller than or equal to 0 (leading to not-defined log)
are set to 0. Log is applied on all other elements.

Modifies the current matrix.
"""

self.mat.data[self.mat.data <= 0] = 1
self.mat.data = np.log(self.mat.data)
self.mat.eliminate_zeros()

def get_non_negative(self):
"""
Turns negative entries to 0.
Expand Down
22 changes: 22 additions & 0 deletions src/composes/transformation/scaling/pmi_weighting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

from scaling import Scaling
from epmi_weighting import EpmiWeighting

class PmiWeighting(Scaling):
"""
Point-wise Mutual Information.

:math:`pmi(r,c) = log\\frac{P(r,c)}{P(r)P(c)}`
"""

_name = "pmi"
_uses_column_stats = True

def apply(self, matrix_, column_marginal=None):

matrix_ = EpmiWeighting().apply(matrix_, column_marginal)
matrix_.log()
return matrix_

def get_column_stats(self, matrix_):
return matrix_.sum(0)