473,480 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I want to create something like AMAZON: those who liked page A also liked page B

Hi Folk

I want to create something like AMAZON: those who liked page A also liked
page B (I am going to apply the concept to a few different selections, but
to keep it simple I will talk about page popularity here - like AMAZON talks
about purchase patterns: those who bought A also bought B, C and D).

In my mysql table, I record everytime a visitor looks at a particular page
(I keep track of people with sessions and they can only look at a page
once).

Next, what I want to do is to show a small list of similar likes. That is,
people who had an interest in page A also had an interest in page B.

The structure of my table is:
id
timestamp
session_id
page_name

There is a unique index on the combination of session_id and page_name.

To work out similar likes for page X, my idea was to make an array of all
the session ids that looked at page X and then select the top 10 of pages
that these folk looked at.

My question is, should I do this in PHP with a bunch of arrays, adding total
counts while looping through an array or shall I use SQL statements. I
could even make a really long string of WHERE session_id = A or session_id =
B, etc..... Although this seems really inefficient.

Another question I have is whether you think this will be a self-fulfilling
prophecy (e.g. if you say that page Y is also popular then people will check
it out and therefore make it more popular).

TIA

- Nicolaas


Oct 26 '05 #1
8 1473
This problem is actually more difficult than one might think -- if you
want to get accurate results. Companies with very large datasets like
Amazon spend a huge number of processor cycles to come up with those
types of rankings.

Do a search for "collaborative filtering" and "nearest neighbor." I
think you'll find that itll be much easier to implement any of the
several algorithms in a language other than SQL.

Oct 26 '05 #2
The problem is actually not as difficult as you might think - and you
can get accurate results

:-)
First thing you need to realise is that you need your table in two
ways:

a) you need to look up id's which have accessed same page as the
current page
b) you need to look up the pages which those id's have accessed

also

c) you need to exclude the current page in b)
d) you need to count the number of times each related page occurs
e) you need to rank the results in order of popularity
This might sound like a programming problem, but it can be done in one
SQL statement

I'll build it up so you can see how I got there:

SELECT * FROM mytable AS t1

gets the whole table and lets you refer to it as "t1"
SELECT * FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id

this gets you shedloads of results - one line for every combination
so if id#1 has visited pages a, b & c then you get

a-a
a-b
a-c
b-a
b-b
b-c
c-a
c-b
c-c

but we only want the ones for, say, page "a", and we only need
(from this data) the 'other' page

SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'

this (for id#1)

would produce

b
c

running against all the data, would produce a whole list of pages - all
the pages that anyone who has visited page 'a' has also been to

If we group by page name, we'll get one line per page name
SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name

g
d
e
f
b
c

If we add a count, we get the number of times that page turned up
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
g 10
d 27
e 19
f 41
b 110
c 83
And if we order by descending popularity, we're almost there

SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC

b 110
c 83
f 41
d 27
e 19
g 10

The last thing to do is limit the results to the top 5 results
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC
LIMIT 5
b 110
c 83
f 41
d 27
e 19

And there's your list
....

Ian

Oct 26 '05 #3
To answer your other question, it will be self-fulfilling (IMHO).
Amazon has the advantage of sales (which is the ultimate indicator of
whether the visitor wanted to go to that page) but you have no such
feedback, so everyone who goes to page a might then click through to
page b and hate it, but it will become more 'popular' in your rating
system.

You could try to balance this out by using the same SQL but with the
DESC removed and have a link "Almost no-one went to these pages - find
out why!"

:-)

I've done this on a web site which searches for businesses, and no-one
stays at the bottom of the popularity list for long

Ian

Oct 26 '05 #4
Ian B wrote:
The problem is actually not as difficult as you might think - and you
can get accurate results

:-)
First thing you need to realise is that you need your table in two
ways:

a) you need to look up id's which have accessed same page as the
current page
b) you need to look up the pages which those id's have accessed

also

c) you need to exclude the current page in b)
d) you need to count the number of times each related page occurs
e) you need to rank the results in order of popularity
This might sound like a programming problem, but it can be done in one
SQL statement

I'll build it up so you can see how I got there:

SELECT * FROM mytable AS t1

gets the whole table and lets you refer to it as "t1"
SELECT * FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id

this gets you shedloads of results - one line for every combination
so if id#1 has visited pages a, b & c then you get

a-a
a-b
a-c
b-a
b-b
b-c
c-a
c-b
c-c

but we only want the ones for, say, page "a", and we only need
(from this data) the 'other' page

SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'

this (for id#1)

would produce

b
c

running against all the data, would produce a whole list of pages -
all the pages that anyone who has visited page 'a' has also been to

If we group by page name, we'll get one line per page name
SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name

g
d
e
f
b
c

If we add a count, we get the number of times that page turned up
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
g 10
d 27
e 19
f 41
b 110
c 83
And if we order by descending popularity, we're almost there

SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC

b 110
c 83
f 41
d 27
e 19
g 10

The last thing to do is limit the results to the top 5 results
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC
LIMIT 5
b 110
c 83
f 41
d 27
e 19

And there's your list
...

Ian


I never read a more indepth and clearer reply!!!!!

Thank you SO MUCH (a lot more than a million).

Nicolaas
Nov 8 '05 #5
Ian B wrote:
To answer your other question, it will be self-fulfilling (IMHO).
Amazon has the advantage of sales (which is the ultimate indicator of
whether the visitor wanted to go to that page) but you have no such
feedback, so everyone who goes to page a might then click through to
page b and hate it, but it will become more 'popular' in your rating
system.

You could try to balance this out by using the same SQL but with the
DESC removed and have a link "Almost no-one went to these pages - find
out why!"

:-)

