Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As for algorithm, it seems like you are just looking for large correlations among keywords. Somtimes people look for large coorelations among keywords as a marker for eliminating redundant variables. If A and B always appear together, you don't need both. Such redunandcy reduction is often used in statisitcal modeling to arrive at an independent set of variables upon which to base a model.

As for code to implement this measure, it is simple to use HoHs:

my %items = ( z => [ qw/one six/ ], 'y' => [ qw/two three five/ ], x => [ qw/one two five/ ], ); my %corr; foreach my $item (keys %items) { my $max = scalar @{$items{$item}} - 1; for my $first (0..$max) { for my $second ($first+1..$max) { $corr{ $items{$item}[$first] }{ $items{$item}[$second] }++; } } } for my $first (keys %corr) { for my $second (keys %{$corr{$first}}) { print "$first $second: $corr{$first}{$second}\n"; } }
Update: Note that this isn't a full correlation calculation, but implements the OPs desired numbers for a 2-way measure. As tall_man pointed out, I blew it :) Here is the corrected code:
foreach my $item (keys %items) { my @set = sort @{$items{$item}}; for my $first (0..$#set) { for my $second ($first+1..$#set) { $corr{ $set[$first] }{ $set[$second] }++; } } } for my $first (sort keys %corr) { for my $second (sort keys %{$corr{$first}}) { print "$first $second: $corr{$first}{$second}\n"; } }

-Mark


In reply to Re: algorithm for 'best subsets' by kvale
in thread algorithm for 'best subsets' by halley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-25 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found