www.allitebooks.com

Learning%20Data%20Mining%20with%20Python Learning%20Data%20Mining%20with%20Python

24.07.2016 Views

Chapter 4 We will sample our dataset to form a training dataset. This also helps reduce the size of the dataset that will be searched, making the Apriori algorithm run faster. We obtain all reviews from the first 200 users: ratings = all_ratings[all_ratings['UserID'].isin(range(200))] Next, we can create a dataset of only the favorable reviews in our sample: favorable_ratings = ratings[ratings["Favorable"]] We will be searching the user's favorable reviews for our itemsets. So, the next thing we need is the movies which each user has given a favorable. We can compute this by grouping the dataset by the User ID and iterating over the movies in each group: favorable_reviews_by_users = dict((k, frozenset(v.values)) for k, v in favorable_ratings groupby("UserID")["MovieID"]) In the preceding code, we stored the values as a frozenset, allowing us to quickly check if a movie has been rated by a user. Sets are much faster than lists for this type of operation, and we will use them in a later code. Finally, we can create a DataFrame that tells us how frequently each movie has been given a favorable review: num_favorable_by_movie = ratings[["MovieID", "Favorable"]]. groupby("MovieID").sum() We can see the top five movies by running the following code: num_favorable_by_movie.sort("Favorable", ascending=False)[:5] Let's see the top five movies list: MovieID Favorable 50 100 100 89 258 83 181 79 174 74 [ 67 ]

Recommending Movies Using Affinity Analysis The Apriori algorithm The Apriori algorithm is part of our affinity analysis and deals specifically with finding frequent itemsets within the data. The basic procedure of Apriori builds up new candidate itemsets from previously discovered frequent itemsets. These candidates are tested to see if they are frequent, and then the algorithm iterates as explained here: 1. Create initial frequent itemsets by placing each item in its own itemset. Only items with at least the minimum support are used in this step. 2. New candidate itemsets are created from the most recently discovered frequent itemsets by finding supersets of the existing frequent itemsets. 3. All candidate itemsets are tested to see if they are frequent. If a candidate is not frequent then it is discarded. If there are no new frequent itemsets from this step, go to the last step. 4. Store the newly discovered frequent itemsets and go to the second step. 5. Return all of the discovered frequent itemsets. This process is outlined in the following workflow: [ 68 ]

Chapter 4<br />

We will sample our dataset to form a training dataset. This also helps reduce<br />

the size of the dataset that will be searched, making the Apriori algorithm run faster.<br />

We obtain all reviews from the first 200 users:<br />

ratings = all_ratings[all_ratings['UserID'].isin(range(200))]<br />

Next, we can create a dataset of only the favorable reviews in our sample:<br />

favorable_ratings = ratings[ratings["Favorable"]]<br />

We will be searching the user's favorable reviews for our itemsets. So, the next thing<br />

we need is the movies which each user has given a favorable. We can <strong>com</strong>pute this<br />

by grouping the dataset by the User ID and iterating over the movies in each group:<br />

favorable_reviews_by_users = dict((k, frozenset(v.values))<br />

for k, v in favorable_ratings<br />

groupby("UserID")["MovieID"])<br />

In the preceding code, we stored the values as a frozenset, allowing us to quickly<br />

check if a movie has been rated by a user. Sets are much faster than lists for this type<br />

of operation, and we will use them in a later code.<br />

Finally, we can create a DataFrame that tells us how frequently each movie has been<br />

given a favorable review:<br />

num_favorable_by_movie = ratings[["MovieID", "Favorable"]].<br />

groupby("MovieID").sum()<br />

We can see the top five movies by running the following code:<br />

num_favorable_by_movie.sort("Favorable", ascending=False)[:5]<br />

Let's see the top five movies list:<br />

MovieID Favorable<br />

50 100<br />

100 89<br />

258 83<br />

181 79<br />

174 74<br />

[ 67 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!