Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

classify a float column into 10 equispaced bins

 
   Database Help (Home) -> mySQL RSS
Next:  Returning a 'uniquely unique' set of rows.  
Author Message
Rahul

External


Since: Mar 24, 2009
Posts: 2



(Msg. 1) Posted: Tue Mar 24, 2009 4:25 pm
Post subject: classify a float column into 10 equispaced bins
Archived from groups: comp>databases>mysql (more info?)

I have a column returning floating point data (+-ve and -ive)

Currently I was using a snippet of this sort to classify it into five
classes (denoted by "colorbit" taking integer values -2 through +2)

CASE
WHEN
( b.BE - a.BE ) < -0.2
THEN -2
WHEN
( b.BE - a.BE ) < 0
THEN -1
WHEN
( b.BE - a.BE ) < 0.2
THEN 1
ELSE
+2
END AS colorbit,

I wanted to make this more generic by, say, always having 10 equal-width
bins between MIN and MAX. So that colorbit takes (-5 through +5) depending
on what the value of ( b.BE - a.BE ) is. MIN and MAX, of course, are the
min and max of the ( b.BE - a.BE ) column.

How can I do this via a mySQL querey?

For the curious the "colorbit" is used by a plotting script down the
pipeline to color different records with different shades.



--
Rahul

 >> Stay informed about: classify a float column into 10 equispaced bins 
Back to top
Login to vote
Gordon Burditt

External


Since: Mar 24, 2009
Posts: 1



(Msg. 2) Posted: Tue Mar 24, 2009 4:25 pm
Post subject: Re: classify a float column into 10 equispaced bins [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>Currently I was using a snippet of this sort to classify it into five
>classes (denoted by "colorbit" taking integer values -2 through +2)
>
> CASE
> WHEN
> ( b.BE - a.BE ) < -0.2
> THEN -2
> WHEN
> ( b.BE - a.BE ) < 0
> THEN -1
> WHEN
> ( b.BE - a.BE ) < 0.2
> THEN 1
> ELSE
> +2
> END AS colorbit,

I don't see where you end up with a value of 0 here.

>I wanted to make this more generic by, say, always having 10 equal-width
>bins between MIN and MAX.

Are you sure you don't want 11 bins, so the middle one can be centered
around zero?

You can do a linear map of [MIN,MAX) to [0,9). Assuming that b.BE
- a.BE always ends up in the interval [MIN, MAX), (not including
the MAX endpoint) this formula should translate the values to an
integer in the range 0 .. 9 .

FLOOR(((b.BE - a.BE)-MIN)*10/(MAX-MIN))
This formula does have a problem that it might return 10 if b.BE -
a.BE is exactly MAX (depending on rounding error or lack thereof).
If you need to range-check for out-of-bounds values, two nested
calls to if(), one for each bound, will work.

if((b.BE-a.BE) >= MAX, 9, if((b.BE-a.BE) <= MIN, 0,
FLOOR(((b.BE - a.BE)-MIN)*10/(MAX-MIN))
))

(I hope I haven't messed up the parentheses here.)


If you want something else, like a range -5 .. +4, subtract 5.

 >> Stay informed about: classify a float column into 10 equispaced bins 
Back to top
Login to vote
Rahul

External


Since: Mar 24, 2009
Posts: 2



(Msg. 3) Posted: Tue Mar 24, 2009 7:27 pm
Post subject: Re: classify a float column into 10 equispaced bins [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

gordonb.mk19e.RemoveThis@burditt.org (Gordon Burditt) wrote in


> I don't see where you end up with a value of 0 here.

Thanks Gordon! My fault. Should have used a <=.

> Are you sure you don't want 11 bins, so the middle one can be centered
> around zero?

No. The physical meaning of the data is such that -ive numbers are
different from +ive nos. So I hope to color the -ives shades of red and
the +ives shades of green. The magnitude of colorbit being keyed to the
intensity of the color.

> You can do a linear map of [MIN,MAX) to [0,9). Assuming that b.BE
> - a.BE always ends up in the interval [MIN, MAX), (not including
> the MAX endpoint) this formula should translate the values to an
> integer in the range 0 .. 9 .
>
> FLOOR(((b.BE - a.BE)-MIN)*10/(MAX-MIN))
> This formula does have a problem that it might return 10 if b.BE -
> a.BE is exactly MAX (depending on rounding error or lack thereof).
> If you need to range-check for out-of-bounds values, two nested
> calls to if(), one for each bound, will work.
>
> if((b.BE-a.BE) >= MAX, 9, if((b.BE-a.BE) <= MIN, 0,
> FLOOR(((b.BE - a.BE)-MIN)*10/(MAX-MIN))
> ))
>
> (I hope I haven't messed up the parentheses here.)
>
>
> If you want something else, like a range -5 .. +4, subtract 5.
>
>

Thanks! That ought to work well for me.


--
Rahul
 >> Stay informed about: classify a float column into 10 equispaced bins 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to re-name a column? - I am using Query Browser version 1.1.2, MySQL Administrator 1.0.14, MySQL Version 4.1.7-nt on Windows XP Service Pack 2 ALTER TABLE `purpleflavours`.`purchaseorderitem` CHANGE COLUMN `PurchaseCost` `PurchaseOrderItemCost` DOUBLE UNSIGNED ZEROFILL..

With INSERT can I increment an existing value in a column? - Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address and have thus included a column in my address table..

select query on latin1 or utf8 column: which is faster? - Assume you have two varchar (or Text) columns named L and U which are identical except that the charset for L is latin1 and the charset for U is utf8. All the records in L and U are identical in terms of content, consisting of only 7 bit ASCII characters...

Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]