<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          GDP越高就越幸福嗎?用Python分析《世界幸福指數(shù)報告》后我們發(fā)現(xiàn)…

          共 6629字,需瀏覽 14分鐘

           ·

          2020-10-26 02:51



          作者:真達、Mika

          數(shù)據(jù):真達

          來源:CDA數(shù)據(jù)分析師

          【導(dǎo)讀】

          今天教大家用Python分析《世界幸福指數(shù)報告》。公眾號后臺,回復(fù)關(guān)鍵字“幸福”獲取完整數(shù)據(jù)。


          大家好,歡迎來到Crossin的編程教室!


          《世界幸福指數(shù)報告》是對全球幸福狀況的一次具有里程碑意義的調(diào)查。


          民意測驗機構(gòu)蓋洛普從2012年起,每年都會在聯(lián)合國計劃下發(fā)布《世界幸福指數(shù)報告》,報告會綜合兩年內(nèi)150多個國家的國民對其所處社會、城市和自然環(huán)境等因素進行評價后,再根據(jù)他們所感知的幸福程度對國家進行排名。


          《世界幸福指數(shù)報告》的編撰主要依賴于對150多個國家的1000多人提出一個簡單的主觀性問題:“如果有一個從0分到10分的階梯,頂層的10分代表你可能得到的最佳生活,底層的0分代表你可能得到的最差生活。你覺得你現(xiàn)在在哪一層?”


          那么哪個國家在總體幸福指數(shù)上排名最高?

          哪些因素對幸福指數(shù)的影響最大?


          今天我們就帶你用Python來聊一聊。


          01

          數(shù)據(jù)理解


          關(guān)鍵字段含義解釋:


          1. rank:幸福指數(shù)排名
          2. region:國家
          3. happiness:幸福指數(shù)得分
          4. gdp_per_capita:GDP(人均國內(nèi)生產(chǎn)總值)
          5. healthy_life_expectancy:健康預(yù)期壽命
          6. freedom_to_life_choise:自由權(quán)
          7. generosity:慷慨程度
          8. year:年份
          9. corruption_perceptions:清廉指數(shù)
          10. social_support:社會支持(客觀上物質(zhì)上的援助和直接服務(wù);主觀上指個體感到在社會中被尊重、被支持和被理解的情緒體驗和滿意程度。)

          02

          數(shù)據(jù)導(dǎo)入和數(shù)據(jù)整理


          首先導(dǎo)入所需包。

          #?數(shù)據(jù)整理?
          import?numpy?as?np?
          import?pandas?as?pd?

          #?可視化
          import?matplotlib.pyplot?as?plt?
          import?seaborn?as?sns?
          import?plotly?as?py?
          import?plotly.graph_objs?as?go?
          import?plotly.express?as?px?
          from?plotly.offline?import?init_notebook_mode,?iplot,?plot?

          init_notebook_mode(connected=True)
          plt.style.use('seaborn')?


          #?讀入數(shù)據(jù)
          df_2015?=?pd.read_csv('./deal_data/2015.csv')
          df_2016?=?pd.read_csv('./deal_data/2016.csv')
          df_2017?=?pd.read_csv('./deal_data/2017.csv')
          df_2018?=?pd.read_csv('./deal_data/2018.csv')?
          df_2019?=?pd.read_csv('./deal_data/2019.csv')

          #?新增列-年份
          df_2015["year"]?=?str(2015)
          df_2016["year"]?=?str(2016)
          df_2017["year"]?=?str(2017)
          df_2018["year"]?=?str(2018)
          df_2019["year"]?=?str(2019)

          #?合并數(shù)據(jù)
          df_all?=?df_2015.append([df_2016,?df_2017,?df_2018,?df_2019],?sort=False)
          df_all.drop('Unnamed:?0',?axis=1,?inplace=True)
          df_all.head()??


          print(df_2015.shape,?df_2016.shape,?df_2017.shape,?df_2018.shape,?df_2019.shape)?
          (158,?10)?(157,?10)?(155,?10)?(156,?11)?(156,?11)


          df_all.info()?


          <class?'pandas.core.frame.DataFrame'>
          Int64Index:?782?entries,?0?to?155
          Data?columns?(total?10?columns):
          region?????????????????????782?non-null?object
          rank???????????????????????782?non-null?int64
          happiness??????????????????782?non-null?float64
          gdp_per_capita?????????????782?non-null?float64
          healthy_life_expectancy????782?non-null?float64
          freedom_to_life_choise?????782?non-null?float64
          corruption_perceptions?????781?non-null?float64
          generosity?????????????????782?non-null?float64
          year???????????????????????782?non-null?object
          social_support?????????????312?non-null?float64
          dtypes:?float64(7),?int64(1),?object(2)
          memory?usage:?67.2+?KB


          03

          數(shù)據(jù)可視化


          2019世界幸福地圖

          整體來看,北歐的國家幸福指數(shù)較高,如冰島、丹麥、挪威、芬蘭;東非和西非的國家幸福指數(shù)較低,如多哥、布隆迪、盧旺達和坦桑尼亞。


          代碼展示:

          data?=?dict(type?=?'choropleth',?
          ???????????locations?=?df_2019['region'],
          ???????????locationmode?=?'country?names',
          ???????????colorscale?=?'RdYlGn',
          ???????????z?=?df_2019['happiness'],?
          ???????????text?=?df_2019['region'],
          ???????????colorbar?=?{'title':'Happiness'})

          layout?=?dict(title?=?'Geographical?Visualization?of?Happiness?Score?in?2019',?
          ??????????????geo?=?dict(showframe?=?True,?projection?=?{'type':?'azimuthal?equal?area'}))

          choromap3?=?go.Figure(data?=?[data],?layout=layout)
          plot(choromap3,?filename='./html/世界幸福地圖.html')


          2019世界幸福國家排行Top10

          2019年報告,芬蘭連續(xù)兩年被評為“全球最幸福國家”。丹麥、挪威、冰島、荷蘭進入前五名,對比2018年報告,中國從86名下降到93名。


          代碼展示:

          #?合并數(shù)據(jù)
          rank_top10?=?df_2019.head(10)[['rank',?'region',?'happiness']]
          last_top10?=?df_2019.tail(10)[['rank',?'region',?'happiness']]
          rank_concat?=?pd.concat([rank_top10,?last_top10])

          #?條形圖
          fig?=?px.bar(rank_concat,?
          ?????????????x="region",?
          ?????????????y="happiness",?
          ?????????????color="region",?
          ?????????????title="World's?happiest?and?least?happy?countries?in?2019")

          plot(fig,?filename='./html/2019世界幸福國家排行Top10和Last10.html')


          幸福指數(shù)相關(guān)性


          我們可以得出以下結(jié)論:


          • 從影響因素相關(guān)性熱力圖可以看出,在影響幸福得分的因素中,GDP、社會支持、健康預(yù)期壽命呈現(xiàn)高度相關(guān),自由權(quán)呈現(xiàn)中度相關(guān),國家的廉政水平呈現(xiàn)低度相關(guān),慷慨程度則呈現(xiàn)極低的相關(guān)性;

          • GDP與健康預(yù)期壽命、社會支持之間存在高度相關(guān)。說明GDP高的國家,醫(yī)療水平和社會福利較為完善,人民的預(yù)期壽命也會越高;

          • 健康預(yù)期壽命與社會支持之間存在中度相關(guān)性。


          以下分別觀察各個因素的影響程度。


          GDP和幸福得分

          人均GDP與幸福得分呈高度線性正相關(guān)關(guān)系,GDP越高的國家,幸福水平相對越高。


          代碼展示:

          #?散點圖
          fig?=?px.scatter(df_all,?x='gdp_per_capita',?
          ?????????????????y='happiness',
          ?????????????????facet_row='year',
          ?????????????????color='year',
          ?????????????????trendline='ols'
          ????????????????)?
          fig.update_layout(height=800,?title_text='GDP?per?capita?and?Happiness?Score')
          plot(fig,?filename='./html/GDP和幸福得分.html')


          健康預(yù)期壽命和幸福得分

          健康預(yù)期壽命與幸福得分呈高度線性正相關(guān)關(guān)系,健康預(yù)期壽命越高的國家,幸福水平相對越高。


          代碼展示:

          ?散點圖
          fig?=?px.scatter(df_all,?x='healthy_life_expectancy',?
          ?????????????????y='happiness',
          ?????????????????facet_row='year',
          ?????????????????color='year',
          ?????????????????trendline='ols'
          ????????????????)??
          fig.update_layout(height=800,?title_text='Healthy?Life?Expecancy?and?Happiness?Score')
          plot(fig,?filename='./html/健康預(yù)期壽命和幸福得分.html')?


          GDP和幸福水平動態(tài)圖


          代碼展示:

          fig?=?px.scatter(df_all,?
          ?????????????????x='gdp_per_capita',?
          ?????????????????y='happiness',
          ?????????????????animation_frame='year',
          ?????????????????animation_group='region',
          ?????????????????size='rank',
          ?????????????????color='region',
          ?????????????????hover_name='region',
          ?????????????????trendline='ols'
          ????????????????)?
          fig.update_layout(title_text='Happiness?Rank?vs?GDP?per?Capita')?
          plot(fig,?filename='./html/GDP和幸福水平動態(tài)圖展示.html')?


          健康預(yù)期壽命和幸福水平動態(tài)圖


          代碼展示:

          fig?=?px.scatter(df_all,?
          ?????????????????x='healthy_life_expectancy',?
          ?????????????????y='happiness',
          ?????????????????animation_frame='year',
          ?????????????????animation_group='region',
          ?????????????????size='rank',
          ?????????????????color='region',
          ?????????????????hover_name='region',
          ?????????????????trendline='ols'
          ????????????????)?
          fig.update_layout(title_text='Happiness?Rank?vs?healthy_life_expectancy')?
          plot(fig,?filename='./html/健康預(yù)期壽命和幸福水平動態(tài)圖展示.html')?


          04

          數(shù)據(jù)建模


          我們使用線性回歸進行建立一個基準(zhǔn)模型,首先篩選一下建模變量,并刪除空值記錄。

          sel_cols?=?['happiness',?'gdp_per_capita',?'healthy_life_expectancy',?
          ????????????'freedom_to_life_choise',?'corruption_perceptions',?'generosity']
          #?重置索引
          df_model.index?=?range(df_model.shape[0])
          df_model?=?df_all[sel_cols]?
          #?刪除空值
          df_model?=?df_model.dropna()?
          df_model.head()?


          from?statsmodels.formula.api?import?ols

          #?建立多元線性回歸模型
          lm_m?=?ols(formula='happiness?~?gdp_per_capita?+?healthy_life_expectancy?+?freedom_to_life_choise?+?corruption_perceptions?+?generosity',?
          ???????????data=df_model).fit()
          lm_m.summary()??



          模型的R-squared=0.744,擬合效果尚可,根據(jù)模型的參數(shù)可知:


          • 變量重要性排序為:gdp_per_capita、freedom_to_life_choise、healthy_life_expectancy、corruption_perceptions、generosity
          • 控制其他變量不變的情況下,GDP指數(shù)每增加一個單位,幸福指數(shù)增加1.32個單位,健康預(yù)期壽命指數(shù)每增加一個單位,幸福指數(shù)增加1.21個單位。


          比較預(yù)測值和真實值的分布:

          df_pred?=?pd.concat([df_model['happiness'],?y_pred],?axis=1)?
          df_pred.columns?=?['y_true',?'y_pred']

          #?散點圖
          fig?=?px.scatter(df_pred,?x='y_true',?y='y_pred',?trendline='ols')
          fig.update_layout(title='Resid?of?OLS?Regression')
          plot(fig,?filename='./html/預(yù)測值和真實值分布圖.html')???


          以下為模型殘差分布圖。

          fig?=?px.histogram(x=lm_m.resid)?
          fig.update_layout(title='Resid?of?OLS?Regression')
          plot(fig,?filename='./html/多元線性回歸殘差分布圖.html')


          公眾號后臺回復(fù)關(guān)鍵字:?幸福
          獲取詳細數(shù)據(jù)代碼


          _往期文章推薦_

          你猜去年電影國內(nèi)票房最高的演員是誰




          瀏覽 50
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天堂AV导航 | 国产第页| 亚洲最大在线观看视频 | 青娱乐国产一区 | www豆花AV |