3.Your First Machine Learning Model

Selecting Data for Modeling

你的数据集有太多的变量包裹住你的头。你怎么能把这些压倒性的数据削减到你能理解的东西?
我们首先使用我们的直觉选择一些变量。 后面的课程将向您展示自动确定变量优先级的统计技巧。
要选择变量/列,我们需要查看数据集中所有列。 这是通过DataFrame的columns属性(下面的代码)完成的。

[1]

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')

[2]

# 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课程更深入地介绍了这些内容,但我们现在将重点关注两种方法。

  1.      点符号,我们用它来选择“预测目标”
  2.      选择列表,我们用它来选择

Selecting The Prediction Target

您可以使用点符号来提取变量。 这一列存储在一个Series中,它大致类似于只有一列数据的DataFrame。
我们将使用点符号来选择我们想要预测的列,这称为预测目标。 按照惯例,预测目标称为y。 因此,我们需要在墨尔本数据中保存房价的代码是

[3]

y = melbourne_data.Price

Choosing "Features"

我们模型中的列(后来用于预测)被称为“特征”。 在我们的例子中,那些将是用于确定房价的列。 有时,您将使用除目标之外的所有列作为要素。 其他时候你用更少的功能会更好。
目前,我们将构建一个只有少数特征的模型。 稍后您将看到如何迭代和比较使用不同特征构建的模型。
我们通过在括号内提供列表名来选择多个特征。 该列表中的每个项目都应该是一个字符串(带引号)。
这是一个例子:

【4】

melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'Lattitude', 'Longtitude']

按照惯例,这个数据称为X.

【5】

X = melbourne_data[melbourne_features]

让我们使用describe方法和head方法快速查看我们将用于预测房价的数据,该方法显示前几行。

【6】

X.describe()
 RoomsBathroomLandsizeLattitudeLongtitude
count6196.0000006196.0000006196.0000006196.0000006196.000000
mean2.9314071.576340471.006940-37.807904144.990201
std0.9710790.711362897.4498810.0758500.099165
min1.0000001.0000000.000000-38.164920144.542370
25%2.0000001.000000152.000000-37.855438144.926198
50%3.0000001.000000373.000000-37.802250144.995800
75%4.0000002.000000628.000000-37.758200145.052700
max8.0000008.00000037000.000000-37.457090145.526350

[7]

X.head()
 RoomsBathroomLandsizeLattitudeLongtitude
121.0156.0-37.8079144.9934
232.0134.0-37.8093144.9944
441.0120.0-37.8072144.9941
632.0245.0-37.8024144.9993
721.0256.0-37.8060144.9954

使用这些命令直观地检查数据是数据科学家工作的重要组成部分。 您经常会在数据集中发现值得进一步检查的惊喜。

Building Your Model

您将使用scikit-learn库来创建模型。 编码时,此库编写为sklearn,您将在示例代码中看到。 Scikit-learn是最常用的库,用于对通常存储在DataFrame中的数据类型进行建模。

构建和使用模型的步骤如下:
     定义:它将是什么类型的模型? 决策树? 其他一些模型? 还指定了模型类型的一些其他参数。
     拟合:从提供的数据中捕获模式,这是建模的核心。
     预测:听起来是什么样的
     评估:确定模型预测的准确程度。

下面是使用scikit-learn定义决策树模型并将其与特征和目标变量拟合的示例。

【8】

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(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指定一个数字可确保您在每次运行中获得相同的结果。 这被认为是一种很好的做法。 您使用任何数字,模型质量不会取决于您选择的确切值。

我们现在有一个可以用来进行预测的拟合模型。

在实践中,你会想要对市场上的新房子进行预测,而不是对我们已经有价格的房屋进行预测。 但是我们将对训练数据的前几行进行预测,以了解预测函数的工作原理。

【9】

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.]

Your Turn

尝试进行模型建立练习

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值