473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ 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 1478
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 "collaborat ive 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,Co unt(t2.page_nam e) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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_n ame) 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,Co unt(t2.page_nam e) 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_n ame) 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
2990
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 waste of time to start writing yet another IDE and so am now thinking in terms of new features/plug-ins for existing systems. Right now I mostly use Komodo Personal and it's pretty close to being what I want. They don't currently support plug-ins,...
0
1613
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 after 5 hours. You have to solve the problems posted at the start of the contest. Teams ranking high will be awared the prizes. As a special note, inspite of C,C++ and Java we also allow Python this time. So we hope a lot of Pythonistas...
14
2257
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
4
1194
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... it should also add the products and create the Menu system for them... I know it is possible... I am just too busy to start all over and I was wondering if someone has alleredy done something simillar....
8
1690
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 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...
14
3078
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 (I don't have IE, but I have my suspicions that wouldn't work either). I'd like to use CSS to put some boxes down the middle of a page. The page is intended to be fluid, but the boxes all need to be the same width, which can be variable...
4
2281
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 want to use this db for our data mgmt needs. Some of the fields in db1 are shared with db2 but the structure and field names are different. When I'm updating/creating records in db1 I want it to see if the record exists in db2 (shared ID number),...
6
2186
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? Here are some specifics: 1) The code, project/solution files are in MS Visual Source Safe
16
1363
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: http://www.amazon.com/exec/obidos/ASIN/B000BT8TRG/ref%3Dpd%5Fkar%5F3/103-1460775-6910259 For example: "Lots of nifty features, but unfortunately it needs a few more months of development before it will really be stable. "
0
8002
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8496
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8338
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5475
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1594
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.