用回归模型来看幸福感和城市富裕程度的关系
(相关数据请关注公众号‘一行数据’,回复“机器学习sklearn”可免费获得)
import matplotlib.pyplot as plt import numpy as np import pandas as pd from sklearn import linear_model #首先处理幸福的数据 #加载数据 oecd_bli = pd.read_csv("oecd_bli_2015.csv",thousands = ',') oecd_bli = oecd_bli[oecd_bli['Inequality']=='Total'] oecd_bli = oecd_bli.pivot(index = 'Country', columns = 'Indicator',values = 'Value') #接着处理gdp的数据 gdp_per_capita = pd.read_csv('gdp_per_capita.csv',thousands = ',', delimiter = '\t', encoding ='latin1',na_values = 'n/a') gdp_per_capita.rename(columns = {'2015':'GDP per captial'},inplace = True) gdp_per_capita.set_index('Country', inplace = True) gdp_per_capita.head(2) #将两张表融合在一起 full_country_stats = pd.merge(left