Intro to Machine Learning
本节主要用于机器学习入门,介绍两个简单的分类模型:
决策树和随机森林
不涉及内部原理,仅仅介绍基础的调用方法
1. How Models Work
以简单的决策树为例
This step of capturing patterns from data is called fitting or training the model
The data used to train the data is called the trainning data
After the model has been fit, you can apply it to new data to predict prices of additional homes

2.Basic Data Exploration
使用pandas中的describle()来探究数据:
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
melbourne.describe()
output:

注:数值含义
count: 非缺失值的数量
mean: 平均值
std: 标准偏差,它度量值在数值上的分布情况
min、25%、50%、75%、max: 将每一列按照从lowest到highest排序,最小值是min, 1/4位置上,大于25%而小于50%是25%
3.Your First Machine Learning Model
- Selecting Data for Modeling
import pandas as pd
melbourne_file_path = ' ../input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
- Selecting The Prediction Target
方法:使用dot-notation来挑选prediction target
- Choosing "Features"
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']
X = melbourne_data[melbourne_features]
查看数据是否加载正确:
X.head()
探究数据基本特性:
- Building Your Model
我们使用scikit-learn来创造模型,scikit-learn教程如下:
具体的原理可以根据需要自己探究
https://scikit-learn.org/stable/supervised_learning.html#supervised-learning
构建模型步骤:
- Define:
What type of model will it be? A decision tree? Some other type of model? Some other parameters of the model type are specified too.
- Fit:
Capture patterns from provided data. This is the heart of modeling
- Predict:
Just what it sounds like
- Evaluate:
Determine how accurate the model's predictions are
实现:
from sklearn.tree import DecisionTreeRegressor
melbourne_mode = DecisionTreeRegressor(random_state=1)
melbourne_mode.fit(X , y)
打印出开始几行:
print (X.head())
预测后的价格如下:
print (melbourne_mode.predict(X.head())
4.Model Validation
由于预测的价格和真实的价格会有差距,而差距多少,我们需要衡量
使用Mean Absolute Error
error= actual-predicted
在实际过程中,我们要将数据分成两份,一份用于训练,叫做training data, 一份用于验证叫validataion data
from sklearn.model_selection import train_test_split
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=0)
melbourne_model = DecisionTreeRegressor()
melbourne_model.fit(train_X, train_y)
val_predictions = melbourne_model.predict(val_X)
print(mean_absolute_error(val_y, val_predictions))
5.Underfitting and Overfitting
- overfitting: A model matches the data almost perfectly, but does poorly in validation and other new data.
- underfitting: When a model fails to capture important distinctions and patterns in the data, so it performs poorly even in training data.

The more leaves we allow the model to make, the more we move from the underfitting area in the above graph to overfitting area.

from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecsionTreeRegressor
def get_ame(max_leaf_nodes, train_X, val_X, train_y, val_y):
model = DecisionTreeRegressor(max_leaf_nodes = max_leaf_nodes, random_state = 0)
model.fit(train_X, train_y)
preds_val = model.predict(val_X)
mae = mean_absolute_error(val_y, preds_val)
return(mae)
我可以使用循环比较选择最合适的max_leaf_nodes
for max_leaf_nodes in [5,50,500,5000]:
my_ame = get_ame(max_leaf_nodes, train_X, val_X, train_y, val_y)
print(max_leaf_nodes, my_ame)

最后可以发现,当max leaf nodes 为 500时,MAE最小, 接下来我们换另外一种模型
6.Random Forests
The random forest uses many trees, and it makes a prediction by averaging the predictions of each component tree. It generally has much better predictive accuracy than a single decision tree and it works well with default parameters.
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
forest_model = RandomForestRegressor(random_state=1)
forest_model.fit(train_X,train_y)
melb_preds = forest_model.predict(val_X)
print(mean_absolute_error(val_y, melb_preds))
可以发现最后的误差,相对于决策树小。
one of the best features of Random Forest models is that they generally work reasonably even without this tuning.

