美文网首页
第四天-模型选择

第四天-模型选择

作者: 徐凯_xp | 来源:发表于2018-07-25 23:38 被阅读0次

    1.错误类型

    • 过拟合
    • 欠拟合

    2.模型复杂度图表



    3.交叉验证集
    用语选择模型



    4.K折交叉验证

    一个非常有用的循环利用数据的方法
    在K折交叉验证中,将数据分为K个包



    如上图所示,这里K = 4,然后我们将模型培训K次



    每次将不同的包用作测试集,剩下的作为训练集,然后求结果的平均值,得到最终模型。
    from sklearn.model_selection import KFold
    kf = KFold(12,3,shuffle = True) #参数为数据大小和测试集的大小,shuffle = True 表示随机
    for train_indices, test_indices in kf:
        print train_indices, test_indicies
    

    建议随机初始化数据,以消除任何可能的偏差。

    学习曲线

    通过学习曲线检测过拟合和欠拟合
    将使用三个模型来训练下面的圆形数据集

    • 决策树模型
    • 逻辑回归模型
    • 支持向量机模型

    其中一个模型会过拟合,一个欠拟合,还有一个正常。首先,我们将编写代码为每个模型绘制学习曲线,最后我们将查看这些学习曲线,判断每个模型对应哪个曲线
    首先,请记住三个模型的学习曲线外观如下所示:


    网格搜索

    在sklearn 中的网格搜索
    在 sklearn 中的网格搜索非常简单。 我们将用一个例子来说明一下。 假设我们想要训练支持向量机,并且我们想在以下参数之间做出决定:

    • kernel:poly或rbf。
    • C:0.1,1 或 10。
      具体步骤如下所示:
    1. 导入 GridSearchCV
    from sklearn.model_selection import GridSearchCV
    

    2.选择参数
    现在我们来选择我们想要选择的参数,并形成一个字典。 在这本字典中,键 (keys) 将是参数的名称,值 (values) 将是每个参数可能值的列表。

    parameters = {'kernel':['poly', 'rbf'],'C':[0.1, 1, 10]}
    

    3.创建一个评分机制(scorer)
    我们需要确认将使用什么指标来为每个候选模型评分。 这里,我们将使用 F1 分数。

    from sklearn.metrics import make_scorer
    from sklearn.metrics import f1_score
    scorer = make_scorer(f1_score)
    
    1. 使用参数 (parameter) 和评分机制 (scorer) 创建一个 GridSearch 对象。 使用此对象与数据保持一致 (fit the data)
    # Create the object.
    grid_obj = GridSearchCV(clf, parameters, scoring=scorer)
    # Fit the data
    grid_fit = grid_obj.fit(X, y)
    

    5.获得最佳估算器 (estimator)

    best_clf = grid_fit.best_estimator_
    
    例子

    使用网格搜索来完善模型
    1.首先,定义一些参数来执行网格搜索。 我们建议使用max_depth, min_samples_leaf, 和 min_samples_split。

    2.使用f1_score,为模型制作记分器。

    3.使用参数和记分器,在分类器上执行网格搜索。

    4.将数据拟合到新的分类器中。

    5.绘制模型并找到 f1_score。

    6.如果模型不太好,请尝试更改参数的范围并再次拟合。

    from sklearn.metrics import make_scorer
    from sklearn.model_selection import GridSearchCV
    
    clf = DecisionTreeClassifier(random_state=42)
    
    # TODO: Create the parameters list you wish to tune.
    parameters = {'max_depth':[2,4,6,8,10],'min_samples_leaf':[2,4,6,8,10], 'min_samples_split':[2,4,6,8,10]}
    
    # TODO: Make an fbeta_score scoring object.
    scorer = make_scorer(f1_score)
    
    # TODO: Perform grid search on the classifier using 'scorer' as the scoring method.
    grid_obj = GridSearchCV(clf, parameters, scoring=scorer)
    
    # TODO: Fit the grid search object to the training data and find the optimal parameters.
    grid_fit = grid_obj.fit(X_train, y_train)
    
    # Get the estimator.
    best_clf = grid_fit.best_estimator_
    
    # Fit the new model.
    best_clf.fit(X_train, y_train)
    
    # Make predictions using the new model.
    best_train_predictions = best_clf.predict(X_train)
    best_test_predictions = best_clf.predict(X_test)
    
    # Calculate the f1_score of the new model.
    print('The training F1 Score is', f1_score(best_train_predictions, y_train))
    print('The testing F1 Score is', f1_score(best_test_predictions, y_test))
    
    # Plot the new model.
    plot_model(X, y, best_clf)
    
    # Let's also explore what parameters ended up being used in the new model.
    best_clf
    

    相关文章

      网友评论

          本文标题:第四天-模型选择

          本文链接:https://www.haomeiwen.com/subject/vmipmftx.html