Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Data Normalization (homework?)

by BrowserUk (Patriarch)
on Apr 11, 2015 at 03:00 UTC ( [id://1123113]=note: print w/replies, xml ) Need Help??


in reply to RE: Data Normalization

Two possibilities, with your values appearing to have been chosen specifically for the latter log scaling method?:

##! perl -slw use strict; sub genScaler { my( $minout, $maxout, $minin, $maxin ) = @_; my $outRange = $maxout - $minout; my $inRange = $maxin - $minin; my $factor = $inRange / $outRange; sub { my $in = shift; return $minout + ( $in / $factor ); } } sub genLogScaler { my( $minout, $maxout, $minin, $maxin ) = @_; sub { my $in = shift; $in = 1 if $in < 1; return $minout + log( $in ); } } my $fscale = genScaler( 1, 10, 0.1, 8000 ); print "Linear:"; printf "%7.1f -> %.1f\n", $_, $fscale->( $_ ) for 0.1, 1, 10, 100, 800 +, 1000, 4000, 6000, 8000; print "\nLog:"; my $lscale = genLogScaler( 1, 10, 0.1, 8000 ); printf "%7.1f -> %.1f\n", $_, $lscale->( $_ ) for 0.1, 1, 10, 100, 800 +, 1000, 4000, 6000, 8000;

Produces:

C:\test>genScalar.pl Linear: 0.1 -> 1.0 1.0 -> 1.0 10.0 -> 1.0 100.0 -> 1.1 800.0 -> 1.9 1000.0 -> 2.1 4000.0 -> 5.5 6000.0 -> 7.8 8000.0 -> 10.0 Log: 0.1 -> 1.0 1.0 -> 1.0 10.0 -> 3.3 100.0 -> 5.6 800.0 -> 7.7 1000.0 -> 7.9 4000.0 -> 9.3 6000.0 -> 9.7 8000.0 -> 10.0

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^3: Data Normalization (homework?)
by lonewolf28 (Beadle) on Apr 11, 2015 at 15:29 UTC

    wow... I loved the way you coded. Thanks. I liked both the methods. Will try and see which method plays out well. Nope it's not a homework. In my work i'm doing risk analysis, we are trying to avoid users to cross-over from non-production to production, so when they create a firewall rule to a allow a connection, they'd be getting a risk scoring for that firewall rule.I already created a script in perl which does a raw scoring but that data would be quite intimidating for the users if not normalized.

      Please note that, as implied in the post above, the log scale generator is actually a cheat, based on noticing that the input & output ranges specified happened to lend themselves to a simplified calculation. Ie. log( 8000 ) + 1 is very close to 10.

      However, that means that genLogScaler() is basically ignoring most of its input parameters and thus won't work for the generic case.

      Here's a fixed version that works for your case and (a couple of other tested examples of) the generic case:

      #! perl -slw use strict; sub genScaler { my( $minout, $maxout, $minin, $maxin ) = @_; my $outRange = $maxout - $minout; my $inRange = $maxin - $minin; my $factor = $inRange / $outRange; sub { my $in = shift; return $minout + ( $in / $factor ); } } sub genLogScaler { my( $minout, $maxout, $minin, $maxin ) = @_; my $outRange = $maxout - $minout; my $inRange = log( $maxin - $minin ); my $factor = $inRange / $outRange; sub { my $in = shift; $in = 1 if $in < 1; return $minout + ( log( $in ) / $factor ); } } my $fscale = genScaler( 1, 10, 0.1, 8000 ); print "Linear:"; printf "%7.1f -> %.1f\n", $_, $fscale->( $_ ) for 0.1, 1, 10, 100, 800 +, 1000, 4000, 6000, 8000; print "\nLog"; my $lscale = genLogScaler( 1, 10, 0.1, 8000 ); printf "%7.1f -> %.1f\n", $_, $lscale->( $_ ) for 0.1, 1, 10, 100, 800 +, 1000, 4000, 6000, 8000; print "\nNother Log:"; my $lscale2 = genLogScaler( -5, +5, 0.1, 8000 ); printf "%7.1f -> %.1f\n", $_, $lscale2->( $_ ) for 0.1, 1, 10, 100, 80 +0, 1000, 4000, 6000, 8000; print "\nNother Log:"; my $lscale3 = genLogScaler( 0, +1000, 0.1, 8000 ); printf "%7.1f -> %.1f\n", $_, $lscale3->( $_ ) for 0.1, 1, 10, 100, 80 +0, 1000, 4000, 6000, 8000;

      Produces:

      C:\test>genScalar.pl Linear: 0.1 -> 1.0 1.0 -> 1.0 10.0 -> 1.0 100.0 -> 1.1 800.0 -> 1.9 1000.0 -> 2.1 4000.0 -> 5.5 6000.0 -> 7.8 8000.0 -> 10.0 Log 0.1 -> 1.0 1.0 -> 1.0 10.0 -> 3.3 100.0 -> 5.6 800.0 -> 7.7 1000.0 -> 7.9 4000.0 -> 9.3 6000.0 -> 9.7 8000.0 -> 10.0 Nother Log: 0.1 -> -5.0 1.0 -> -5.0 10.0 -> -2.4 100.0 -> 0.1 800.0 -> 2.4 1000.0 -> 2.7 4000.0 -> 4.2 6000.0 -> 4.7 8000.0 -> 5.0 Nother Log: 0.1 -> 0.0 1.0 -> 0.0 10.0 -> 256.2 100.0 -> 512.4 800.0 -> 743.8 1000.0 -> 768.6 4000.0 -> 922.9 6000.0 -> 968.0 8000.0 -> 1000.0

      But note: it still contains the hack-y $in = 1 if $in < 1; fix.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1123113]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found