I've done this on a web site which searches for businesses, and no-one
stays at the bottom of the popularity list for long

Ian


I can solve this by using a session variable that makes clicks from the
popularity list be excluded from the popularity list, thereby reducing some
of the self-fulfilling prophecy clicks lead to more clicks.

THANKS
- Nicolaas
Nov 8 '05 #6
Ian B wrote:
The problem is actually not as difficult as you might think - and you
can get accurate results

:-)
First thing you need to realise is that you need your table in two
ways:

a) you need to look up id's which have accessed same page as the
current page
b) you need to look up the pages which those id's have accessed

also

c) you need to exclude the current page in b)
d) you need to count the number of times each related page occurs
e) you need to rank the results in order of popularity
This might sound like a programming problem, but it can be done in one
SQL statement

I'll build it up so you can see how I got there:

SELECT * FROM mytable AS t1

gets the whole table and lets you refer to it as "t1"
SELECT * FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id

this gets you shedloads of results - one line for every combination
so if id#1 has visited pages a, b & c then you get

a-a
a-b
a-c
b-a
b-b
b-c
c-a
c-b
c-c

but we only want the ones for, say, page "a", and we only need
(from this data) the 'other' page

SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'

this (for id#1)

would produce

b
c

running against all the data, would produce a whole list of pages -
all the pages that anyone who has visited page 'a' has also been to

If we group by page name, we'll get one line per page name
SELECT t2.page_name FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name

g
d
e
f
b
c

If we add a count, we get the number of times that page turned up
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
g 10
d 27
e 19
f 41
b 110
c 83
And if we order by descending popularity, we're almost there

SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC

b 110
c 83
f 41
d 27
e 19
g 10

The last thing to do is limit the results to the top 5 results
SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC
LIMIT 5
b 110
c 83
f 41
d 27
e 19

And there's your list
...

Ian


The only thing one could probably add is to factor in how many places were
visited by someone in total. That is, a person who looked at alsmost all
pages should not have an influence as great as someone who only looked at
two pages (i.e. those links are meaningful).

How to do that in SQL as described above, I have no idea, but I may look
into this later.
Nov 8 '05 #7
How does this site work? Why do you need to exclude people from pages
that they have been to before? And why does it mean less if you visit
more pages? I'd be interested to know.

Nov 8 '05 #8
Gazornenplat wrote:

How does this site work? .
Here is how it works in mysql:

SELECT t2.page_name,Count(t2.page_name) AS popularity FROM mytable AS
t1
LEFT JOIN mytable AS t2 ON t1.session_id = t2.session_id
WHERE t1.page_name = 'a'
AND t2.page_name <> 'a'
GROUP BY t2.page_name
ORDER BY Count(t2.page_name) DESC
LIMIT 5

where mytable is a list of unique combinations of session IDs and pages
visited.
e.g. a table with the following fields:

ID, session_id, page_name

where you can only have unique combos of session_id and page_names

The sql will give you a list of the five most popular pages for people who
visited page "a"

Why do you need to exclude people from pages
that they have been to before?

If you publish this list with links to these "also popular" pages, then you
need to exclude these visits from my_table, otherwise it becomes a
self-fulfilling prophecy. That is, once a page is popular, it will become
more popular because it is popular, etc....

And why does it mean less if you visit
more pages?


People who visit many pages will have more influence on the "also popular"
list. However, the link for them between page a and b is less strong,
because there is also a link between page a and c, a and d, a and e, and so
on. While someone who only visits page a and b shows a clear link.

I am actually going to look at more than just page visits, I am going to
look at things like click-throughs to links from a page and email message as
these will be more meaningful than just page views.

HTH

- Nicolaas
Nov 8 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2976
by: Chris Cioffi | last post by:
I started writing this list because I wanted to have definite points to base a comparison on and as the starting point of writing something myself. After looking around, I think it would be a...
0
1605
by: Sridhar | last post by:
Hi, We, the students of CEG, Anna University are organizing an online programming contest as part of aBaCus 2005. The contest itself will start on 6th March 2005 at 1:00 pm IST and will end...
14
2237
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
4
1186
by: Angelos | last post by:
Ok ... I want to Create a Content Management System that I will add pages and then it will automatically create a menu system and add each page in it.... Also if I have a page with products......
8
1679
by: windandwaves | last post by:
Hi Folk I want to create something like AMAZON: those who liked page A also liked page B (I am going to apply the concept to a few different selections, but to keep it simple I will talk about...
14
3062
by: Eric Lindsay | last post by:
I've seen a page using display, and especially display table that did some neat things with boxes, but basically it only worked with Mozilla browsers. Fell over fairly badly with Opera and Safari...
4
2266
by: HEATHER CARTER-YOUNG | last post by:
Please help. I have two databases - one I'm designing that will be our in-house data mgmt system (db1) and another that is a federally-mandated system (db2). We must submit data from db2 but don't...
6
2177
by: Peter Row | last post by:
Hi, Can someone give me a set of steps for an ASP.NET project (well actually its just a VB.NET class DLL that implements HttpHandler) that will work when moved to another developers machine? ...
16
1346
by: John A. Bailo | last post by:
I was pricing VS.NET Professional 2005 upgrade and found a good price on Amazon -- but then I read the customer reviews and they were terrible: ...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.