sklearn快速入门教程
目录
导言
决策树给你留下了一个艰难的决定。一棵长着很多叶子的大树会过度生长,因为每一个预测都来自历史数据,这些数据只来自它叶子上的少数几栋房屋。但是,一棵叶子很少的浅树将表现不佳,因为它无法在原始数据中捕捉到同样多的差异。
即使是今天最复杂的建模技术也面临着这种不适和过度适配之间的紧张关系。但是,许多模型都有可以带来更好性能的聪明想法。我们将以随机森林为例。
随机森林使用许多树,它通过平均每个组成树的预测来进行预测。它通常比单个决策树具有更好的预测精度,并且可以很好地处理默认参数。如果你继续建模,你可以学习更多性能更好的模型,但其中许多模型参数对正确的结果很敏感。
范例
您已经多次看到加载数据的代码。在数据加载结束时,我们有以下变量:
- train_X
- val_X
- train_y
- val_y
import pandas as pd
# Load data
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path)
# Filter rows with missing values
melbourne_data = melbourne_data.dropna(axis=0)
# Choose target and features
y = melbourne_data.Price
melbourne_features = ['Rooms', 'Bathroom', 'Landsize', 'BuildingArea',
'YearBuilt', 'Lattitude', 'Longtitude']
X = melbourne_data[melbourne_features]
from sklearn.model_selection import train_test_split
# split data into training and validation data, for both features and target
# The split is based on a random number generator. Supplying a numeric value to
# the random_state argument guarantees we get the same split every time we
# run this script.
train_X, val_X, train_y, val_y = train_test_split(X, y,random_state = 0)
我们构建了一个随机森林模型,类似于我们在scikit-learn
中构建决策树的方式——这次使用的是“RandomForestRegressor”类,而不是“DecisionTreeRegressor”。
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))
191669.7536453626
结论
可能还有进一步改进的空间,但与最佳决策树错误250000相比,这是一个很大的改进。有一些参数允许您更改随机林的性能,就像我们更改单个决策树的最大深度一样。但随机森林模型最好的特点之一是,即使没有这种调整,它们通常也能正常工作。
轮到你了
尝试使用随机森林模型看看它对你的模型有多大的改进。
以下为练习部分
扼要重述
这里是你之前写的代码。
# Code you have previously used to load data
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
# Path of the file to read
iowa_file_path = '../input/home-data-for-ml-course/train.csv'
home_data = pd.read_csv(iowa_file_path)
# Create target object and call it y
y = home_data.SalePrice
# Create X
features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
X = home_data[features]
# Split into validation and training data
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
# Specify Model
iowa_model = DecisionTreeRegressor(random_state=1)
# Fit Model
iowa_model.fit(train_X, train_y)
# Make validation predictions and calculate mean absolute error
val_predictions = iowa_model.predict(val_X)
val_mae = mean_absolute_error(val_predictions, val_y)
print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
# Using best value for max_leaf_nodes
iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
iowa_model.fit(train_X, train_y)
val_predictions = iowa_model.predict(val_X)
val_mae = mean_absolute_error(val_predictions, val_y)
print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
# Set up code checking
from learntools.core import binder
binder.bind(globals())
from learntools.machine_learning.ex6 import *
print("\nSetup complete")
Validation MAE when not specifying max_leaf_nodes: 29,653
Validation MAE for best value of max_leaf_nodes: 27,283
Setup complete
练习
数据科学并不总是那么容易。但用随机森林取代决策树将是一个轻松的胜利。
第一步:使用随机森林
from sklearn.ensemble import RandomForestRegressor
# Define the model. Set random_state to 1
rf_model = ____
# fit your model
____
# Calculate the mean absolute error of your Random Forest model on the validation data
rf_val_mae = ____
print("Validation MAE for Random Forest Model: {}".format(rf_val_mae))
# Check your answer
step_1.check()
继续
准备好了吗? 进入 竞赛 部分
练习答案
# step 1:
rf_model = RandomForestRegressor()
# fit your model
rf_model.fit(train_X, train_y)
# Calculate the mean absolute error of your Random Forest model on the validation data
rf_val_predictions = rf_model.predict(val_X)
rf_val_mae = mean_absolute_error(rf_val_predictions, val_y)