Advertising
- slope1.d
- Tuesday, August 28th, 2007 at 11:41:48am MDT
- void init(T, U, V)(ref T[U] assoc, U where, lazy V how) {
- static assert(is(V: T));
- if (!(where in assoc)) assoc[where]=how;
- }
- struct slope1(I, U) { /// Items and Users
- struct userPrefs { U user; float[I] ratings; }
- float[I][I] diffs;
- size_t[I][I] freqs;
- void update(userPrefs[] prefs) {
- foreach (entry; prefs) foreach (item, rating; entry.ratings) {
- init(diffs, item, (float[I]).init);
- init(freqs, item, (size_t[I]).init);
- foreach (item2, rating2; entry.ratings) {
- init(diffs[item], item2, 0f);
- init(freqs[item], item2, 0);
- freqs[item][item2]+=1;
- diffs[item][item2]+=rating-rating2;
- }
- }
- foreach (item, ratings; diffs)
- foreach (item2, rating; ratings)
- ratings[item2]/=freqs[item][item2];
- }
- float[I] predict(userPrefs prefs) {
- float[I] preds; size_t[I] freqs;
- foreach (item, rating; prefs.ratings) {
- foreach (diffitem, diffratings; diffs) {
- if (!(diffitem in this.freqs) || (!(item in this.freqs[diffitem]))) continue;
- auto freq=this.freqs[diffitem][item];
- init(preds, diffitem, 0f);
- init(freqs, diffitem, 0);
- preds[diffitem]+=freq*(diffratings[item] + rating);
- freqs[diffitem]+=freq;
- }
- }
- float[I] res;
- foreach (item, value; preds) if (!(item in prefs.ratings) && freqs[item]>0) res[item]=value/freqs[item];
- return res;
- }
- }
- template constEnum(T, int start, S...) {
- static if(S.length)
- const char[] constEnum="const "~T.stringof~" "~S[0]~" = "~start.stringof~"; "~constEnum!(T, start+1, S[1..$]);
- else const char[] constEnum="";
- }
- import std.stdio;
- void main() {
- typedef int critters;
- mixin(constEnum!(critters, 0, "cuttlefish", "octupus", "squid", "nautilus"));
- typedef slope1!(critters, string) mySlope;
- mySlope s;
- s.update([
- mySlope.userPrefs("alice", [squid: 1f, cuttlefish: .5f, octupus: .2f]),
- mySlope.userPrefs("bob", [squid: 1f, octupus: 0.5f, nautilus: 0.2f]),
- mySlope.userPrefs("carole", [squid: .2f, octupus: 1f, cuttlefish: .4f, nautilus: .4f]),
- mySlope.userPrefs("dave", [cuttlefish: 0.9f, octupus: 0.4f, nautilus: 0.5f])
- ]);
- writefln(s.predict(mySlope.userPrefs("", [squid: 0.4])));
- }
- xt4100 ~/d $ gdc slope1.d -o slope1 && ./slope1
- [0:0.25,1:0.233333,3:0.1]
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.