您的位置:首页 > 编程语言 > Python开发

利用python进入数据分析之MovieLens_1M数据分析

2017-10-14 09:43 513 查看


数据设置

In [26]:

import pandas as pd
import os
encoding = 'latin1'

upath = os.path.expanduser('ch02/movielens/users.dat')
rpath = os.path.expanduser('ch02/movielens/ratings.dat')
mpath = os.path.expanduser('ch02/movielens/movies.dat')

unames = ['user_id', 'gender', 'age', 'occupation', 'zip']
rnames = ['user_id', 'movie_id', 'rating', 'timestamp']
mnames = ['movie_id', 'title', 'genres']

users = pd.read_csv(upath, sep='::', header=None, names=unames, encoding=encoding)
ratings = pd.read_csv(rpath, sep='::', header=None, names=rnames, encoding=encoding)
movies = pd.read_csv(mpath, sep='::', header=None, names=mnames, encoding=encoding)


In [6]:

users[:5]


Out[6]:

user_idgenderageoccupationzip
01F11048067
12M561670072
23M251555117
34M45702460
45M252055455
In [7]:

ratings[:5]


Out[7]:

user_idmovie_idratingtimestamp
0111935978300760
116613978302109
219143978301968
3134084978300275
4123555978824291
In [8]:

movies[:5]


Out[8]:

movie_idtitlegenres
01Toy Story (1995)Animation|Children's|Comedy
12Jumanji (1995)Adventure|Children's|Fantasy
23Grumpier Old Men (1995)Comedy|Romance
34Waiting to Exhale (1995)Comedy|Drama
45Father of the Bride Part II (1995)Comedy
In [9]:

ratings


Out[9]:

user_idmovie_idratingtimestamp
0111935978300760
116613978302109
219143978301968
3134084978300275
4123555978824291
5111973978302268
6112875978302039
7128045978300719
815944978302268
919194978301368
1015955978824268
1119384978301752
12123984978302281
13129184978302124
14110355978301753
15127914978302188
16126873978824268
17120184978301777
18131055978301713
19127974978302039
20123213978302205
2117203978300760
22112705978300055
2315275978824195
24123403978300103
251485978824351
26110974978301953
27117214978300055
28115454978824139
2917453978824268
...............
1000179604027624956704584
1000180604010363956715455
100018160405084956704972
1000182604010414957717678
1000183604037354960971654
1000184604027914956715569
1000185604027941956716438
100018660405275956704219
1000187604020031956716294
100018860405354964828734
1000189604020105957716795
1000190604020114956716113
1000191604037514964828782
1000192604020195956703977
100019360405414956715288
1000194604010775964828799
1000195604010792956715648
100019660405494956704746
1000197604020203956715288
1000198604020213956716374
1000199604020225956716207
1000200604020285956704519
1000201604010804957717322
1000202604010894956704996
1000203604010903956715518
1000204604010911956716541
1000205604010945956704887
100020660405625956704746
1000207604010964956715648
1000208604010974956715569
1000209 rows × 4 columns


数据合并

In [10]:

data = pd.merge(pd.merge(ratings, users), movies)
data


Out[10]:

