Skip to content

Viewcpd#1

Open
shark8me wants to merge 2 commits into
CyberPoint:masterfrom
shark8me:viewcpd
Open

Viewcpd#1
shark8me wants to merge 2 commits into
CyberPoint:masterfrom
shark8me:viewcpd

Conversation

@shark8me

Copy link
Copy Markdown

Added a method that creates and populates a Pandas dataframe with the all the assignments to all the random variables involved in the CPD, along with the conditional probabilities for each assignments.

The CPD for "Grade" in the default example gets printed this way:

    Grade   Letter  probability
    0    A   weak    0.10
    1    A   strong  0.90
    2    B   weak    0.40
    3    B   strong  0.60
    4    C   weak    0.99
    5    C   strong  0.01 

@cccabot

cccabot commented Apr 28, 2014

Copy link
Copy Markdown
Contributor

@shark8me, thank you for your contribution. We are going to refrain from incorporating it as it stands, only because it seems to introduce a new dependency to the library (Pandas). We aim to reduce libpgm's dependencies as much as possible in order to ease its installation and keep it lightweight.

However, the added functionality of the pretty-printing would certainly make libpgm more useful. If you can replicate the functionality using libpgm's current dependencies, we would be pleased to incorporate the change. Let us know what you think.

@shark8me

Copy link
Copy Markdown
Author

That's certainly logical. I've seen other libraries using either pandas or the pretty table library to add the pretty-printing feature.
Would you be interested in incorporating it if other functionality from pandas can be used?

@cccabot

cccabot commented May 1, 2014

Copy link
Copy Markdown
Contributor

What types of added functionality from Pandas do you envision? In general, if it improves or expands Bayesian network analysis capability, we would be interested in incorporating it. If it branches into other types of data analysis, it might fit better in a different project with libpgm as a dependency. Also, if the functionality could be attained without the new dependency, we would want to attain it that way.

@dov

dov commented May 9, 2015

Copy link
Copy Markdown
Contributor

Here is a simple pure python pretty print alternative:

def log10(x):
  return math.log(x)/math.log(10)

def simple_table_format(columns, rows, float_format='.5f'):
  # Plain text formatting like pandas
  num_columns = len(columns)

  # Measure the maximum column width of each column
  col_widths = [0] * num_columns
  for row in [columns] + rows:
    for j,x in enumerate(row):
      try:
        ls = len(x)
      except:
        ls = len(('%'+float_format)%x)
      if ls > col_widths[j]:
        col_widths[j] = ls

  # Get the number of digits needed for the index
  index_len = int(log10(len(rows)))+1

  # Resulting output
  output = ''
  for i,row in enumerate([columns] + rows):
    for j,x in enumerate([None] + list(row)):

      # Index column
      if j==0:
        # Only for second row on
        if i>0:
          output += ('%'+str(index_len)+'d')%(i-1)
        else:
          output += ' ' * index_len
        output += '  '
        continue

      # Column names
      if i==0:
        format = '%' + str(col_widths[j-1]) + 's'

      # Format and for either integers or floating point numbers
      else:
        format = '%' + str(col_widths[j-1])
        if isinstance(x, str) or isinstance(x, unicode):
          format += 's'
        elif isinstance(x, int):
          format += 'd'
        else:
          format += float_format

      output += format%x + '  '
    output += '\n'

  return output

def format_cpd_table(factor):
    '''returns the CDP as a pandas DataFrame like formatted string'''
    x=[factor.inputbn.Vdata[i]["vals"] for i in factor.scope]
    #creates the cartesian product
    k=[a + [b] for a,b in zip([list(i) for i in itertools.product(*x[::-1])],factor.vals)]
    return simple_table_format([i for i in reversed(factor.scope)]+['probability'],
                               k)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants