数据科学包——Day2

  • 利用Pandas, Numpy进行电影数据分析
    • 准备工作
    • 任务:
    • 数据读取
    • 数据合并
    • 按性别查看各个电影的平均评分
    • 男女评分差异最大的电影
    • 活跃电影排行
    • 前十大活跃电影——被评价次数超过1000的电影top10
  • 前二十大高分电影——评分分最高的电影
    • 前十大活跃电影平均分——不一定越活跃的电影评分越高
    • 前二十大高分电影的活跃程度——不一定越高分的电影越活跃,可能评价的人少但是均分很高
    • 十大好电影——活跃度高,均分也高
    • 综合均分与活跃度

利用Pandas, Numpy进行电影数据分析

准备工作

从网站grouplens.org/datasets/movielens 下载 MovieLens 1M Dataset 数据及数据说明书

任务:

  1. 数据读取
  2. 数据合并
  3. 统计电影平均得分
  4. 统计活跃电影——被评价的次数越多说明越活跃
  5. 女生最喜欢的电影
  6. 男生最喜欢的电影
  7. 男女生评分差异最大的电影——某类电影女生喜欢而男生不喜欢
  8. 最具争议的电影排行榜——评分的方差最大
import pandas as pd
import numpy as np

数据读取

'''
数据读取
'''
user_names=['user_id','gender','age','occupation','zip']
users=pd.read_table('ml-1m/users.dat', sep='::', header=None, names=user_names, engine='python')rating_names = ['user_id', 'movie_id', 'rating', 'timestamp']
ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=None, names=rating_names, engine='python')movie_names = ['movie_id', 'title', 'genres']
movies = pd.read_table('ml-1m/movies.dat', sep='::', header=None, names=movie_names, engine='python')
print(len(users)
users.head()'''
6040user_id gender  age occupation    zip
0       1        F    1         10  48067
1       2        M   56         16  70072
2       3        M   25         15  55117
3       4        M   45          7  02460
4       5        M   25         20  55455
'''
print(len(ratings)
ratings.head()'''
1000209user_id   movie_id   rating  timestamp
0       1        1193        5  978300760
1       1         661        3  978302109
2       1         914        3  978301968
3       1        3408        4  978300275
4       1        2355        5  978824291
'''
print(len(movies)
movies.head()'''
3883movie_id                                title                         genres
0        1                    Toy Story (1995)   Animation|Children's|Comedy
1        2                      Jumanji (1995)   Adventure|Children's|Fantasy
2        3             Grumpier Old Men (1995)                 Comedy|Romance
3        4            Waiting to Exhale (1995)                   Comedy|Drama
4        5  Father of the Bride Part II (1995)                         Comedy
'''

数据合并

data=pd.merge(pd.merge(users,ratings),movies)
data.head(5)'''user_id   gender  age occupation    zip   movie_id    rating  timestamp                title                     genres
0       1        F    1         10  48067       1193         5  978300760   One Flew Over the Cuckoo's Nest (1975) Drama
1       2        M   56         16  70072       1193         5  978298413   One Flew Over the Cuckoo's Nest (1975) Drama
2      12        M   25         12  32793       1193         4  978220179   One Flew Over the Cuckoo's Nest (1975) Drama
3      15        M   25          7  22903       1193         4  978199279   One Flew Over the Cuckoo's Nest (1975) Drama
4      17        M   50          1  95350       1193         5  978158471   One Flew Over the Cuckoo's Nest (1975) Drama
'''

按性别查看各个电影的平均评分

mean_ratings_gender=data.pivot_table(values='rating',index='title',columns='gender',aggfunc='mean'

男女评分差异最大的电影

mean_ratings_gender['diff']=mean_ratings_gender.F-mean_ratings_gender.M
mean_ratings_gender.head(5)'''gender        F           M         difftitle          $1,000,000 Duck (1971)  3.375000    2.761905     0.613095'Night Mother (1986)  3.388889    3.352941     0.035948'Til There Was You (1997) 2.675676    2.733333    -0.057658'burbs, The (1989)    2.793478    2.962085    -0.168607
...And Justice for All (1979)   3.828571    3.689024     0.139547
'''
'''
按差值排序
'''
mean_ratings_gender.sort_values(by='diff',ascending=True)

活跃电影排行

ratings_by_movie_title=data.groupby('title').size()
ratings_by_movie_title.head(5)
'''
title
$1,000,000 Duck (1971)            37
'Night Mother (1986)              70
'Til There Was You (1997)         52
'burbs, The (1989)               303
...And Justice for All (1979)    199
dtype: int64
'''

前十大活跃电影——被评价次数超过1000的电影top10

top_ratings=ratings_by_movie_title[ratings_by_movie_title>1000]
top_10_ratings=top_ratings.sort_values(ascending=False).head(10)'''
title
American Beauty (1999)                                   3428
Star Wars: Episode IV - A New Hope (1977)                2991
Star Wars: Episode V - The Empire Strikes Back (1980)    2990
Star Wars: Episode VI - Return of the Jedi (1983)        2883
Jurassic Park (1993)                                     2672
Saving Private Ryan (1998)                               2653
Terminator 2: Judgment Day (1991)                        2649
Matrix, The (1999)                                       2590
Back to the Future (1985)                                2583
Silence of the Lambs, The (1991)                         2578
dtype: int64
'''

前二十大高分电影——评分分最高的电影

mean_ratings=data.pivot_table(values='ratings',index='title',aggfunc='mean')
top_20_mean_ratings=mean_ratings.sort_values(ascending=False).head(20)'''
title
Gate of Heavenly Peace, The (1995)                                     5.000000
Lured (1947)                                                           5.000000
Ulysses (Ulisse) (1954)                                                5.000000
Smashing Time (1967)                                                   5.000000
Follow the Bitch (1998)                                                5.000000
Song of Freedom (1936)                                                 5.000000
Bittersweet Motel (2000)                                               5.000000
Baby, The (1973)                                                       5.000000
One Little Indian (1973)                                               5.000000
Schlafes Bruder (Brother of Sleep) (1995)                              5.000000
I Am Cuba (Soy Cuba/Ya Kuba) (1964)                                    4.800000
Lamerica (1994)                                                        4.750000
Apple, The (Sib) (1998)                                                4.666667
Sanjuro (1962)                                                         4.608696
Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)    4.560510
Shawshank Redemption, The (1994)                                       4.554558
Godfather, The (1972)                                                  4.524966
Close Shave, A (1995)                                                  4.520548
Usual Suspects, The (1995)                                             4.517106
Schindler's List (1993)                                                4.510417
Name: rating, dtype: float64
'''

前十大活跃电影平均分——不一定越活跃的电影评分越高

mean_ratings[top_10_ratings.index]
'''
title
American Beauty (1999)                                   4.317386
Star Wars: Episode IV - A New Hope (1977)                4.453694
Star Wars: Episode V - The Empire Strikes Back (1980)    4.292977
Star Wars: Episode VI - Return of the Jedi (1983)        4.022893
Jurassic Park (1993)                                     3.763847
Saving Private Ryan (1998)                               4.337354
Terminator 2: Judgment Day (1991)                        4.058513
Matrix, The (1999)                                       4.315830
Back to the Future (1985)                                3.990321
Silence of the Lambs, The (1991)                         4.351823
Name: rating, dtype: float64
'''

前二十大高分电影的活跃程度——不一定越高分的电影越活跃,可能评价的人少但是均分很高

ratings_by_movie_title[top_20_mean_ratings.index]'''
title
Gate of Heavenly Peace, The (1995)                                        3
Lured (1947)                                                              1
Ulysses (Ulisse) (1954)                                                   1
Smashing Time (1967)                                                      2
Follow the Bitch (1998)                                                   1
Song of Freedom (1936)                                                    1
Bittersweet Motel (2000)                                                  1
Baby, The (1973)                                                          1
One Little Indian (1973)                                                  1
Schlafes Bruder (Brother of Sleep) (1995)                                 1
I Am Cuba (Soy Cuba/Ya Kuba) (1964)                                       5
Lamerica (1994)                                                           8
Apple, The (Sib) (1998)                                                   9
Sanjuro (1962)                                                           69
Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)     628
Shawshank Redemption, The (1994)                                       2227
Godfather, The (1972)                                                  2223
Close Shave, A (1995)                                                   657
Usual Suspects, The (1995)                                             1783
Schindler's List (1993)                                                2304
dtype: int64
'''

十大好电影——活跃度高,均分也高

top_10_movies=mean_ratings[top_ratings.index].sort_values(ascending=False).head(10)'''
title
Shawshank Redemption, The (1994)                                               4.554558
Godfather, The (1972)                                                          4.524966
Usual Suspects, The (1995)                                                     4.517106
Schindler's List (1993)                                                        4.510417
Raiders of the Lost Ark (1981)                                                 4.477725
Rear Window (1954)                                                             4.476190
Star Wars: Episode IV - A New Hope (1977)                                      4.453694
Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1963)    4.449890
Casablanca (1942)                                                              4.412822
Sixth Sense, The (1999)                                                        4.406263
Name: rating, dtype: float64
'''

综合均分与活跃度

df_top_10_movies=pd.DataFrame(top_10_movies)
df_top_10_movies['hot']=top_ratings[top_10_movies.index]'''rating  hottitle       Shawshank Redemption, The (1994)    4.554558    2227Godfather, The (1972)   4.524966    2223Usual Suspects, The (1995)  4.517106    1783Schindler's List (1993)    4.510417    2304Raiders of the Lost Ark (1981)  4.477725    2514Rear Window (1954)  4.476190    1050Star Wars: Episode IV - A New Hope (1977)   4.453694    2991
Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1963) 4.449890    1367Casablanca (1942)   4.412822    1669Sixth Sense, The (1999) 4.406263    2459
'''

完结!

数据科学包——Day2相关推荐

  1. python 数据科学 包_什么时候应该使用哪个Python数据科学软件包?

    python 数据科学 包 Python is the most popular language for data science. Unfortunately, it can be tricky ...

  2. python第二阶段(2)入门-数据科学包 pandas

    数据科学包 pandas 导入pandas 创建对象 1 系列 2 日期序列(1) 3 日期序列(2) 4 Series的操作(1) 5 Series的操作(2) 合并,新增,连接和比较 1 连接 2 ...

  3. 机器学习---数据科学包-第2天

    1 pandas快速入门(一) .Series()方法.Series类型由一组数据及与之相关的数据索引组成. import pandas as pd import numpy as np s = pd ...

  4. 3.机器学习—数据科学包3.2pandas基础

    pandas基础 一.pandas介绍 1.什么是pandas 2.pandas用途 3.课程内容 二.Ipython开发环境搭建 1.安装 2.新建运行环境 3.Ipython技巧 4.Ipytho ...

  5. 看看这些鲜为人知的宝藏Python数据科学包吧!

    动态数据科学的这三剑客几乎无人不知无人不晓:Numpy,Pandas和Matplotlib.你可能已经熟悉这些包以及它们的运作方式. 还有其他很炫酷的包,你肯定也想试一试,例如Plotly,Seabo ...

  6. python数据科学包第三天(索引、分组计算、数据聚合、分组运算和转换、载入数据、日期范围、数据可视化)

    索引 行索引 列索引 索引的分类 重复索引的处理 s = pd.Series(np.random.rand(5), index=list('abcde')) s a 0.566924 b 0.6034 ...

  7. python中画出距平垂线_3.机器学习—数据科学包3.3pandas操作

    pandas操作 一.pandas索引 1.Series索引index 2.DateFrame行索引index和列索引columns 3.pandas预置索引的类 4.重复索引 4.1重复索引定义 4 ...

  8. Python数据科学包(六)-----数据可视化和例子

    文章目录 一. 数据可视化 1. 线型图 2. 柱状图 3. 直方图 4. 密度图 5. 散布图 6. 饼图 7. 高级绘图 二. 股票数据分析 1. 分析波动幅度 2. 增长曲线 3. 增长倍数 4 ...

  9. 机器学习数据科学包(二)——Pandas入门

    目录 二.查看数据 三.选择 四.缺失值处理 五.相关操作 六.合并 七.分组 八.重塑(Reshaping) 九.时间序列 十.Categorical 十一.画图 十二.导入和保存数据 本文对十分钟 ...

最新文章

  1. Android安卓开发中图片缩放讲解
  2. 读了那么多CV论文,竟然连车道线分割都实现不了!
  3. windows7下载python教程-Windows 7下Python Web环境搭建图文教程
  4. 大牛的距离(笑cry)精简算法
  5. 有什么事情是你当了程序员之后才知道的?
  6. DataX在数据迁移中的应用
  7. .net core image怎么保存_C# 将PDF转为多种Image图像文件格式(Png/Bmp/Emf/Tiff)
  8. NumPy中如何确定两个ndarray数组完全相同
  9. Problem E: 深入浅出学算法019-求n的阶乘
  10. 数据预处理中的缺失值问题
  11. c# JSON转变量实例
  12. 联想蓝牙没有连接的地方计算机,联想笔记本连不上蓝牙怎么办 笔记本连接蓝牙耳机没声音解决方法...
  13. 提高代码质量——使用Jest和Sinon给已有的代码添加单元测试
  14. c#获取百度服务器时间
  15. 怎么让联想计算机升级,联想电脑怎么升级win11?联想电脑升级win11的几种方法...
  16. android各版本api区别,Android各个版本API的区别
  17. Virginia(维吉尼亚)无密钥解密
  18. 【超详细】多元线性回归模型statsmodels_ols
  19. 五个经典漏斗模型,看漏斗思维穿透流程化的本质
  20. 关键帧提取——RCC提取关键帧(1)

热门文章

  1. Hive(2):Apache Hive 安装部署
  2. Java并发编程知识大汇总
  3. Android源码定制(5)——root指纹定制与抹除
  4. Symmetrix GK盘介绍
  5. mysql报错:Column count doesn‘t match value count at row 1
  6. 关于H5的一些杂思细想(一)
  7. 重庆春季高考计算机试题,2017年重庆春季高考数学模拟练习题一
  8. 用轻量服务器搭建在线协作绘图白板
  9. 【机器人学】基于PoE模型的串联机械臂UR5的正运动学、微分运动学和逆运动学
  10. 步进电机替换伺服电机如何计算?