user_idmovie_idratingtimestampgenderageoccupationziptitlegenres
0111935978300760F11048067One Flew Over the Cuckoo's Nest (1975)Drama
1211935978298413M561670072One Flew Over the Cuckoo's Nest (1975)Drama
21211934978220179M251232793One Flew Over the Cuckoo's Nest (1975)Drama
31511934978199279M25722903One Flew Over the Cuckoo's Nest (1975)Drama
41711935978158471M50195350One Flew Over the Cuckoo's Nest (1975)Drama
51811934978156168F18395825One Flew Over the Cuckoo's Nest (1975)Drama
61911935982730936M11048073One Flew Over the Cuckoo's Nest (1975)Drama
72411935978136709F25710023One Flew Over the Cuckoo's Nest (1975)Drama
82811933978125194F25114607One Flew Over the Cuckoo's Nest (1975)Drama
93311935978557765M45355421One Flew Over the Cuckoo's Nest (1975)Drama
103911935978043535M18461820One Flew Over the Cuckoo's Nest (1975)Drama
114211933978038981M25824502One Flew Over the Cuckoo's Nest (1975)Drama
124411934978018995M451798052One Flew Over the Cuckoo's Nest (1975)Drama
134711934977978345M18494305One Flew Over the Cuckoo's Nest (1975)Drama
144811934977975061M25492107One Flew Over the Cuckoo's Nest (1975)Drama
154911934978813972M181277084One Flew Over the Cuckoo's Nest (1975)Drama
165311935977946400M25096931One Flew Over the Cuckoo's Nest (1975)Drama
175411935977944039M50156723One Flew Over the Cuckoo's Nest (1975)Drama
185811935977933866M25230303One Flew Over the Cuckoo's Nest (1975)Drama
195911934977934292F50155413One Flew Over the Cuckoo's Nest (1975)Drama
206211934977968584F35398105One Flew Over the Cuckoo's Nest (1975)Drama
218011934977786172M56149327One Flew Over the Cuckoo's Nest (1975)Drama
228111935977785864F25060640One Flew Over the Cuckoo's Nest (1975)Drama
238811935977694161F45102476One Flew Over the Cuckoo's Nest (1975)Drama
248911935977683596F56985749One Flew Over the Cuckoo's Nest (1975)Drama
259511935977626632M45098201One Flew Over the Cuckoo's Nest (1975)Drama
269611933977621789F251678028One Flew Over the Cuckoo's Nest (1975)Drama
279911932982791053F11019390One Flew Over the Cuckoo's Nest (1975)Drama
28102119351040737607M351920871One Flew Over the Cuckoo's Nest (1975)Drama
2910411932977546620M251200926One Flew Over the Cuckoo's Nest (1975)Drama
.................................
1000179493330843962757020M251594040Home Page (1999)Documentary
10001804802221821014866656M56140601Juno and Paycock (1930)Drama
1000181481223082962932391M181425301Detroit 9000 (1973)Action|Crime
100018248746244962781918F25470808Condition Red (1995)Action|Drama|Thriller
1000183505914344962484364M451622652Stranger, The (1994)Action
1000184594714344957190428F451697215Stranger, The (1994)Action
1000185507718683962417299M25220037Truce, The (1996)Drama|War
1000186594418681957197520F181027606Truce, The (1996)Drama|War
100018751054043962337582M50718977Brother Minister: The Assassination of Malcolm...Documentary
100018851854044963402617F35444485Brother Minister: The Assassination of Malcolm...Documentary
100018955324045959619841M251727408Brother Minister: The Assassination of Malcolm...Documentary
100019055434043960127592M251797401Brother Minister: The Assassination of Malcolm...Documentary
1000191522025433961546137M25791436Six Ways to Sunday (1997)Comedy
1000192575425434958272316F18160640Six Ways to Sunday (1997)Comedy
100019352275913961475931M181064050Tough and Deadly (1995)Action|Drama|Thriller
100019457955911958145253M25192688Tough and Deadly (1995)Action|Drama|Thriller
1000195531336565960920392M56055406Lured (1947)Crime
1000196532824384960838075F25491740Outside Ozona (1998)Drama|Thriller
1000197533433233960796159F561346140Chain of Fools (2000)Comedy|Crime
100019853341271960795494F561346140Silence of the Palace, The (Saimt el Qusur) (1...Drama
1000199533433825960796159F561346140Song of Freedom (1936)Drama
1000200542018433960156505F11914850Slappy and the Stinkers (1998)Children's|Comedy
100020154332863960240881F351745014Nemesis 2: Nebula (1995)Action|Sci-Fi|Thriller
1000202549435304959816296F351794306Smoking/No Smoking (1993)Comedy
1000203555621983959445515M45692103Modulations (1998)Documentary
1000204594921985958846401M181747901Modulations (1998)Documentary
1000205567527033976029116M351430030Broken Vessels (1998)Drama
1000206578028451958153068M181792886White Boys (1999)Drama
1000207585136075957756608F182055410One Little Indian (1973)Comedy|Drama|Western
1000208593829094957273353M25135401Five Wives, Three Secretaries and Me (1998)Documentary
1000209 rows × 10 columns

In [11]:

data.ix[0]


D:\python2713\lib\anaconda_install\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix """Entry point for launching an IPython kernel.


Out[11]:

user_id                                            1
movie_id                                        1193
rating                                             5
timestamp                                  978300760
gender                                             F
age                                                1
occupation                                        10
zip                                            48067
title         One Flew Over the Cuckoo's Nest (1975)
genres                                         Drama
Name: 0, dtype: object



计算电影平均分

In [34]:

import sys
reload(sys)
sys.setdefaultencoding('latin1')
mean_ratings = data.pivot_table('rating', index='title',columns='gender', aggfunc='mean')


In [38]:

mean_ratings[:5]


Out[38]:

genderFM
title
$1,000,000 Duck (1971)3.3750002.761905
'Night Mother (1986)3.3888893.352941
'Til There Was You (1997)2.6756762.733333
'burbs, The (1989)2.7934782.962085
...And Justice for All (1979)3.8285713.689024
In [39]:

ratings_by_title = data.groupby('title').size()  #对title进行分组


In [40]:

ratings_by_title[:5]


Out[40]:

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


In [41]:

active_titles = ratings_by_title.index[ratings_by_title >= 250] # 获得评论数据大于250的电影


In [42]:

active_titles[:10]


Out[42]:

Index([u''burbs, The (1989)', u'10 Things I Hate About You (1999)',
u'101 Dalmatians (1961)', u'101 Dalmatians (1996)',
u'12 Angry Men (1957)', u'13th Warrior, The (1999)',
u'2 Days in the Valley (1996)', u'20,000 Leagues Under the Sea (1954)',
u'2001: A Space Odyssey (1968)', u'2010 (1984)'],
dtype='object', name=u'title')


In [43]:

mean_ratings = mean_ratings.ix[active_titles]
mean_ratings


Out[43]:

genderFM
title
'burbs, The (1989)2.7934782.962085
10 Things I Hate About You (1999)3.6465523.311966
101 Dalmatians (1961)3.7914443.500000
101 Dalmatians (1996)3.2400002.911215
12 Angry Men (1957)4.1843974.328421
13th Warrior, The (1999)3.1120003.168000
2 Days in the Valley (1996)3.4888893.244813
20,000 Leagues Under the Sea (1954)3.6701033.709205
2001: A Space Odyssey (1968)3.8255814.129738
2010 (1984)3.4468093.413712
28 Days (2000)3.2094242.977707
39 Steps, The (1935)3.9655174.107692
54 (1998)2.7017542.782178
7th Voyage of Sinbad, The (1958)3.4090913.658879
8MM (1999)2.9062502.850962
About Last Night... (1986)3.1886793.140909
Absent Minded Professor, The (1961)3.4693883.446809
Absolute Power (1997)3.4691363.327759
Abyss, The (1989)3.6592363.689507
Ace Ventura: Pet Detective (1994)3.0000003.197917
Ace Ventura: When Nature Calls (1995)2.2696632.543333
Addams Family Values (1993)3.0000002.878531
Addams Family, The (1991)3.1861703.163498
Adventures in Babysitting (1987)3.4557823.208122
Adventures of Buckaroo Bonzai Across the 8th Dimension, The (1984)3.3085113.402321
Adventures of Priscilla, Queen of the Desert, The (1994)3.9890713.688811
Adventures of Robin Hood, The (1938)4.1666673.918367
African Queen, The (1951)4.3242324.223822
Age of Innocence, The (1993)3.8270683.339506
Agnes of God (1985)3.5348843.244898
.........
White Men Can't Jump (1992)3.0287773.231061
Who Framed Roger Rabbit? (1988)3.5693783.713251
Who's Afraid of Virginia Woolf? (1966)4.0297034.096939
Whole Nine Yards, The (2000)3.2965523.404814
Wild Bunch, The (1969)3.6363644.128099
Wild Things (1998)3.3920003.459082
Wild Wild West (1999)2.2754492.131973
William Shakespeare's Romeo and Juliet (1996)3.5326093.318644
Willow (1988)3.6586833.453543
Willy Wonka and the Chocolate Factory (1971)4.0639533.789474
Witness (1985)4.1158543.941504
Wizard of Oz, The (1939)4.3550304.203138
Wolf (1994)3.0740742.899083
Women on the Verge of a Nervous Breakdown (1988)3.9343073.865741
Wonder Boys (2000)4.0437963.913649
Working Girl (1988)3.6067423.312500
World Is Not Enough, The (1999)3.3375003.388889
Wrong Trousers, The (1993)4.5882354.478261
Wyatt Earp (1994)3.1470593.283898
X-Files: Fight the Future, The (1998)3.4894743.493797
X-Men (2000)3.6823103.851702
Year of Living Dangerously (1982)3.9512203.869403
Yellow Submarine (1968)3.7142863.689286
You've Got Mail (1998)3.5424243.275591
Young Frankenstein (1974)4.2899634.239177
Young Guns (1988)3.3717953.425620
Young Guns II (1990)2.9347832.904025
Young Sherlock Holmes (1985)3.5147063.363344
Zero Effect (1998)3.8644073.723140
eXistenZ (1999)3.0985923.289086
1216 rows × 2 columns

In [44]:

mean_ratings = mean_ratings.rename(index={'Seven Samurai (The Magnificent Seven) (Shichinin no samurai) (1954)':
'Seven Samurai (Shichinin no samurai) (1954)'})


In [45]:

top_female_ratings = mean_ratings.sort_index(by='F', ascending=False)# 获取女性观众最喜欢的电影
top_female_ratings[:10]


Out[45]:

genderFM
title
Close Shave, A (1995)4.6444444.473795
Wrong Trousers, The (1993)4.5882354.478261
Sunset Blvd. (a.k.a. Sunset Boulevard) (1950)4.5726504.464589
Wallace & Gromit: The Best of Aardman Animation (1996)4.5631074.385075
Schindler's List (1993)4.5626024.491415
Shawshank Redemption, The (1994)4.5390754.560625
Grand Day Out, A (1992)4.5378794.293255
To Kill a Mockingbird (1962)4.5366674.372611
Creature Comforts (1990)4.5138894.272277
Usual Suspects, The (1995)4.5133174.518248
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息