机器学习实战 - Chapter 1 - ML Landscape - Code Annotation

本文通过Python加载并处理OECD的生活满意度和IMF的GDP数据,进行数据预处理,去除异常值。然后,通过数据可视化展示二者之间的关系,并建立模型预测生活满意度。最后,对模型进行评估并探讨可能的关联性。
摘要由CSDN通过智能技术生成

Instance

If money makes people happy, so you download the Better life index from the OECD’s website as well as stats about GDP per capita from the IMF’s website. Then you join the tables and sort by GDP per capita.

Reference: https://www.bilibili.com/video/BV1iJ411k7Gg

1. Preparation

Code:

assert : validate the command line, if yes, continue

# Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

path.join( load the datasets’ path)
The last ‘’’’ : the symbol of \

import os
datapath = os.path.join("datasets", "lifesat", "")

%matplotlib inline: Show the figures directly within Jupyter

# To plot pretty figures directly within Jupyter
%matplotlib inline
import matplotlib as mpl
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

2. Load And Preparation Life satisfaction and GDP per capita data

delimiter=’\t’ : delimiter(分隔符) \t(制表符)
na_values=‘n/a’ Change nan or NaN data to N/A

oecd_bli = pd.read_csv(datapath+'oecd_bli_2015.csv', thousands=',')
gdp_per_capita = pd.read_csv(datapath+'gdp_per_capita.csv', thousands=',',delimiter='\t', encoding='latin1',na_values='n/a')

3. Review Dataset Architecture

在这里插入图片描述
在这里插入图片描述

4. Data Processing

Need to filter Inequality == TOT

在这里插入图片描述

Use pivot to reset index and columns

在这里插入图片描述

Replace column name and set index
inplace: Modify on the original data

在这里插入图片描述

Merge two file into one
left_index: 如果为True,则使用左侧DataFrame中的索引(行标签)作为其连接键。 对于具有MultiIndex(分层)的DataFrame,级别数必须与右侧DataFrame中的连接键数相匹配。
right_index: 与left_index功能相似

在这里插入图片描述

Sort values as GDP per capita
Default is ascending order

在这里插入图片描述

Merge the final data

在这里插入图片描述

5.Visualize the data

kind, refer to https://blog.csdn.net/h_hxx/article/details/90635650

在这里插入图片描述

Save data to a file

country_stats.to_csv('country_stats.csv')

6. Outliers Processing

Delete outliers
iloc → 基于行、列索引序号进行查询

在这里插入图片描述

Visualize the outlier data

在这里插入图片描述

7. Model Processing

Show valuable data

在这里插入图片描述

Model conjecture(猜想)

在这里插入图片描述

Train the Model

在这里插入图片描述

Gain the perfect intercept and coefficient of the linear

在这里插入图片描述

Input Cyprus’s GDP and try to predict its satisfaction

在这里插入图片描述

Visualize all data

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值