7.Machine Learning Competitions
- Build a Random Forest model with all of your data
- Read in the "test" data, which doesn't include values for the target. Predict home values in the test data with your Random Forest model.
- Submit those predictions to the competition and see your score.
- Optionally, come back to see if you can improve your model by adding features or changing your model. Then you can resubmit to see how that stacks up on the competition leaderboard.
Intro to Machine Learning的更多相关文章
- 【机器学习Machine Learning】资料大全
昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】
转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...
- How do I learn machine learning?
https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644 How Can I Learn X? ...
- 机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- Easy machine learning pipelines with pipelearner: intro and call for contributors
@drsimonj here to introduce pipelearner – a package I'm developing to make it easy to create machine ...
- How do I learn mathematics for machine learning?
https://www.quora.com/How-do-I-learn-mathematics-for-machine-learning How do I learn mathematics f ...
- 机器学习案例学习【每周一例】之 Titanic: Machine Learning from Disaster
下面一文章就总结几点关键: 1.要学会观察,尤其是输入数据的特征提取时,看各输入数据和输出的关系,用绘图看! 2.训练后,看测试数据和训练数据误差,确定是否过拟合还是欠拟合: 3.欠拟合的话,说明模 ...
- 【Machine Learning】KNN算法虹膜图片识别
K-近邻算法虹膜图片识别实战 作者:白宁超 2017年1月3日18:26:33 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
随机推荐
- 深扒JVM,对它进行“开膛破肚”式解析!
1. 打怪升级,你绕不开JVM JVM,对Java程序员进阶而言,是一个绝对绕不开,也不能绕开的话题. 在你打怪升级.进阶蜕变的路上,势必会遇到项目上线中各种OOM.GC等问题,此时JVM的功底就至关 ...
- 理解MySQL(二)--数据库事务
1.事务:事务内的语句,要么全部执行成功,要么全部执行失败. a) 数据库事务四要素:ACID,原子性,一致性,隔离性,持久性. b) 原子性:一个事务必须被视为不可分割的最小单元 ...
- React Native 混合开发与实现
关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 随着 React 的盛行,其移动开发框架 React Native 也收到了广大开发者的青睐,以下简 ...
- Source Maps简介
提高网站性能最简单的方式之一是合并压缩JavaScript和CSS文件.但是当你需要调试这些压缩文件中的代码时,那将会是一场噩梦.不过也不用担心,souce maps将会帮你解决这一问题. Sourc ...
- UI 组件 | Toggle
Toggle(复选框)组件 Toggle 是一个 CheckBox,当它和 ToggleGroup 一起使用的时候,可以变成 RadioButton. 创建 Toggle 组件 层级管理器右击-> ...
- HBase 系列(十一)—— Spring/Spring Boot + Mybatis + Phoenix 整合
一.前言 使用 Spring+Mybatis 操作 Phoenix 和操作其他的关系型数据库(如 Mysql,Oracle)在配置上是基本相同的,下面会分别给出 Spring/Spring Boot ...
- React引入AntD按需加载报错
背景:React使用create-react-app脚手架创建,然后yarn run eject暴露了配置之后修改less配置, 需求:实现antd组件按需加载与修改主题. 一开始是按照webpack ...
- C# - 协变、逆变 看完这篇就懂了
1. 基本概念 官方:协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型.[MSDN] 公式: ...
- 一键部署 Spring Boot 到远程 Docker 容器,就是这么秀!
不知道各位小伙伴在生产环境都是怎么部署 Spring Boot 的,打成 jar 直接一键运行?打成 war 扔到 Tomcat 容器中运行?不过据松哥了解,容器化部署应该是目前的主流方案. 不同于传 ...
- 跨库数据迁移利器 —— Sqoop
一.Sqoop 基本命令 1. 查看所有命令 # sqoop help 2. 查看某条命令的具体使用方法 # sqoop help 命令名 二.Sqoop 与 MySQL 1. 查询MySQL所有数据 ...