Kaggle机器学习之建模必要流程

Kaggle的机器学习教程中,概括了建模的几个常识或者必要流程:

1. 清洗好数据,得到X和y。

2. 选择合适的模型,面对未知的数据和业务需求可以先尝试不同的模型。

3. 将样本数据分为训练数据和检验数据两类,训练数据带入模型,参数可先从简,检验数据进行模型检验。

4. 模型参数优化,以防欠拟合和过拟合。

以下为对应代码 :

  1. 清洗好数据,得到X和y。
import pandas as pd

main_file_path = '../input/train.csv' # this is the path to the Iowa data that you will use
data = pd.read_csv(main_file_path)
#target y and input x
y = data.SalePrice
predictors = ['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr',
             'TotRmsAbvGrd']
x = data[predictors]
  1. 用决策树模型,将样本数据分为训练数据和检验数据两类,训练数据带入模型,参数可先从简,检验数据进行模型检验(MAE,平均绝对偏差)。
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

train_X, val_X, train_y, val_y = train_test_split(x, y,random_state = 0)
lowa_model = DecisionTreeRegressor()
lowa_model.fit(train_X,train_y)
val_prices = lowa_model.predict(val_X)

mean_absolute_error(val_y, val_prices)

3.模型参数优化

def get_mae(max_leaf_nodes, predictors_train, predictors_val, targ_train, targ_val):
    model = DecisionTreeRegressor(max_leaf_nodes=max_leaf_nodes, random_state=0)
    model.fit(predictors_train, targ_train)
    preds_val = model.predict(predictors_val)
    mae = mean_absolute_error(targ_val, preds_val)
    return(mae)

#求得最佳参数
import numpy as np
case = []
for max_leaf_nodes in range(5,500):
    my_mae = get_mae(max_leaf_nodes, train_X, val_X, train_y, val_y)
    case.append(my_mae)
#     print("Max leaf nodes: %d  \t\t Mean Absolute Error:  %d" %(max_leaf_nodes, my_mae))
case = np.array(case)
print (np.min(case))
ii = np.where(case==np.min(case)) 
print ("The best leaf nodes is ", ii[0][0]+5)

你可能感兴趣的:(数据分析与机器学习)