My Computer Forum Computer Science Forum

Go Back   My Computer Forum > Computer Science Forum > Algorithms

Algorithms Algorithms and Data Structures - Analysis, Graph, Search, String, Sorting, Merge, Compression, Optimization, Quantum


Reply
 
LinkBack Thread Tools Display Modes
April 27th, 2008, 08:56 AM   #1
 
Joined: Dec 2007
Posts: 138
How to weight the data?

I'm doing a project in perl and I'm wondering what the best way to weight/sort my data is.

As it stands, I have a horribly kludged together array where each element is an array which contains all the data... a wise programmer would have constructed a class, and made an array of objects... I am no such creature.

Anyway, I'm going to define a weighting function, and sort my data based on the weight of the element, then start shifting them off based on that.
E.g. if foo has weight 3 and bar has weight 5, my array should look like:

(bar, foo), so I'd shift bar off the list for my first choice...

My question comes in here:

What is the best way to weight and sort the data? Ideas that have come to mind are:
1) create a hash of (object => weight), and sort that. The problem is that I don't quite understand how perl handles hashes, and sorting, and I really don't want to spend time learning it.

2) unshift the weight onto the front of each subarray, so that each "data element" is (weight, value1, value2, etc). The 2 problems with this are: the first value in the array "should" be the key for a hash anyway, but the arrays are easier to work with than a hash; and the only way to get an array of arrays in perl is to reference them and
[code] @bar[0] =\@foo;
print" $($bar[0])[0]\n"; #if that works... [\code]
is a pain in the ass, and I don't want to deal with that.

3) two arrays; the obvious problem here is this violates abstraction, and if I forget to update the two arrays together, I'll have nonsense.

4) Actually make a class, so each of my data elements are objects... this would either 1) make the code I already have look even uglier in comparison, or 2) require me to rewrite my code. Neither of these seem optimal.
cknapp is offline  
 

My Computer Forum is free to register and we welcome everyone!

April 27th, 2008, 08:07 PM   #2
 
Joined: Dec 2007
Posts: 232
Re: How to weight the data?

Quote:
Originally Posted by cknapp
As it stands, I have a horribly kludged together array where each element is an array which contains all the data...
That's called a jagged array. It's just as efficient as class reference, but maybe a little harder on you (since you say "kludged").

Quote:
Originally Posted by cknapp
1) create a hash of (object => weight), and sort that. The problem is that I don't quite understand how perl handles hashes, and sorting, and I really don't want to spend time learning it.
No, bad. That will sort the weights in apparently-random order rather than the order you want.

Quote:
Originally Posted by cknapp
2) unshift the weight onto the front of each subarray, so that each "data element" is (weight, value1, value2, etc). The 2 problems with this are: the first value in the array "should" be the key for a hash anyway, but the arrays are easier to work with than a hash; and the only way to get an array of arrays in perl is to reference them and
[code] @bar[0] =\@foo;
print" $($bar[0])[0]\n"; #if that works... [\code]
is a pain in the ass, and I don't want to deal with that.
This, or some variant of it would be good.

Quote:
Originally Posted by cknapp
3) two arrays; the obvious problem here is this violates abstraction, and if I forget to update the two arrays together, I'll have nonsense.
If by that you mean an array of weights and indexes into the second array, then that would be even more efficient:

foo: weight 1, ...foo data...
bar: weight 5, ...bar data...
spam: weight 3, ...spam data...
eggs: weight 8, ...eggs data...

to

((1, 0), (5, 1), (3, 2), (8, 3))
and
(...foo data..., ...bar data..., ...spam data..., ...eggs data...)

Quote:
Originally Posted by cknapp
4) Actually make a class, so each of my data elements are objects... this would either 1) make the code I already have look even uglier in comparison, or 2) require me to rewrite my code. Neither of these seem optimal.
If you were in Java or C# I'd recommend this, but not in Perl. That's anti-Perl, making classes where they really aren't needed.
CRGreathouse is offline  
April 28th, 2008, 07:46 AM   #3
 
Joined: Dec 2007
Posts: 138
Re: How to weight the data?

Quote:
Originally Posted by CRGreathouse
That's called a jagged array. It's just as efficient as class reference, but maybe a little harder on you (since you say "kludged").
Yeah, it's annoying to deal with... although perhaps no more than dealing with objects...

Quote:
Quote:
Originally Posted by cknapp
1) create a hash of (object => weight), and sort that. The problem is that I don't quite understand how perl handles hashes, and sorting, and I really don't want to spend time learning it.
No, bad. That will sort the weights in apparently-random order rather than the order you want.
So I figured

Quote:
Quote:
Originally Posted by cknapp
2) unshift the weight onto the front of each subarray, so that each "data element" is (weight, value1, value2, etc). The 2 problems with this are: the first value in the array "should" be the key for a hash anyway, but the arrays are easier to work with than a hash; and the only way to get an array of arrays in perl is to reference them and
Code:
 @bar[0] =\@foo;
