forked from apachecn/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Leytton
authored and
Leytton
committed
Oct 16, 2019
1 parent
28f23c6
commit edcf2a2
Showing
6 changed files
with
274 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# 机器学习入门1 模型是怎样工作的 | ||
|
||
> 原文:https://www.kaggle.com/dansbecker/how-models-work | ||
# Kaggle 官方教程:机器学习入门1 模型是怎样工作的 | ||
> 原文:[Intro to Machine Learning](https://www.kaggle.com/learn/intro-to-machine-learning) > [How Models Work](https://www.kaggle.com/dansbecker/how-models-work) | ||
> | ||
> 译者:[Leytton](https://github.com/Leytton) | ||
> | ||
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
PS:水平有限,欢迎交流指正([email protected]) | ||
|
||
## 1、简介 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# 机器学习入门2 数据探索 | ||
|
||
> 原文:https://www.kaggle.com/dansbecker/basic-data-exploration | ||
# Kaggle 官方教程:机器学习入门2 数据探索 | ||
> 原文:[Intro to Machine Learning](https://www.kaggle.com/learn/intro-to-machine-learning) > [Basic Data Exploration](https://www.kaggle.com/dansbecker/basic-data-exploration) | ||
> | ||
> 译者:[Leytton](https://github.com/Leytton) | ||
> | ||
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
PS:水平有限,欢迎交流指正([email protected]) | ||
|
||
## 1、使用Pandas熟悉数据 | ||
任何机器学习项目的第一步都是熟悉数据。你可以使用`Pandas`来实现。`Pandas`是数据科学家用来探索和操作数据的主要工具。大多数人在代码中将`panda`简写为`pd`,使用以下代码将其引用: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Kaggle 官方教程:机器学习入门3 你的第一个机器学习模型 | ||
> 原文:[Intro to Machine Learning](https://www.kaggle.com/learn/intro-to-machine-learning) > [Your First Machine Learning Model](https://www.kaggle.com/dansbecker/your-first-machine-learning-model) | ||
> | ||
> 译者:[Leytton](https://github.com/Leytton) | ||
> | ||
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
PS:水平有限,欢迎交流指正([email protected]) | ||
|
||
## 1、选择建模数据 | ||
原始数据集有太多的干扰变量,难以理解,甚至无法很好地打印出来。如何将这些数据处理为比较精简易懂呢? | ||
|
||
我们先凭直觉选择几个变量。后面的课程将向你展示使用统计技术来自动优选变量。 | ||
|
||
选择变量/列前,我们先来看看数据集中有哪些列,使用`DataFrame`的`columns`属性来实现,代码如下: | ||
```python | ||
import pandas as pd | ||
|
||
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv' | ||
melbourne_data = pd.read_csv(melbourne_file_path) | ||
melbourne_data.columns | ||
``` | ||
输出结果: | ||
``` | ||
Index(['Suburb', 'Address', 'Rooms', 'Type', 'Price', 'Method', 'SellerG', | ||
'Date', 'Distance', 'Postcode', 'Bedroom2', 'Bathroom', 'Car', | ||
'Landsize', 'BuildingArea', 'YearBuilt', 'CouncilArea', 'Lattitude', | ||
'Longtitude', 'Regionname', 'Propertycount'], | ||
dtype='object') | ||
``` | ||
|
||
- Melbourne 数据集有部分缺失(有些房子没有记录变量。) | ||
- 我们将在后面的教程中学习如何处理缺失值。 | ||
- Iowa 数据没有缺失值。 | ||
- 现在我们将采用最简单的方法,从数据中删除掉数据缺失的房屋。 | ||
- 现在不要太担心这个,代码如下: | ||
```python | ||
# dropna 删除有缺失的数据 (na可以看作是"not available") | ||
melbourne_data = melbourne_data.dropna(axis=0) | ||
``` | ||
|
||
有许多选择数据子集的方法,[《Pandas微课程》](https://www.kaggle.com/learn/pandas)将更深入地介绍这些内容,但目前我们将重点介绍两种方法。 | ||
There are many ways to select a subset of your data. The Pandas Micro-Course covers these in more depth, but we will focus on two approaches for now. | ||
|
||
1. `点符号`,用来选择`"预测目标"`; | ||
2. `列数组`, 用来选择`“特性”`。 | ||
|
||
|
||
## 2、选择预测目标 | ||
你可以用`点符号`来获取一个变量。这个单列存储在一个系列中,与只有单列数据的`DataFrame`非常类似。 | ||
|
||
我们将使用点符号来选择要预测的列,这称为预测目标。按照惯例,预测目标我们记为`y`,将房价保存到`y`变量的代码为: | ||
```python | ||
y = melbourne_data.Price | ||
``` | ||
|
||
## 3、选择特征值 | ||
输入到模型中的数据列(稍后用于进行预测)称为“特性”。在我们的例子中,这些数据列是用来确定房价。有时,你将使用除预测目标之外的所有数据列作为特性。但有时候,剔除无效的特征,预测效果会更好。 | ||
|
||
现在,我们将构建一个只包含几个特性的模型。稍后你将看到如何迭代和比较这些使用不同特性构建的模型。 | ||
我们通过在括号内提供列名列表来选择多个特性。列表中的每一项都应该是一个字符串(带引号)。 | ||
|
||
在数据集中提取多个特征列的代码如下(列名称应该是字符串类型): | ||
|
||
```python | ||
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude'] | ||
``` | ||
按照惯例,这个数据称为X。 | ||
```python | ||
X = melbourne_data[melbourne_features] | ||
``` | ||
我们快速地用`describe`方法与`head `方法,来查看数据概览: | ||
```python | ||
X.describe() | ||
``` | ||
输出结果: | ||
|
||
![在这里插入图片描述](/img/learn/intro-to-machine-learning/3.1.png) | ||
|
||
```python | ||
X.head() | ||
``` | ||
输出结果: | ||
|
||
![在这里插入图片描述](/img/learn/intro-to-machine-learning/3.2.png) | ||
|
||
使用这些命令可视化地检查数据是数据科学家工作的一个重要部分,你会经常在其中发现值得进一步研究的惊喜。 | ||
|
||
--- | ||
## 4、构建模型 | ||
|
||
你将使用`scikit-learn`库创建模型。编写代码时,这个库被编写为`sklearn`,你将在示例代码中看到。`Scikit-learn` 很容易将处理存储在`DataFrames`中的数据进行建模,是最流行的库。 | ||
|
||
建立和使用模型的步骤如下: | ||
|
||
`定义`: 它将是什么类型的模型?一个决策树吗?还是其他类型的模型?也指定了模型类型的一些参数。 | ||
`拟合: `从提供的数据中捕获模式,这是建模的核心。 | ||
`预测: `表面意思,这个就不说了 | ||
`评估: `确定模型的预测有多准确。 | ||
|
||
下面是一个用`scikit-learn`定义一个`决策树模型`,并用`特性`和`目标`变量进行拟合的例子: | ||
```python | ||
from sklearn.tree import DecisionTreeRegressor | ||
|
||
# 定义模型. 指定一个参数random_state确保每次运行结果一致 | ||
melbourne_model = DecisionTreeRegressor(random_state=1) | ||
|
||
# 拟合模型 | ||
melbourne_model.fit(X, y) | ||
``` | ||
输出结果: | ||
``` | ||
DecisionTreeRegressor(criterion='mse', max_depth=None, max_features=None, | ||
max_leaf_nodes=None, min_impurity_decrease=0.0, | ||
min_impurity_split=None, min_samples_leaf=1, | ||
min_samples_split=2, min_weight_fraction_leaf=0.0, | ||
presort=False, random_state=1, splitter='best') | ||
``` | ||
许多机器学习模型在模型训练中允许一定的随机性。为`random_state`指定一个数字可以确保每次运行得到相同的结果。这被认为是一种很好的做法。你使用任何数字,而模型的质量并不完全取决于你所选择的值。 | ||
|
||
我们现在有了一个拟合的模型,可以用来进行预测。 | ||
|
||
在实践中,你会想要预测即将上市的新房子,而不是我们已经有价格的房子。但我们将对训练数据的前几行进行预测,以了解`predict`函数是如何工作的。 | ||
```python | ||
print("Making predictions for the following 5 houses:") | ||
print(X.head()) | ||
print("The predictions are") | ||
print(melbourne_model.predict(X.head())) | ||
``` | ||
输出结果: | ||
``` | ||
Making predictions for the following 5 houses: | ||
Rooms Bathroom Landsize Lattitude Longtitude | ||
1 2 1.0 156.0 -37.8079 144.9934 | ||
2 3 2.0 134.0 -37.8093 144.9944 | ||
4 4 1.0 120.0 -37.8072 144.9941 | ||
6 3 2.0 245.0 -37.8024 144.9993 | ||
7 2 1.0 256.0 -37.8060 144.9954 | ||
The predictions are | ||
[1035000. 1465000. 1600000. 1876000. 1636000.] | ||
``` | ||
## 5、去吧,皮卡丘 | ||
在[模型构建练习](https://www.kaggle.com/kernels/fork/400771)中自己尝试一下~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Kaggle 官方教程:机器学习入门4 模型验证 | ||
> 原文:[Intro to Machine Learning](https://www.kaggle.com/learn/intro-to-machine-learning) > [Model Validation](https://www.kaggle.com/dansbecker/model-validation) | ||
> | ||
> 译者:[Leytton](https://github.com/Leytton) | ||
> | ||
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
PS:水平有限,欢迎交流指正([email protected]) | ||
|
||
你已经建立了一个模型。但是它好不好呢? | ||
在本节课中,你将学习使用模型验证来度量模型的质量。度量模型质量是迭代改进模型的关键。 | ||
|
||
# 1、什么是模型验证 | ||
你将需要评估几乎所有构建的模型。在大多数应用程序中,模型质量的相关度量是预测精度。换言之,模型预测结果是否接近实际发生情况。 | ||
|
||
`许多人在测量预测精度时犯了一个巨大的错误。他们用训练数据进行预测,并将预测结果与训练数据中的目标值进行比较。`你很快就会发现这个弊端并且学习如何解决它,先让我们想一下该怎么做吧。 | ||
|
||
你首先需要以一种可理解的方式总结模型质量。如果你比较10000套房子的预测和实际房价,你可能会发现好坏参半。浏览10000个预测值和实际值的数据是没有意义的。我们需要将其总结为一个单一的度量。 | ||
|
||
总结模型质量有许多度量标准,但我们将从一个称为`平均绝对误差(Mean Absolute Error,也称为MAE)`的度量标准开始。让我们从最后一个单词`error`开始分析这个度量。 | ||
|
||
每栋房子的预测误差为: | ||
``` | ||
error = actual − predicted | ||
``` | ||
所以,如果一栋房子实际售价15万美元,而你预测它的售价是10万美元,则误差是5万美元。 | ||
|
||
利用MAE度量,我们取每个误差的绝对值,这将把每个误差转换为一个正数。然后取绝对误差的平均值。这就是我们对模型质量的评估。也可以这么说: | ||
>我们的预测平均偏离了X。 | ||
为了计算MAE,我们首先需要一个模型: | ||
|
||
```python | ||
import pandas as pd | ||
|
||
# 加载数据 | ||
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv' | ||
melbourne_data = pd.read_csv(melbourne_file_path) | ||
# 过滤缺失数据 | ||
filtered_melbourne_data = melbourne_data.dropna(axis=0) | ||
# 选择预测目标和特征 | ||
y = filtered_melbourne_data.Price | ||
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea', | ||
'YearBuilt', 'Lattitude', 'Longtitude'] | ||
X = filtered_melbourne_data[melbourne_features] | ||
|
||
from sklearn.tree import DecisionTreeRegressor | ||
# 定义模型 | ||
melbourne_model = DecisionTreeRegressor() | ||
# 拟合模型 | ||
melbourne_model.fit(X, y) | ||
``` | ||
我们有了一个模型,下面来计算平均绝对误差: | ||
```python | ||
from sklearn.metrics import mean_absolute_error | ||
|
||
predicted_home_prices = melbourne_model.predict(X) | ||
mean_absolute_error(y, predicted_home_prices) | ||
``` | ||
输出数据: | ||
``` | ||
434.71594577146544 | ||
``` | ||
# 2、“样本内”分数问题 | ||
刚刚计算的测量值可以称为“样本内”分数。我们使用了单个的房屋“样本”来构建模型并对其进行评估。这就是弊端产生原因。 | ||
|
||
想象一下,在大型房地产市场中,门的颜色与房价无关。 | ||
|
||
然而,在你用于构建模型的数据样本中,所有带有绿色门的住宅都非常昂贵。该模型的工作是找到预测房价的模式,所以它会看到这种模式,而且总是会将带有绿色门的房子预测为高价。 | ||
|
||
由于这种模式是从训练数据中推导出来的,所以模型在训练数据中会显得比较准确。 | ||
但是,如果模型使用新数据进行预测,这种模式是不成立的,在实际使用中,模型将非常不准确。 | ||
|
||
由于模型的实用价值体现在对新数据的预测,所以我们需要使用没有参与模型构建的数据,来度量模型性能。 | ||
最直接的方法是从模型构建过程中`排除一些数据`,然后使用这些数据来测试模型的准确性。这些数据称为`验证数据`。 | ||
|
||
# 3、编程实现 | ||
`scikit-learn`库有一个函数`train_test_split`,用于将数据分成两部分。我们将使用一部分数据作为训练数据来适应模型,并将使用另一部分数据作为验证数据来计算`mean_absolute_error`。 | ||
|
||
代码如下: | ||
```python | ||
from sklearn.model_selection import train_test_split | ||
|
||
# 将数据分割为训练和验证数据,都有特征和预测目标值 | ||
# 分割基于随机数生成器。为random_state参数提供一个数值可以保证每次得到相同的分割 | ||
# 执行下面代码 | ||
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)) | ||
``` | ||
输出数据: | ||
``` | ||
260585.51323434475 | ||
``` | ||
|
||
# 3、哇! | ||
|
||
样本内数据的平均绝对误差是500美元。样本外价格超过25万美元。 | ||
|
||
这就是一个几乎完全正确的模型和一个不实用模型之间的区别。实际上,验证数据中的平均房屋价值为110万美元。因此,预测误差约为实际平均房价的四分之一。 | ||
|
||
有很多方法可以改进这个模型,比如尝试找到更好的特征或使用不同类型的模型。 | ||
|
||
# 4、去吧,皮卡丘 | ||
在我们考虑改进这个模型之前,请自己尝试[模型验证](https://www.kaggle.com/kernels/fork/1259097)。 | ||
|
||
原文: | ||
https://www.kaggle.com/dansbecker/model-validation |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.