【发布时间】:2018-02-07 06:16:18
【问题描述】:
我有一个字典,其中键作为我的客户 ID,值作为我的电影 ID。尽管客户已经多次观看同一部电影,但我希望它成为一部。 在这里,我需要将我的字典转换为二进制数据。 在所有行中,我需要客户 ID 和列作为电影 ID,如果客户看过电影,则为 1,否则为 0。
d = {'121212121' : 111, 222, 333, 333,444, 444, '212121212' : 222, 555, 555, 666, '212123322' : 555, 666, 666, 666, 777}
期望的输出:
customer ID 111 222 333 444 555 666 777
121212121 1 1 1 1 0 0 0
212121212 0 1 0 0 1 1 0
121323231 0 0 0 0 1 1 1
我尝试过使用 count vectorizer()
代码:
cv = CountVectorizer()
movies = cv.fit_transform(cust['movies_list'])
cols = cv.vocabulary_
movies_ = pd.DataFrame(movies.toarray(), columns = cols, index =
cust['customer_id'])
movies_
输出:
customer ID 111 222 333 444 555 666 777
212121212 1 1 2 2 0 0 0
121212121 0 1 0 0 2 1 0
121323231 0 0 0 0 1 3 1
客户 ID 非常匹配,我计算了他观看电影的次数。
【问题讨论】:
-
欢迎来到 StackOverflow!此示例包含不运行的代码。请阅读how to ask a question(尤其是how to create a good example)以获得良好的响应。
-
看起来你必须从字典值创建一个稀疏矩阵。我说的对吗?
标签: python pandas feature-engineering