Question
Part B: Discovering genres? (Exercises 3-5) Each review connects a user to a book. Suppose we want to cluster users into groups based on what
Part B: Discovering genres? (Exercises 3-5)
Each review connects a user to a book. Suppose we want to cluster users into groups based on what books they reviewed; or similarly, suppose we want to cluster books together based on who reviewed them. This kind of clustering betweentwotimes of objects is sometimes referred to as abiclusteringproblem.
To biclustering, we first need to build a (sparse) matrix that encodes these connections. Here is one way. LetA
Aanmn
mnmatrix ofm
musers (rows) andn
nbooks (columns). Let each entrya
i,j
ai,jbe the rating that useri
igave to bookj
j. With such a matrix, we can then use a biclustering algorithm calledspectral co-clusteringto construct both user-user and book-book clusters.
Exercise 3 (2 points): Map string IDs to logical indices
Suppose you are given a pandasSeriesobject,x, whose values are strings. For example, supposexis the followingSeries:
xcatdogcatcataardvarkdoganemoneaardvark
Write function,create_map(x), so that it does the following:
- It determines the unique values inx.
- It sorts these unique values in ascending order.
- It assigns each unique value to a unique integer, starting from 0 and corresponding to the value's position in sorted order.
- It constructs and returns a Python dictionary that mapsx-values (keys) to integers (values).
For instance, for the precedingx, the unique sorted values would beaardvark,anemone,cat, anddog. Therefore, we would assignaardvarkto 0,anemoneto 1,catto 2, anddogto 3. Therefore, the function would return the dictonary,
{'aardvark': 0, 'anemone': 1, 'cat': 2, 'dog', 3}
Note:Your function mustnotmodify the inputSeries,x. The test cell will check for that and may fail with strange errors if you do so.
In[]:
def create_map(x): assert isinstance(x, pd.Series) ### ### YOUR CODE HERE ###
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started