Udacity数据分析(入门)-TMDb电影数据集探索
TMDb電影數(shù)據(jù)集探索
- 項(xiàng)目概述
- 數(shù)據(jù)集
- 項(xiàng)目重點(diǎn)
- 需要探索的問題
- 目的
- 項(xiàng)目流程
- 第一步:選擇數(shù)據(jù)集
- 第二步:組織文件
- 第三步:數(shù)據(jù)整理
- 常規(guī)屬性
- 數(shù)據(jù)清理
- 第四步:探索性數(shù)據(jù)分析
- 研究問題 1 :哪種體裁/類型(genres)的平均預(yù)算(budget)最高?
- 研究問題 2 :最受歡迎(popularity)前五的體裁/類型(genres),每年的受歡迎程度是如何變化的?
- 研究問題 3 :哪種電影類型的評分(vote_average)最高;評分和受歡迎程度(popularity)有何關(guān)系?
- 結(jié)論
- 數(shù)據(jù)集來源
項(xiàng)目概述
在本項(xiàng)目中,將分析一個TMDb(The Movie Database:美國電影數(shù)據(jù)集),然后傳達(dá)我的發(fā)現(xiàn)。將使用 Python 庫 NumPy、Pandas 和 Matplotlib 來使幫助我進(jìn)行分析。
數(shù)據(jù)集
本數(shù)據(jù)集中包含 10,000 條電影信息,信息來源為“電影數(shù)據(jù)庫”(TMDb,The Movie Database),包括用戶評分和票房。
“演職人員 (cast)”、“電影類別 (genres)”等數(shù)據(jù)列包含由豎線字符(|)分隔的多個數(shù)值。“演職人員 (cast) ”列中有一些奇怪的字符。以“_adj”結(jié)尾的最后兩列表示了考慮了通貨膨脹之后的相關(guān)電影的預(yù)算和收入(以2010年美元的價值來計(jì)算)。
項(xiàng)目重點(diǎn)
需要探索的問題
目的
經(jīng)過對數(shù)據(jù)集的探索:
- 了解如何調(diào)查數(shù)據(jù)集中的問題,以及將數(shù)據(jù)整理成可以使用的格式
- 并練習(xí)傳達(dá)我的分析結(jié)果。
- 能夠在 NumPy 和 Pandas 中使用向量化運(yùn)算,以加快數(shù)據(jù)分析代碼的運(yùn)行速度
- 熟悉 Pandas 的 Series 和 DataFrame 對象,可以使訪問數(shù)據(jù)更方便
- 了解如何使用 Matplotlib 生成圖形,展示你的發(fā)現(xiàn)
項(xiàng)目流程
第一步:選擇數(shù)據(jù)集
本次選取探索的數(shù)據(jù)集為“TMDb電影數(shù)據(jù)庫”,數(shù)據(jù)集中包含來自1960-2016年上映的近11,000部電影的基本信息,主要包括了電影類型、預(yù)算、票房、演職人員、時長、評分等信息。
第二步:組織文件
- 用來傳達(dá)發(fā)現(xiàn)的報告文檔
- 用來分析數(shù)據(jù)的Python 代碼()
- 數(shù)據(jù)集(tmdb-movies.csv)
第三步:數(shù)據(jù)整理
#加載要使用的庫 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns%matplotlib inline常規(guī)屬性
#導(dǎo)入數(shù)據(jù) df_tmdb = pd.read_csv('tmdb-movies.csv') #查看基本信息 df_tmdb.info()查看后發(fā)現(xiàn),共包含10866組,每組數(shù)據(jù)包含21種數(shù)據(jù)。
#了解數(shù)據(jù)的基本形態(tài) df_tmdb.head(3)| 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Action|Adventure|Science Fiction|Thriller | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 |
| 76341 | tt1392190 | 28.419936 | 150000000 | 378436354 | Mad Max: Fury Road | Tom Hardy|Charlize Theron|Hugh Keays-Byrne|Nic... | http://www.madmaxmovie.com/ | George Miller | What a Lovely Day. | ... | An apocalyptic story set in the furthest reach... | 120 | Action|Adventure|Science Fiction|Thriller | Village Roadshow Pictures|Kennedy Miller Produ... | 5/13/15 | 6185 | 7.1 | 2015 | 1.379999e+08 | 3.481613e+08 |
| 262500 | tt2908446 | 13.112507 | 110000000 | 295238201 | Insurgent | Shailene Woodley|Theo James|Kate Winslet|Ansel... | http://www.thedivergentseries.movie/#insurgent | Robert Schwentke | One Choice Can Destroy You | ... | Beatrice Prior must confront her inner demons ... | 119 | Adventure|Science Fiction|Thriller | Summit Entertainment|Mandeville Films|Red Wago... | 3/18/15 | 2480 | 6.3 | 2015 | 1.012000e+08 | 2.716190e+08 |
3 rows × 21 columns
#查看重復(fù)數(shù)據(jù) df_tmdb.duplicated().sum()顯示有1條重復(fù)數(shù)據(jù)。
#清除重復(fù)數(shù)據(jù)并檢查 df_tmdb.drop_duplicates(inplace=True) df_tmdb.duplicated().sum() 0 #查看空值數(shù)據(jù) df_tmdb.isnull().sum() id 0 imdb_id 10 popularity 0 budget 0 revenue 0 original_title 0 cast 76 homepage 7929 director 44 tagline 2824 keywords 1493 overview 4 runtime 0 genres 23 production_companies 1030 release_date 0 vote_count 0 vote_average 0 release_year 0 budget_adj 0 revenue_adj 0 dtype: int64 #了解各項(xiàng)目的數(shù)據(jù)分布 df_tmdb.describe()| 10865.000000 | 10865.000000 | 1.086500e+04 | 1.086500e+04 | 10865.000000 | 10865.000000 | 10865.000000 | 10865.000000 | 1.086500e+04 | 1.086500e+04 |
| 66066.374413 | 0.646446 | 1.462429e+07 | 3.982690e+07 | 102.071790 | 217.399632 | 5.975012 | 2001.321859 | 1.754989e+07 | 5.136900e+07 |
| 92134.091971 | 1.000231 | 3.091428e+07 | 1.170083e+08 | 31.382701 | 575.644627 | 0.935138 | 12.813260 | 3.430753e+07 | 1.446383e+08 |
| 5.000000 | 0.000065 | 0.000000e+00 | 0.000000e+00 | 0.000000 | 10.000000 | 1.500000 | 1960.000000 | 0.000000e+00 | 0.000000e+00 |
| 10596.000000 | 0.207575 | 0.000000e+00 | 0.000000e+00 | 90.000000 | 17.000000 | 5.400000 | 1995.000000 | 0.000000e+00 | 0.000000e+00 |
| 20662.000000 | 0.383831 | 0.000000e+00 | 0.000000e+00 | 99.000000 | 38.000000 | 6.000000 | 2006.000000 | 0.000000e+00 | 0.000000e+00 |
| 75612.000000 | 0.713857 | 1.500000e+07 | 2.400000e+07 | 111.000000 | 146.000000 | 6.600000 | 2011.000000 | 2.085325e+07 | 3.370173e+07 |
| 417859.000000 | 32.985763 | 4.250000e+08 | 2.781506e+09 | 900.000000 | 9767.000000 | 9.200000 | 2015.000000 | 4.250000e+08 | 2.827124e+09 |
1.涉及空值的數(shù)據(jù)列為imdb_id,cast,homepage,director,tagline,keywords,overview,genres,production_companies;對于已提出的問題,僅涉及genres含有23個空值(為自變量),需要消除空值行。
2.對于"budget"列,存在“零值”,為了消除影響需將所有零值數(shù)據(jù)行去除
數(shù)據(jù)清理
#填充'genres'中的空值數(shù)據(jù)為“NaN” df_tmdb['genres'] = df_tmdb['genres'].fillna("NaN")#獲取被填充的空值的索引 df_index = df_tmdb[df_tmdb.genres == 'NaN'].index.tolist()#刪除對應(yīng)索引的行并檢查 df_tmdb = df_tmdb.drop(df_index) df_tmdb.shape (10842, 21)由10866組刪除后變?yōu)?0842組;刪除的1組為重復(fù)數(shù)據(jù),23組為’genres’的空值數(shù)據(jù)。
#根據(jù)'genres'中的不同體裁類型單列成行,生成新的dataframe df_tmdb = df_tmdb.drop('genres', axis = 1).join(df_tmdb['genres'].str.split('|', expand = True).stack().reset_index(level = 1, drop = True).rename('genres')) df_tmdb.head()| 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 | Action |
| 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 | Adventure |
| 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 | Science Fiction |
| 135397 | tt0369610 | 32.985763 | 150000000 | 1513528810 | Jurassic World | Chris Pratt|Bryce Dallas Howard|Irrfan Khan|Vi... | http://www.jurassicworld.com/ | Colin Trevorrow | The park is open. | ... | Twenty-two years after the events of Jurassic ... | 124 | Universal Studios|Amblin Entertainment|Legenda... | 6/9/15 | 5562 | 6.5 | 2015 | 1.379999e+08 | 1.392446e+09 | Thriller |
| 76341 | tt1392190 | 28.419936 | 150000000 | 378436354 | Mad Max: Fury Road | Tom Hardy|Charlize Theron|Hugh Keays-Byrne|Nic... | http://www.madmaxmovie.com/ | George Miller | What a Lovely Day. | ... | An apocalyptic story set in the furthest reach... | 120 | Village Roadshow Pictures|Kennedy Miller Produ... | 5/13/15 | 6185 | 7.1 | 2015 | 1.379999e+08 | 3.481613e+08 | Action |
5 rows × 21 columns
#去除“budget_adj”的零值數(shù)據(jù) df_tmdb = df_tmdb[df_tmdb['budget_adj'] > 0] df_tmdb = df_tmdb[df_tmdb['popularity'] > 0]"popularity"實(shí)際不存在零值數(shù)據(jù)。
df_tmdb['genres'].value_counts() Drama 2316 Comedy 1740 Thriller 1641 Action 1428 Adventure 906 Romance 861 Crime 823 Horror 765 Science Fiction 701 Family 523 Fantasy 508 Mystery 440 Animation 260 History 183 Music 169 War 155 Western 74 Documentary 64 Foreign 35 TV Movie 9 Name: genres, dtype: int64第四步:探索性數(shù)據(jù)分析
研究問題 1 :哪種體裁/類型(genres)的平均預(yù)算(budget)最高?
要計(jì)算不同"genres"電影的"budget"均值,并經(jīng)過排序得出最高的類型;并且為了消除通脹影響,計(jì)算"budget_adj"的數(shù)據(jù)。
# 以“genres”分組,計(jì)算各種體裁對應(yīng)的budget平均值,按降序排列 budget_mean = df_tmdb.groupby('genres')['budget_adj'].mean().sort_values(ascending=False) budget_mean genres Adventure 7.133755e+07 Animation 6.800557e+07 Fantasy 6.749065e+07 Family 6.337153e+07 Action 5.502584e+07 Western 5.462267e+07 Science Fiction 5.176227e+07 War 5.039431e+07 History 4.847202e+07 Thriller 3.663947e+07 Mystery 3.586516e+07 Crime 3.542695e+07 Comedy 3.470445e+07 Music 3.135773e+07 Romance 3.113657e+07 Drama 3.052799e+07 Horror 1.661574e+07 Foreign 1.277944e+07 TV Movie 5.492844e+06 Documentary 5.063684e+06 Name: budget_adj, dtype: float64 #根據(jù)計(jì)算出的budget平均值,做bar圖比較 budget_mean.plot(kind = 'bar',color = 'grey')plt.ylabel('Budget')plt.title('Budget of different kinds of Genres');結(jié)論1:根據(jù)降序排列畫出的圖,排第一(平均預(yù)算最高的)的為“Adventure”類型。
研究問題 2 :最受歡迎(popularity)前五的體裁/類型(genres),每年的受歡迎程度是如何變化的?
計(jì)算不同"genres"電影對應(yīng)的"popularity"均值,降序得到前五位的類型;并作出各類型電影,“popularity"隨"release_year”(上映時間)變化的圖像。
#計(jì)算不同體裁的影片,受歡迎程度的總平均值,降序排列 popularity_mean = df_tmdb.groupby(['genres'])['popularity'].mean().sort_values(ascending=False) #選取最受歡迎前五的體裁(獲取對應(yīng)的索引表) most_popularity = popularity_mean[:5].index.tolist() most_popularity ['Adventure', 'Science Fiction', 'Fantasy', 'Animation', 'Action']結(jié)論2-1:最受歡迎(popularity)前五的電影類型是Adventure,Science Fiction,Fantasy,Action,Animation。
#選取前五的體裁,生成包含體裁(genres)、上映時間(release_year)、受歡迎度(popularity)的數(shù)據(jù)表 df_popularity = df_tmdb[['genres', 'release_year', 'popularity']].set_index('genres').loc[most_popularity].reset_index('genres') #建立一個五種體裁的,根據(jù)年份的受歡迎程度表 df_popularity_year = pd.pivot_table(df_popularity, values='popularity', index='release_year', columns='genres') #作圖,橫軸為年份,縱軸為受歡迎程度 df_popularity_year.plot(kind='line',subplots=True, sharex=True, sharey=True, figsize=(20,20));結(jié)論2-2:1960-2016期間最受歡迎的前五類電影類型中,除"Animation"外均在2010年后有了進(jìn)一步提升。
研究問題 3 :哪種電影類型的評分(vote_average)最高;評分和受歡迎程度(popularity)有何關(guān)系?
計(jì)算"vote_average"的平均值并選出最高的類型(“genres”);根據(jù)作出的"vote_average"(評分)和"popularity"(受歡迎程度)散點(diǎn)圖了解相關(guān)關(guān)系。
# 得到vote_average均值最大的體裁類型 df_tmdb.groupby('genres')['vote_average'].mean().sort_values(ascending=False).index[0] 'Documentary'結(jié)論3-1:Documentary體裁的平均評分最高。
#得到不同體裁的評分、受歡迎程度表 df_vote = df_tmdb[['genres', 'vote_average', 'popularity']] #根據(jù)不同體裁,做“評分”與對應(yīng)“受歡迎度”的散點(diǎn)圖 df_vote_scatter = sns.FacetGrid(df_vote, col='genres', col_wrap=4, hue='vote_average') df_vote_scatter.map(plt.scatter, 'vote_average', 'popularity', alpha=.7) df_vote_scatter.add_legend();結(jié)論3-2:除去Western、Documentary、TV Movie、Foreign數(shù)據(jù)較少;可以發(fā)現(xiàn)隨著平均評分由低到高,均存在較低的受歡迎度(popularity),而隨著受歡迎度增加,對應(yīng)的平均評分也相應(yīng)提高。
結(jié)論
1.以根據(jù)通脹后的預(yù)算(budget)數(shù)據(jù)來比較,"Adventure"類型的電影1960-2016期間的平均預(yù)算最高。
其中去除了可能產(chǎn)生影響的"budget_adj"的零值數(shù)據(jù)(異常值),因?yàn)閷?shí)際預(yù)算不可能為零因此此類零值數(shù)據(jù)不應(yīng)作為分析要素使用,當(dāng)然經(jīng)過驗(yàn)證:去除前后得到的上述結(jié)論相同。
2.1960-2016最受歡迎(popularity)前五的電影類型是Adventure,Science Fiction,Fantasy,Action,Animation;其中Adventure,Science Fiction,Action在2010年后受歡迎度有明顯提升。
有3種電影類型,由于對"budget_adj"異常值的處理 以及 本身的數(shù)據(jù)缺失,導(dǎo)致1985年以前的數(shù)據(jù)不連貫,因此5種最受歡迎類型的直接比較僅適用于1985年之后的變化。
3.1960-2016的數(shù)據(jù)中Documentary類型的平均評分最高;更高的受歡迎度,伴隨更高的評分,但高評分不代表受歡迎。
不同類型下的"popularity"均存在數(shù)值較大的數(shù)據(jù),但不了解實(shí)際打分依據(jù)就無法真正判定是否為異常值,因此未做處理;另外根據(jù)Western、Documentary、TV Movie、Foreign四種類型,無法得出上述結(jié)論,可能由于數(shù)據(jù)量有限的緣故。
數(shù)據(jù)集來源
TMDb電影數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的Udacity数据分析(入门)-TMDb电影数据集探索的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FGSM论文阅读笔记
- 下一篇: 队列(纯C语言实现)