Udacity数据分析(入门)-TMDb电影数据集探索
TMDb電影數據集探索
- 項目概述
- 數據集
- 項目重點
- 需要探索的問題
- 目的
- 項目流程
- 第一步:選擇數據集
- 第二步:組織文件
- 第三步:數據整理
- 常規屬性
- 數據清理
- 第四步:探索性數據分析
- 研究問題 1 :哪種體裁/類型(genres)的平均預算(budget)最高?
- 研究問題 2 :最受歡迎(popularity)前五的體裁/類型(genres),每年的受歡迎程度是如何變化的?
- 研究問題 3 :哪種電影類型的評分(vote_average)最高;評分和受歡迎程度(popularity)有何關系?
- 結論
- 數據集來源
項目概述
在本項目中,將分析一個TMDb(The Movie Database:美國電影數據集),然后傳達我的發現。將使用 Python 庫 NumPy、Pandas 和 Matplotlib 來使幫助我進行分析。
數據集
本數據集中包含 10,000 條電影信息,信息來源為“電影數據庫”(TMDb,The Movie Database),包括用戶評分和票房。
“演職人員 (cast)”、“電影類別 (genres)”等數據列包含由豎線字符(|)分隔的多個數值。“演職人員 (cast) ”列中有一些奇怪的字符。以“_adj”結尾的最后兩列表示了考慮了通貨膨脹之后的相關電影的預算和收入(以2010年美元的價值來計算)。
項目重點
需要探索的問題
目的
經過對數據集的探索:
- 了解如何調查數據集中的問題,以及將數據整理成可以使用的格式
- 并練習傳達我的分析結果。
- 能夠在 NumPy 和 Pandas 中使用向量化運算,以加快數據分析代碼的運行速度
- 熟悉 Pandas 的 Series 和 DataFrame 對象,可以使訪問數據更方便
- 了解如何使用 Matplotlib 生成圖形,展示你的發現
項目流程
第一步:選擇數據集
本次選取探索的數據集為“TMDb電影數據庫”,數據集中包含來自1960-2016年上映的近11,000部電影的基本信息,主要包括了電影類型、預算、票房、演職人員、時長、評分等信息。
第二步:組織文件
- 用來傳達發現的報告文檔
- 用來分析數據的Python 代碼()
- 數據集(tmdb-movies.csv)
第三步:數據整理
#加載要使用的庫 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns%matplotlib inline常規屬性
#導入數據 df_tmdb = pd.read_csv('tmdb-movies.csv') #查看基本信息 df_tmdb.info()查看后發現,共包含10866組,每組數據包含21種數據。
#了解數據的基本形態 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
#查看重復數據 df_tmdb.duplicated().sum()顯示有1條重復數據。
#清除重復數據并檢查 df_tmdb.drop_duplicates(inplace=True) df_tmdb.duplicated().sum() 0 #查看空值數據 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 #了解各項目的數據分布 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.涉及空值的數據列為imdb_id,cast,homepage,director,tagline,keywords,overview,genres,production_companies;對于已提出的問題,僅涉及genres含有23個空值(為自變量),需要消除空值行。
2.對于"budget"列,存在“零值”,為了消除影響需將所有零值數據行去除
數據清理
#填充'genres'中的空值數據為“NaN” df_tmdb['genres'] = df_tmdb['genres'].fillna("NaN")#獲取被填充的空值的索引 df_index = df_tmdb[df_tmdb.genres == 'NaN'].index.tolist()#刪除對應索引的行并檢查 df_tmdb = df_tmdb.drop(df_index) df_tmdb.shape (10842, 21)由10866組刪除后變為10842組;刪除的1組為重復數據,23組為’genres’的空值數據。
#根據'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”的零值數據 df_tmdb = df_tmdb[df_tmdb['budget_adj'] > 0] df_tmdb = df_tmdb[df_tmdb['popularity'] > 0]"popularity"實際不存在零值數據。
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第四步:探索性數據分析
研究問題 1 :哪種體裁/類型(genres)的平均預算(budget)最高?
要計算不同"genres"電影的"budget"均值,并經過排序得出最高的類型;并且為了消除通脹影響,計算"budget_adj"的數據。
# 以“genres”分組,計算各種體裁對應的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 #根據計算出的budget平均值,做bar圖比較 budget_mean.plot(kind = 'bar',color = 'grey')plt.ylabel('Budget')plt.title('Budget of different kinds of Genres');結論1:根據降序排列畫出的圖,排第一(平均預算最高的)的為“Adventure”類型。
研究問題 2 :最受歡迎(popularity)前五的體裁/類型(genres),每年的受歡迎程度是如何變化的?
計算不同"genres"電影對應的"popularity"均值,降序得到前五位的類型;并作出各類型電影,“popularity"隨"release_year”(上映時間)變化的圖像。
#計算不同體裁的影片,受歡迎程度的總平均值,降序排列 popularity_mean = df_tmdb.groupby(['genres'])['popularity'].mean().sort_values(ascending=False) #選取最受歡迎前五的體裁(獲取對應的索引表) most_popularity = popularity_mean[:5].index.tolist() most_popularity ['Adventure', 'Science Fiction', 'Fantasy', 'Animation', 'Action']結論2-1:最受歡迎(popularity)前五的電影類型是Adventure,Science Fiction,Fantasy,Action,Animation。
#選取前五的體裁,生成包含體裁(genres)、上映時間(release_year)、受歡迎度(popularity)的數據表 df_popularity = df_tmdb[['genres', 'release_year', 'popularity']].set_index('genres').loc[most_popularity].reset_index('genres') #建立一個五種體裁的,根據年份的受歡迎程度表 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));結論2-2:1960-2016期間最受歡迎的前五類電影類型中,除"Animation"外均在2010年后有了進一步提升。
研究問題 3 :哪種電影類型的評分(vote_average)最高;評分和受歡迎程度(popularity)有何關系?
計算"vote_average"的平均值并選出最高的類型(“genres”);根據作出的"vote_average"(評分)和"popularity"(受歡迎程度)散點圖了解相關關系。
# 得到vote_average均值最大的體裁類型 df_tmdb.groupby('genres')['vote_average'].mean().sort_values(ascending=False).index[0] 'Documentary'結論3-1:Documentary體裁的平均評分最高。
#得到不同體裁的評分、受歡迎程度表 df_vote = df_tmdb[['genres', 'vote_average', 'popularity']] #根據不同體裁,做“評分”與對應“受歡迎度”的散點圖 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();結論3-2:除去Western、Documentary、TV Movie、Foreign數據較少;可以發現隨著平均評分由低到高,均存在較低的受歡迎度(popularity),而隨著受歡迎度增加,對應的平均評分也相應提高。
結論
1.以根據通脹后的預算(budget)數據來比較,"Adventure"類型的電影1960-2016期間的平均預算最高。
其中去除了可能產生影響的"budget_adj"的零值數據(異常值),因為實際預算不可能為零因此此類零值數據不應作為分析要素使用,當然經過驗證:去除前后得到的上述結論相同。
2.1960-2016最受歡迎(popularity)前五的電影類型是Adventure,Science Fiction,Fantasy,Action,Animation;其中Adventure,Science Fiction,Action在2010年后受歡迎度有明顯提升。
有3種電影類型,由于對"budget_adj"異常值的處理 以及 本身的數據缺失,導致1985年以前的數據不連貫,因此5種最受歡迎類型的直接比較僅適用于1985年之后的變化。
3.1960-2016的數據中Documentary類型的平均評分最高;更高的受歡迎度,伴隨更高的評分,但高評分不代表受歡迎。
不同類型下的"popularity"均存在數值較大的數據,但不了解實際打分依據就無法真正判定是否為異常值,因此未做處理;另外根據Western、Documentary、TV Movie、Foreign四種類型,無法得出上述結論,可能由于數據量有限的緣故。
數據集來源
TMDb電影數據
總結
以上是生活随笔為你收集整理的Udacity数据分析(入门)-TMDb电影数据集探索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FGSM论文阅读笔记
- 下一篇: 队列(纯C语言实现)