机器学习基础:案例研究——week 5

#导入数据
import graphlab
song_data = graphlab.SFrame("song_data.gl/")
#查看数据结构
song_data.head()

数据结构如下:

Paste_Image.png

数据由user_id,song_id,listen_count,title,artist,song这几列构成。

  1. Which of the artists below have had the most unique users listening to their songs?('Kanye West,'Foo Fighters,Taylor Swift,Lady GaGa)
print song_data[song_data['artist'] == 'Kanye West']

将artist为Kanye West的数据全部选定,得到如下数据:

Paste_Image.png

然后对用户(user_id)进行统计,这里使用unique()函数,其可以输出其中不重复的用户名

print song_data[song_data['artist'] == 'Kanye West']['user_id'].unique()

这样就将所有用户统计了出来,输入结果如下:

Paste_Image.png
len(song_data[song_data['artist'] == 'Kanye West']['user_id'].unique())

输出结果:2522
对剩下的三人进行重复的操作

len(song_data[song_data['artist'] == 'Foo Fighters']['user_id'].unique())
len(song_data[song_data["artist"] == "Taylor Swift"]["user_id"].unique())
len(song_data[song_data["artist"] == "Lady GaGa"]["user_id"].unique())

输出结果:2055,3246,2928

2 . Which of the artists below is the most popular artist, the one with highest total listen_count, in the data set?

3 .
Which of the artists below is the least popular artist, the one with smallest total listen_count, in the data set?
这里要用到groupby(key_columns, operations, *args)
其可以将关键列按给出的列聚合。
i. key_columns , which takes the column we want to group, in our case, 'artist'
ii. operations , where we define the aggregation operation we using, in our case, we want to sum over the 'listen_count'.

data = song_data.groupby(key_columns='artist', operations={'total_count': graphlab.aggregate.SUM('listen_count')}).sort('total_count', ascending=False)
print data[0]
print data[-1]

输出结果如下:
{'total_count': 43218, 'artist': 'Kings Of Leon'}
{'total_count': 14, 'artist': 'William Tabbert'}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,916评论 0 23
  • 星城,叶落秋凉。宁静的周末,总是被时不时的鞭炮声惊扰,淡淡的思绪,渲染了飘零的梧桐,铺落在远去的街头。一秋又...
    那些年聆听的阅读 212评论 0 0
  • 情人节过去很久了,想起玫瑰。其实节日里我并未收到玫瑰,仅获几本书而已。翻书偶见莎翁十四行诗曰:对天生的尤物我们要求...
    妙云花花阅读 583评论 0 2