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

RE: Data Normalization

by lonewolf28 (Beadle)
on Apr 11, 2015 at 02:05 UTC ( [id://1123107]=perlquestion: print w/replies, xml ) Need Help??

lonewolf28 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have data ranging from 0.01 to 8000 and i would like to normalize that range to 1 to 10. It's like a scoring mechanism that i'd like to implement in my code.For example if say value 8000, the score should be 10. How can i achieve this ? Thanks for your time. -cheers

Replies are listed 'Best First'.
Re^2: Data Normalization (homework?)
by BrowserUk (Patriarch) on Apr 11, 2015 at 03:00 UTC

    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

      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
Re^2: Data Normalization
by aaron_baugher (Curate) on Apr 11, 2015 at 02:28 UTC

    Like this?

    #!/usr/bin/env perl use 5.010; use strict; use warnings; sub normalize { my $n = shift; # start with $n between .01 and 8000 inclusive return( ($n - .01) # shift range to begin at 0 / 7999.99 # divide by original range * 9 # multiply by new range + 1); # shift up to new start of 1 } say "$_ : ". normalize($_) for (.01, 1, 3000, 8000);

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re^2: Data Normalization
by marinersk (Priest) on Apr 11, 2015 at 02:37 UTC
    Linear or exponential normalization?
Re^2: Data Normalization (arithmetic)
by Anonymous Monk on Apr 11, 2015 at 02:29 UTC
    so 10 / 8000 = 0.00125 , so
    $ perl -e " printf qq{ %9d %10f %.0f\n}, $_, $_*0.00125, $_*0.00125 fo +r 7999,4000,555,400,0.01 " 7999 9.998750 10 4000 5.000000 5 555 0.693750 1 400 0.500000 0 0 0.000013 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found