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

[7]

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

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

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

尝试进行模型建立练习

猜你喜欢

转载自blog.csdn.net/cg129054036/article/details/82250954