Kaggle官方课程链接:Your First Machine Learning Model
本专栏旨在Kaggle官方课程的汉化,让大家更方便地看懂。
建立你的第一个模型。好哇!
你的数据集有太多的变量,你无法理解,甚至无法很好地打印出来。你如何将如此庞大的数据量缩减到你能理解的程度?
我们将从使用直觉选择几个变量开始。后续课程将向您展示自动确定变量优先级的统计技术。
要选择变量/列,我们需要看到数据集中所有列的列表。这是通过DataFrame的columns属性(下面的代码底线)完成的。
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')
# The Melbourne data has some missing values (some houses for which some variables weren't recorded.)
# We'll learn to handle missing values in a later tutorial.
# Your Iowa data doesn't have missing values in the columns you use.
# So we will take the simplest option for now, and drop houses from our data.
# Don't worry about this much for now, though the code is:
# dropna drops missing values (think of na as "not available")
melbourne_data = melbourne_data.dropna(axis=0)
有很多方法可以选择数据的子集。Pandas课程更深入地介绍了这些,但我们现在将重点介绍两种方法。
点符号,我们用它来选择“预测目标”
使用列列表进行选择,我们用它来选择“功能”
你可以用点符号提取一个变量。这一列存储在Series中,它大致类似于只有一列数据的DataFrame。
我们将使用点符号来选择要预测的列,称为预测目标。按照惯例,预测目标称为y。因此,我们需要在墨尔本数据中保存房价的代码是
y = melbourne_data.Price
输入到我们模型中的列(后来用于进行预测)称为“特征”。在我们的例子中,这些列将用于确定房价。有时,您将使用除目标之外的所有列作为特征。其他时候,你会用更少的功能做得更好。
现在,我们将构建一个只有少数功能的模型。稍后,您将看到如何迭代和比较使用不同功能构建的模型。
我们通过在括号内提供列名列表来选择多个功能。该列表中的每个项目都应该是一个字符串(带引号)。
以下是一个示例:
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']
按照惯例,这些数据被称为X。
X = melbourne_data[melbourne_features]
让我们快速回顾一下我们将使用描述法和头部法预测房价的数据,头部法显示了前几行。
X.describe()
Rooms | Bathroom | Landsize | Lattitude | Longtitude | |
---|---|---|---|---|---|
count | 6196.000000 | 6196.000000 | 6196.000000 | 6196.000000 | 6196.000000 |
mean | 2.931407 | 1.576340 | 471.006940 | -37.807904 | 144.990201 |
std | 0.971079 | 0.711362 | 897.449881 | 0.075850 | 0.099165 |
min | 1.000000 | 1.000000 | 0.000000 | -38.164920 | 144.542370 |
25% | 2.000000 | 1.000000 | 152.000000 | -37.855438 | 144.926198 |
50% | 3.000000 | 1.000000 | 373.000000 | -37.802250 | 144.995800 |
75% | 4.000000 | 2.000000 | 628.000000 | -37.758200 | 145.052700 |
max | 8.000000 | 8.000000 | 37000.000000 | -37.457090 | 145.526350 |
X.head()
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 |
使用这些命令直观地检查数据是数据科学家工作的重要组成部分。您经常会在数据集中发现值得进一步检查的惊喜。
您将使用scikit-learn库创建模型。在编码时,这个库被写成sklearn,正如您将在示例代码中看到的那样。Scikit-learn很容易成为对通常存储在DataFrames中的数据类型进行建模的最受欢迎的库。
构建和使用模型的步骤是:
定义:它将是什么类型的模型?决策树?其他类型的模型?还指定了模型类型的其他一些参数。
拟合:从提供的数据中捕获模式。这是建模的核心。
预测:听起来就是这样
评估:确定模型的预测有多准确。
下面是一个使用scikit-learn定义决策树模型并用特征和目标变量对其进行拟合的示例。
from sklearn.tree import DecisionTreeRegressor
# Define model. Specify a number for random_state to ensure same results each run
melbourne_model = DecisionTreeRegressor(random_state=1)
# Fit model
melbourne_model.fit(X, y)
DecisionTreeRegressor(random_state=1)
许多机器学习模型允许模型训练中存在一些随机性。为random_state指定一个数字可确保您在每次运行中获得相同的结果。这被认为是一种良好的做法。您可以使用任何数字,模型质量将不会有意义地取决于您选择的确切值。
我们现在有一个拟合模型,可以用来进行预测。
在实际操作中,你会希望对即将上市的新房进行预测,而不是对我们已经有价格的房子进行预测。但是,我们将对训练数据的前几行进行预测,看看预测函数是如何工作的。
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.]
在建模练习中自己试试