print" $($bar[0])[0]\n"; #if that works...
is a pain in the ass, and I don't want to deal with that.
This, or some variant of it would be good.
Hmm...

Quote:
Quote:
Originally Posted by cknapp
3) two arrays; the obvious problem here is this violates abstraction, and if I forget to update the two arrays together, I'll have nonsense.
If by that you mean an array of weights and indexes into the second array, then that would be even more efficient:

foo: weight 1, ...foo data...
bar: weight 5, ...bar data...
spam: weight 3, ...spam data...
eggs: weight 8, ...eggs data...

to

((1, 0), (5, 1), (3, 2), (8, 3))
and
(...foo data..., ...bar data..., ...spam data..., ...eggs data...)
That's what I had in mind. So... you seem to like this idea... I think you're a trustworthy source...

Quote:
Quote:
Originally Posted by cknapp
4) Actually make a class, so each of my data elements are objects... this would either 1) make the code I already have look even uglier in comparison, or 2) require me to rewrite my code. Neither of these seem optimal.
If you were in Java or C# I'd recommend this, but not in Perl. That's anti-Perl, making classes where they really aren't needed.
So I figured...

Now if I could only figure out why my code is using the wrong argument, I'll be ok...
Note to self: Do not do a project in a new language at the last minute...
cknapp is offline  
April 28th, 2008, 09:56 AM   #4
 
Joined: Dec 2007
Posts: 232
Re: How to weight the data?

Quote:
Originally Posted by cknapp
That's what I had in mind. So... you seem to like this idea... I think you're a trustworthy source...
Trustworthy, yes. Good at writing efficient algorithms, mostly. Good at trendy programming languages... not really. Go talk to that kid cknapp about that. :lol:
CRGreathouse is offline  
April 28th, 2008, 02:19 PM   #5
 
Joined: Dec 2007
Posts: 138
Re: How to weight the data?

Quote:
Originally Posted by CRGreathouse
Good at trendy programming languages... not really. Go talk to that kid cknapp about that. :lol:
Haha.

As far as perl, I'm learning it to work on a project with a professor, and in (the same professor's) class, I have to write a program with a lot of file i/o... specifically the kind of I/O that regexes are optimized for... Hence, I might as well jump in over my head... I've been doing that for 3 years, why stop now?

It's funny, because I first started learning Scheme because I figured it would be useful for this class (AI)... The two languages I'll be spending the most time in for the next few months, and I learned them both for the same class... Oh, dear...
cknapp is offline  
April 29th, 2008, 11:49 AM   #6
 
Joined: Dec 2007
Posts: 232
Re: How to weight the data?

(Save some ellipses for the rest of us, man!)
Quote:
Originally Posted by cknapp
As far as perl, I'm learning it to work on a project with a professor, and in (the same professor's) class, I have to write a program with a lot of file i/o... specifically the kind of I/O that regexes are optimized for.
Perl's natural for that, yeah. And it's an easy language to learn, so I wouldn't worry about it.

Quote:
Originally Posted by cknapp
It's funny, because I first started learning Scheme because I figured it would be useful for this class (AI).
Has it been? I've heard that, too, about LISP-relatives.
CRGreathouse is offline  
April 29th, 2008, 02:34 PM   #7
 
Joined: Dec 2007
Posts: 138
Re: How to weight the data?

It hasn't because it's been all theory, and very little application... so this is our one real project, and it's file I/O, so, yeah...

As far as AI in general, LISP is optimized for
1)Writing lisp-writing programs
2)re-writing/overriding the language

Which are both really useful for learning algorithms and genetic algorithms... two large parts of AI.

It's also a lot closer to a small formal system than most other languages, which makes it optimal for bridging the theory-application gap.
cknapp is offline  
April 29th, 2008, 03:41 PM   #8
 
Joined: Dec 2007
Posts: 232
Re: How to weight the data?

Quote:
Originally Posted by cknapp
As far as AI in general, LISP is optimized for
1)Writing lisp-writing programs
2)re-writing/overriding the language
Oddly, I would do neither for algorithms, not even genetic algorithms.
CRGreathouse is offline  
April 29th, 2008, 06:31 PM   #9
 
Joined: Dec 2007
Posts: 138
Re: How to weight the data?

"learning algorithm" was meant as one term, as in machine learning, not "to learn an algorithm".
cknapp is offline  
April 29th, 2008, 06:42 PM   #10
 
Joined: Dec 2007
Posts: 232
Re: How to weight the data?

Ah. Well I've never written a learning algorithm, but the genetic algorithms I've written haven't ever needed to do either. They stayed entirely within the von Neumann model.
CRGreathouse is offline  
Reply

  My Computer Forum > Computer Science Forum > Algorithms

Tags
data, weight



Thread Tools
Display Modes






Copyright © 2018 My Computer Forum Forum. All rights reserved.