PROBLEM AT HAND: every row in my csv file has the same value.
I am trying to implement a term document matrix and store it in a CSV file.In my program I store the features and the value within a hash for a document. Each hash is stored in an array. The array then is stored within another array to seperate the document classes.
I get the hash function by going through an array and adding the terms to the hash as follows (hash %termFreq is specific for the document and hash %docTerms is the features of all documents):
while ($element = shift(@numOfWordArr)) {
$termsFreq{$element} ++;
if(!exists($docTerms{$element}))
{
$docTerms{$element}++;
}
}
Then add the hash into an array of documents as follows:
push(@docArray, \%termsFreq);
Finally pass the array into an array to seperate the classes of the documents:
push(@classArr, \@docArray);
took the advice from another thread to iterate through the arrays to get a hash value and print the doc matrix to a csv file:
foreach my $subArr_ref(@classArr) {
print "subArr_ref: ".@{$subArr_ref}."\n";
foreach my $hashRef(@{$subArr_ref}) {
print "hashRef: ".$hashRef."\n";
foreach my $key (sort keys %{$hashRef}) {
#print $csv $key.":".$hashRef->{$key}.",";
print $csv "$key : ${$hashRef}{$key},";
}
# foreach my $feat(@featureVector) {
# print $csv $hashRef->{$feat}.",";
# }
print $csv $i."\n";
}
My problem is that I get rows of the same value since it is accessing the same hash per iteration.
Help is much appreciated.
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.